#!/bin/sh
# Run the expire function of apt-cacher-ng non-interactively (unless errors 
# are detected).

# optional, specify hostname to appear in error messages and links
# HOSTNAME=externallyVisibleHostName
# export HOSTNAME

test -x /usr/lib/apt-cacher-ng/acngtool || exit 0

if test -e /etc/default/apt-cacher-ng
then
        . /etc/default/apt-cacher-ng
        test -z "$NO_CRON_RUN" || exit 0
fi

if [ -d /var/log/apt-cacher-ng ]; then
		# rm older than 10 days
		find /var/log/apt-cacher-ng -maxdepth 1 -name 'maint_*.log.html.xz' -or -name 'maint_*.log.html' -type f -user apt-cacher-ng -mtime +10 -delete
		# compress older than 5 days
		find /var/log/apt-cacher-ng -maxdepth 1 -name 'maint_*.log.html' -type f -user apt-cacher-ng -mtime +5 -print0 | xargs -r0 xz
fi

rpage=$(/usr/lib/apt-cacher-ng/acngtool -c /etc/apt-cacher-ng 2>/dev/null printvar ReportPage)
if test 0 = $(echo "$rpage" | wc -w)
then
	echo Warning: ReportPage setting of apt-cacher-ng was disabled by administrator. >&2
	echo See /usr/share/doc/apt-cacher-ng/README.Debian for details. >&2
	exit 42
fi

/usr/lib/apt-cacher-ng/acngtool maint -c /etc/apt-cacher-ng SocketPath=/var/run/apt-cacher-ng/socket
