#!/bin/bash

CHRBIN=/usr/sbin/chroot
CHRDIR=/chroot/maild
MAILROOT=/var/mail
TMP=/tmp/salearnham

# DEBUG="-D all,rules,info,plugin"

logarchive=`find /var/log/ -mtime -15 -mtime +1 -name 'mail.log*.gz' 2>/dev/null |grep -v 'mail.login' |tail -n1`

if [ -z "$logarchive" ]; then
	exit 0
fi

logafile=`basename $logarchive`

mboxes=`zgrep "imap-login: Login:" /var/log/${logafile} |less |sed -E "s/.*user=<([^>]*)>.*/\1/" |sort |uniq |shuf |head -n100`

[ -d $CHRDIR$TMP ] || mkdir $CHRDIR$TMP

count=0
for mbox in $mboxes; do

	user=${mbox%%@*}
	dom=${mbox##*@}
	
	mdir="${CHRDIR}${MAILROOT}/$dom/$user/Maildir"

	find $mdir/cur/ -type f -size -50k -type f -mtime +7 -mtime -12 | shuf | head -n15 | xargs -r -d '\n' cp -t $CHRDIR$TMP
	if (chown amavis:amavis $CHRDIR$TMP/* >/dev/null 2>&1); then
		find $CHRDIR$TMP/ -type f -exec mv '{}' '{}'.gz \;
		gunzip $CHRDIR$TMP/*
		dircount=`ls -1 $CHRDIR$TMP | wc -l`
		count=$((count+dircount))
		#echo "$mdir: $dircount (sum: $count)"
		$CHRBIN $CHRDIR su amavis -c "sa-learn --no-sync --ham $TMP" >/dev/null
		rm $CHRDIR$TMP/*
	fi

	if [ $count -gt 200 ]; then
		break;
	fi

done

$CHRBIN $CHRDIR su amavis -c "sa-learn --sync" >/dev/null

rmdir $CHRDIR$TMP

exit 0
