#!/bin/bash
#                      /usr/local/bin/darlimit.pixy
# http://crystalfaeries.net/posix/bin/darlimit.pixy
# celeste crystalfaery 2016-11-14 20:04:17+00:00
# darlimit deletes old diskarchive (dar) backups to prevent disk full condition

# where is the darlimit config file? ############################################################################
limit=$HOME/.darlimit

# where do we log? ##############################################################################################
log=/var/log/dar.log	# combined darchive log

# what partition holds the dar archives? ########################################################################
partition=/var/cache/dar	# /var/cache/dar

# where are the .dar backups we delete? #########################################################################
mkdir -p /var/cache/dar/`/bin/hostname`								|| exit 1
cd	 /var/cache/dar/									|| exit 1

touch /tmp/darlimit$$	# one shot e-mail flag
let darlimit=`head -n 1 $limit | cut -f 1` # fetch from config file
let percent=$darlimit	# assume we are at limit already
let sleep=100-$percent	# sleep less the fuller we are

# loop forever as a daemon; checking more frequently as disk fills
while sleep "$sleep"m
do
	let darlimit=`head -n 1 $limit | cut -f 1` # re-fetch from config file in case administrator changed it
	percent=$(df -h | grep $partition | sed 's/\%.*$// ; s/^.* //') || exit 2
	let sleep=100-$percent	# sleep less the fuller we are
	if [[ $percent -gt $darlimit ]]
	then	# we have exceeded our disk usage limit

		/usr/local/bin/thumbnail_expire	"$sleep" &		# release the daleks in the background

		find . -name '*.dar' > /tmp/$$.txt			# find any dar files
		if [ -s /tmp/$$.txt ]
		then
			oldest=$(ls -rt $(cat /tmp/$$.txt) | head -n 1) # got oldest .dar file
echo			remove	"$oldest"	| mail -s "$(hostname) $(basename $0) $oldest removed." root &
			rm	"$oldest"				# bye oldest .dar file
		else	# we have no more .dar files left to remove, what else can we free up?
			if [ -f /tmp/darlimit$$ ]
			then	# one shot e-mail flag exists, this is our one shot to notify administrator
				rm /tmp/darlimit$$
echo				"Process $$ Disk Usage: $percent \nTouch /tmp/darlimit$$ to reenable e-mail notification." \
				| mail -s "$(hostname) $(basename $0) OUT OF DISK SPACE" root &
				aptitude autoclean			# first level package cleaning
				apt-get autoclean			# first level package cleaning
			else	# one shot e-mail flag is gone, we already notified the administrator
				aptitude clean				# second level package cleaning
				apt-get clean				# second level package cleaning
				# we are now out of disk space and are out of ways to free up space
				# our only remaining option is to kill the darchive process eating disk
echo				"`/usr/local/bin/now` $0 killing dar for lack of diskspace" >> $log
echo				"`/usr/local/bin/now` $0 killing dar for lack of diskspace" |& mail -s "$(hostname) $(basename $0) darchive killed." root &
				touch /tmp/darlimit$$	# one shot e-mail flag re-enabled for next dar execution
				pkill dar		# so long, farewell, goodbye
				let sleep=$sleep+$sleep	# binary back-off
			fi
		fi
	fi	
	if [[ $sleep -lt 1 ]]
	then
		let sleep=1	# minimum sleep time so somebody besides me can also run
	fi
done

