summaryrefslogtreecommitdiffstats
path: root/cron-jobs/ftpdir-cleanup
blob: 18e26f287d8e3bf111c074bbef1e6f98caffdd02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/bin/bash

usage() {
  echo "Usage: $0 repo architecture"
}

getpkgname() {
  local tmp

  tmp=${1##*/}
  tmp=${tmp%.pkg.tar.gz}
  tmp=${tmp%-i686}
  tmp=${tmp%-x86_64}
  echo ${tmp%-*-*}
}

reponame=$1
arch=$2

FTPBASEDIR="/home/ftp"
FTPDIR=${FTPBASEDIR}/$reponame/os/$arch
DBFILE=${FTPDIR}/$reponame.db.tar.gz
MISSINGFILES=""
DELETEFILES=""

if [ $# -lt 2 -o ! -f ${DBFILE} ]; then
  usage
  exit 1
fi

TMPDIR=$(mktemp -d /tmp/cleanup.XXXXXX) || exit 1

cd ${TMPDIR}
tar xzf ${DBFILE}
for pkg in *; do
  filename=$(grep -A1 '^%FILENAME%$' ${pkg}/desc | tail -n1)
  [ -z "${filename}" ] && filename="${pkg}.pkg.tar.gz"
  if [ ! -f ${FTPDIR}/${filename} ]; then
    MISSINGFILES="${MISSINGFILES} ${filename}"
  else
    pkgname="$(getpkgname ${filename})"
    for otherfile in ${FTPDIR}/${pkgname}-*; do
      otherfile="$(basename ${otherfile})"
      if [ "${otherfile}" != "${filename}" -a "${pkgname}" = "$(getpkgname ${otherfile})" ]; then
        DELETEFILES="${DELETEFILES} ${otherfile}"
      fi
    done
  fi
done

cd - >/dev/null
rm -rf ${TMPDIR}

echo -ne "DIRECTORY:\n${FTPDIR}\n\n"
echo -ne "DELETEFILES:\n${DELETEFILES}\n\n"
echo -ne "MISSINGFILES:\n${MISSINGFILES}\n\n"

cd ${FTPDIR}
[ -n "${DELETEFILES}" ] && mv ${DELETEFILES} /home/package-cleanup/
cd - >/dev/null