#!/bin/bash
#
# spamclean
# =============
# The spamclean script is only necessary if spamkiller is run in quarantine
# mode, and is designed to be run as a daily cron job. It uses the
# offsetdate utility to decide which quarantined spam messages should be
# deleted. offsetdate is a free download from www.libelle-systems.com
# where it is one of the utilities distributed with the 'C environment
# support library'.
#
# Copyright 2011, Martin Gregorie.
#
# This file is part of the Spamkiller package.
#
# Spamkiller is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# Spamkiller is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Spamkiller.  If not, see <http://www.gnu.org/licenses/>.
#              

help=no
debug=no
retain=7
verbose=yes
error=no
qdir="/home/getmail/quarantined"

for opt in $*
do
	case $opt in
	-\?)	help=yes;;
	-d)	debug=yes;;
	-r=*)	n=$(expr length "$opt"); 
		retain=$(expr substr "$opt" 4 $n);;
	-nv)	verbose=no;;
	-*)	echo "spamclean: unrecognised option $opt"; error=yes;;
	*)	qdir="$opt";;
	esac
done

if [ $help == 'yes' ]
then
	echo "Syntax:   spamclean [opts] pathname"
	echo "Function: Delete old spam from the quarantine list built"
	echo "          by spamkiller."
	echo "          'pathname' is the absolute pathname of the quarantine"
	echo "          and defaults to /home/getmail/quarantined."
	echo "          This script is normally run as a cron job."
	echo "Options:"
	echo "          -d    Run in debug mode"
	echo "          -r=7  Number of days of spam to retain. Default is 7."
	echo "          -nv   Run in non-verbose mode. This reports counts of"
	echo "                spam retained and deleted."
	exit 1
fi

n1=$(expr "$retain" : '[+0-9-]*')
n2=$(expr length "$retain")
if [ "$n1" -ne "$n2" ]
then
	echo "spamclean: retention must be an integer"
	error=yes
else
	if [ "$retain" -lt 1 ]
	then
		echo "spamclean: retention period must be 1 or more days"
		error=yes
	else
		retain="-$retain"
	fi
fi

if [ ! -d "$qdir" ]
then
	echo "spamclean: $qdir is not an accessable directory"
	error=yes
fi

if [ $error == 'yes' ]
then
	exit 1
fi

match=$(/usr/local/bin/offsetdate -o=$retain -e=day -f="%Y-%m-%d")

if [ $debug == 'yes' ]
then
	echo "retain     = $retain"  
	echo "verbose    = $verbose"
	echo "qdir       = $qdir"
	echo "match date = $match"
fi

if [ $verbose == yes ]
then
	echo "All spam recorded before $match will be deleted"
	echo
fi

total=0
totdel=0
totret=0
for f in $(ls "$qdir")
do
	total=$(expr $total + 1)
	res=$(expr "$f" \< "$match")  
	if [ "$res" == '1' ]
	then
		totdel=$(expr $totdel + 1)
		rm "$qdir/$f"
	else
		totret=$(expr $totret + 1)
	fi
done

if [ $verbose == yes ]
then
	echo "Quarantined spam statistics"
	echo "---------------------------"
	echo "In quarantine $total"
	echo "Deleted       $totdel"
	echo "Retained      $totret"
	echo "---------------------------"
fi
