#!/bin/sh

user="$1"
[ "$user" = "" ] && echo "usage: run_one_nitemc username"
[ "$user" = "" ] && exit 99

# where the $HOMEs are:
userdir=/export/home/nitemc/$user
svndir=/export/home/nitemc/svn

mkdir -p $userdir
cd $userdir
mv log log.1

(
echo "Starting corpus-nightly for $user ($svndir , $userdir)"

. /etc/profile
HOME=$userdir
PERL=/local/perl586/bin/perl
TMPDIR=/tmpfs
export HOME PERL TMPDIR

# create the .corpus file, keeping the rsync password secret;
# the string __RSYNC_PASSWORD__ will be replaced with the
# contents of /export/home/nitemc/USERNAME/rsync_password

src=$svndir/backend/nitemc/corpus.$user
[ -f $src ] || src=$svndir/backend/nitemc/corpus.default

if [ ! -f rsync_password ] ; then
  cp ../rsync_password.default rsync_password
fi

perl -pe '
  s{__USERNAME__}{'$user'}g;
  s{__RSYNC_PASSWORD__}{ incfile("rsync_password") }eg;

  sub incfile {
    my $file = shift;
    my $new=`cat $file`; chop $new; $new =~ s/\s+/ /gs;
    return "\"".$new."\"";
  }
' $src > .corpus

# clean up any trash from another user's mass-checks
rm -rf $svndir/masses/tmp
rm -rf $svndir/masses/spamassassin
rm -rf $svndir/masses/*.log

# ensure we use the shared SVN checkout
rm -f svn
ln -s ../svn svn

# set up default user_prefs, override with a custom one if existing
echo '
  use_bayes 0
  use_auto_whitelist 0
  internal_networks 127/8
  trusted_networks 127/8
' > user_prefs.default
[ -f user_prefs.nightly ] || cp user_prefs.default user_prefs.nightly
[ -f user_prefs.weekly ] || cp user_prefs.default user_prefs.weekly

# ensure we always have at least 1 mail of each class for each user
for class in ham spam ; do
  corpusdir=/export/home/bbmass/uploadedcorpora/$user/$class
  if [ `ls $corpusdir | wc -l` = 0 ] ; then
    mkdir -p $corpusdir
    cp $svndir/backend/nitemc/empty.mbox $corpusdir
  fi
done

cd $svndir
exec nice bash $svndir/masses/rule-qa/corpus-nightly

) 2>&1 | tee log

