summaryrefslogtreecommitdiffstats
path: root/cron-jobs/ftpdir-cleanup
blob: 83c7cf1e3eab9ea2a37eef99be2b4ca049a0ae56 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/bash

if [ $# -ne 2 ]; then
	echo "usage: $(basename $0) <reponame> <arch>"
	exit 1
fi

reponame=$1
arch=$2

##### Arch specific stuff. TODO make this configurable #####
ftppath="/home/ftp/$reponame/os/$arch/"
############################################################

if [ ! -d "$ftppath" ]; then
    echo "FTP path '$ftppath' does not exist"
    exit 1
fi

if [ ! -f /etc/makepkg.conf ]; then
    echo "/etc/makepkg.conf not found! Aborting"
    exit 1
fi

. /etc/makepkg.conf

getpkgname() {
	local tmp

	tmp=${1##*/}
	tmp=${tmp%$PKGEXT}
	tmp=${tmp%-$arch}
	echo ${tmp%-*-*}
}

getpkgname_ver() {
	local tmp

	tmp=${1##*/}
	tmp=${tmp%$PKGEXT}
	echo ${tmp%-$arch}
}

MISSINGFILES=""
DELETEFILES=""
EXTRAFILES=""

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

cd "${TMPDIR}"
/bin/tar xzf "$ftppath/$reponame.db.tar.$DB_COMPRESSION"

for pkg in *; do
  filename=$(grep -A1 '^%FILENAME%$' "${pkg}/desc" | tail -n1)
  [ -z "${filename}" ] && filename="${pkg}.pkg.tar.gz"

  if [ ! -f "${ftppath}/${filename}" ]; then
    MISSINGFILES="${MISSINGFILES} ${filename}"
  else
    pkgname="$(getpkgname ${filename})"
    for otherfile in ${ftppath}/${pkgname}-*; do
      otherfile="$(basename ${otherfile})"
      if [ "${otherfile}" != "${filename}" -a "${pkgname}" = "$(getpkgname ${otherfile})" ]; then
        DELETEFILES="${DELETEFILES} ${otherfile}"
      fi
    done
  fi
done

cd "$ftppath"
for pkg in *$PKGEXT; do
    pkgname="$(getpkgname_ver ${filename})"
    if [ ! -d "$TMPDIR/$pkgname" ]; then
        EXTRAFILES="$EXTRAFILES $pkg"
    fi
done 



cd "$ftppath"
rm -rf ${TMPDIR}

if [ -n "${DELETEFILES}" ]; then
    #rm -f ${DELETEFILES}
    #mv ${DELETEFILES} /home/package-cleanup/
    echo ""
fi

echo "Scan complete for $reponame ($arch) at ${ftppath}"
if [ -n "$DELETEFILES" ]; then
    echo "    The following files have been moved to package-cleanup:"
    for f in $DELETEFILES; do
        echo "        $f"
    done
fi
echo ""
if [ -n "$MISSINGFILES" ]; then
    echo "    The following files are missing in the repo:"
    for f in $MISSINGFILES; do
        echo "        $f"
    done
fi
echo ""
if [ -n "$EXTRAFILES" ]; then
    echo "    The following files are in the repo but not the db:"
    for f in $EXTRAFILES; do
        echo "        $f"
    done
fi