#!/bin/bash
#
#  routinely purge amavis saved junkfiles and users junk mail
#
# ::copy::
# ::maintainer::
#
CMD=$(basename $0)
CMDVER="2.1"
CMDSTR="$CMD v$CMDVER (2022-03-01)"

set -e -u

report_removed()
{
	local text=$1
	local actual_count=$(find . -type f -print | wc -l)

	if [ $Files_count != $actual_count ]
	then
		printf " purged %6d %s\n" $(expr $Files_count - $actual_count) "$text"
		Files_count=$actual_count
	fi
}

# (MAIN)

trap '' EXIT
trap 'echo -e "\n*INTR\n"; exit 255' 1 2 3
trap 'echo -e "\nunexpected error $? at $LINENO\n"' ERR

# defaults
#
cfgfile="/etc/default/$CMD"
[ -f $cfgfile ] && {
	. $cfgfile
}
WORKDIR=${WORKDIR:-$(kusa-conf amavis.workdir 2>/dev/null)}
WORKDIR=${WORKDIR:-"/w/work/amavis"}

DELETE_ACTION=${DELETE_ACTION:-"-print"}
LIFE_DEFAULT=${LIFE_DEFAULT:-30}

LIFE_SPAM=${LIFE_SPAM:-$LIFE_DEFAULT}
ACTION_SPAM=${ACTION_SPAM:-$DELETE_ACTION}

LIFE_BANNED=${LIFE_BANNED:-$LIFE_DEFAULT}
ACTION_BANNED=${ACTION_BANNED:-$DELETE_ACTION}

LIFE_VIRUS=${LIFE_VIRUS:-$LIFE_DEFAULT}
ACTION_VIRUS=${ACTION_VIRUS:-$DELETE_ACTION}

LIFE_OTHERS=${LIFE_OTHERS:-$LIFE_DEFAULT}
ACTION_OTHERS=${ACTION_OTHERS:-$DELETE_ACTION}


CLEAN_USERDIRS=${CLEAN_USERDIRS:-"false"}
USERS_DIRS=${USERS_DIRS:-"/home"}
USERS_SPAMDIRS=${USERS_SPAMDIRS:-"Junk Spamassassin"}
USERS_LIFE_JUNK=${USERS_LIFE_JUNK:-"60"}
USERS_MAX_JUNK=${USERS_MAX_JUNK:-"100"}




virus_dir="$WORKDIR/virusmails"

[ -d "$virus_dir" ] || exit 0

# sanity checks
#
for userdir in $USERS_DIRS
do
	case $userdir in
	  /*)	;; #ok
	  *)	echo "$CMD error in \$USERS_DIRS definition" >&2
	  	echo "  dir '$userdir': paths must be absolute" >&2
		exit 1
		;;
	esac
done


# do we clean users dirs?
#
case $CLEAN_USERDIRS in
  true|y|Y|yes|YES|1)	CLEAN_USERDIRS=true ;;
  *)			CLEAN_USERDIRS=false ;;
esac



cd "$virus_dir"

echo
echo "== $CMDSTR == CLEANUP REPORT =="
echo
echo " TOTAL VIRUS:   " $(find . -type f -name 'virus-*' -print | wc -l)
echo " TOTAL SPAM:    " $(find . -type f -name 'spam-*' -print | wc -l)
echo " TOTAL BANNED:  " $(find . -type f -name 'banned-*' -print | wc -l)
echo " TOTAL OTHERS:  " $(find . -type f -print | egrep -v 'virus-|spam-|banned-' | wc -l)
echo

fmt=" %-18s %4s %s\n"
printf "$fmt" "CLEAN OBJECTS:" "life" "action"
printf "$fmt" "  virus:"	$LIFE_VIRUS "$ACTION_VIRUS"
printf "$fmt" "  spam:"		$LIFE_SPAM "$ACTION_SPAM"
printf "$fmt" "  banned:"	$LIFE_BANNED "$ACTION_BANNED"
printf "$fmt" "  other:"	$LIFE_OTHERS "$ACTION_OTHERS"
printf "$fmt" "  default:"	$LIFE_DEFAULT "$DELETE_ACTION"
echo
echo " CLEAN USERDIRS:      $CLEAN_USERDIRS ($USERS_SPAMDIRS)"
printf "$fmt" "  junk age:"	$USERS_LIFE_JUNK "$DELETE_ACTION"
printf "$fmt" "  junk max count:" $USERS_MAX_JUNK "$DELETE_ACTION"
echo


Files_count=$(find . -type f -print | wc -l)

find . -type f -name 'virus-*' -mtime +$LIFE_VIRUS $DELETE_ACTION || :
report_removed "VIRUS files in $virus_dir"

find . -type f -name 'spam-*' -mtime +$LIFE_SPAM $DELETE_ACTION || :
report_removed "SPAM files in $virus_dir"

find . -type f -name 'banned-*' -mtime +$LIFE_BANNED $DELETE_ACTION || :
report_removed "BANNED files in $virus_dir"

do_delete=false
do_print=false
echo "X$ACTION_OTHERS" | grep -q -- '-delete' && do_delete=true
echo "X$ACTION_OTHERS" | grep -q -- '-print' && do_print=true

find . -type f -mtime +$LIFE_OTHERS | egrep -v 'virus-|spam-|banned-' | while read file
do
	$do_delete && rm "$file"
	$do_print && echo "$file"
done
report_removed "OTHERS files in $virus_dir\n"


$CLEAN_USERDIRS && {

	do_delete=false
	do_print=false

	echo "X$DELETE_ACTION" | grep -q "X.*-delete" && do_delete=true
	echo "X$DELETE_ACTION" | grep -q "X.*-print" && do_print=true

	file_pattern='[0-9][0-9][0-9][0-9][0-9]*.*'

	for userdir in $USERS_DIRS
	do
		for maildir in $USERS_SPAMDIRS
		do
			ls -d $userdir/*/Maildir/.$maildir/cur 2>/dev/null
			ls -d $userdir/*/Maildir/.$maildir/new 2>/dev/null
		done
	done | sort -u | while read dir
	do
		cd "$dir"

		Files_count=$(find . -type f -name "$file_pattern" -print | wc -l)
		#printf "userdir: %6d files in %s\n" $Files_count "$dir" >&2

		find . -type f -name "$file_pattern" -mtime +$USERS_LIFE_JUNK $DELETE_ACTION || :

		report_removed "JUNK (aged) email in $dir"

		Files_count=$(find . -type f -name "$file_pattern" -print | wc -l)
		#printf "userdir: %6d files in %s\n" $Files_count "$dir" >&2

		capfiles=$(ls -t [0-9][0-9][0-9][0-9][0-9]*.* 2>/dev/null | sed -e "1,${USERS_MAX_JUNK}d")

		[ "$capfiles" != "" ] && {
			$do_print && { echo $capfiles | sed -e "s/ /\n/g"; }
			$do_delete && rm -f $capfiles
		}

		report_removed "JUNK email in $dir"
	done

} # $CLEAN_USERDIRS


echo
echo "-- $CMDSTR"

exit 0
