#!/bin/sh

# settings are located in $HOME/.corpus

. $HOME/.corpus

renice 10 -p $$

set -x
set -e
export RSYNC_PASSWORD=$password
export TIME="%e,%U,%S"
export TZ=UTC

# ensure we're running after 0900 UTC
if date | egrep -q '^... ... .. (08|10):'; then
	exit
fi

# day of week
if date | egrep -q '^Sat' || [ "$1" = "net" ]; then
	net="net-"
else
	net=""
fi

# enter tree
cd $tree

# find current revision
rm -f weekly-versions.txt nightly-versions.txt
if ! wget --tries=120 --wait=30 http://rsync.spamassassin.org/weekly-versions.txt http://rsync.spamassassin.org/nightly-versions.txt; then
	echo "wget failed" 1>&2
	exit 0
fi
if [ -n "$net" ]; then
	revision=$(tail -1 weekly-versions.txt|awk '{print $2}')
else
	revision=$(tail -1 nightly-versions.txt|awk '{print $2}')
fi

# update
set +e
retry=0
while true; do
	killall -TERM svn
	sleep 10
	killall -KILL svn
	if svn update -r $revision; then
		break;
	fi
	if [ $retry -eq 120 ]; then
		echo "svn update failed" 1>&2
		exit 1
	fi
	retry=$(( $retry + 1 ))
	sleep 20
done
set -e

# run test
cd masses
rm -f spamassassin/auto*
rm -f spamassassin/bayes*
rm -f razor-agent.log
date > test.start
if [ -n "$net" ]; then
	prefs=$prefs_weekly
	opts=$opts_weekly
else
	prefs=$prefs_nightly
	opts=$opts_nightly
fi
if [ -n "$prefs" -a -f "$prefs" ]; then
	cp $prefs spamassassin/user_prefs
else
	rm -f spamassassin/user_prefs
fi
./mass-check $opts
uptime
date > test.end

# results name
mv spam.log spam-$net$username.log
mv ham.log ham-$net$username.log

# rsync
set +e
retry=0
while true; do
	if rsync -CPcvuzb --timeout=120 spam-$net$username.log ham-$net$username.log $username@rsync.spamassassin.org::corpus/; then
		break;
	fi
	if [ $retry -eq 120 ]; then
		echo "rsync failed" 1>&2
		exit 1
	fi
	retry=$(( $retry + 1 ))
	sleep 30
done
set -e
