#!/bin/bash

echo -n "HAM-learn start: "
date

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

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

oldlogs=`find /var/log/ -mtime -9 -mtime +3 -name "mail.log[.-]*.gz"`

if [ -n "$oldlogs" ]; then

	mboxes=`zgrep "imap-login: Login:" $oldlogs |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="${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

fi

echo -n "HAM-learn end: "
date
