#! /bin/bash
# Usage: sa-education-spamtrap
#
# Purpose: inject into 'sa-learn --spam' and delete the mail received by a
# spamtrap whose Maildir is located in $SPAMTRAP_MAILDIR.

#
##
### Configuration variables

SPAMTRAP_MAILDIR='/var/mail/boum.org/spam/Maildir'
SA_USER_PREFS='/var/lib/amavis/.spamassassin/sa-education-spamtrap_user_prefs'

#
##
### Sanity checks

DIR="$SPAMTRAP_MAILDIR/new"

if [ ! -d "$DIR" ]; then
    echo "$SPAMTRAP_MAILDIR is not a valid Maildir folder."
    exit 1
fi

if [ ! -r "$SA_USER_PREFS" ]; then
    echo "$SA_USER_PREFS does not exist."
    exit 1
fi

#
##
### Main

# Inject these emails into sa-learn.
sa-learn --prefs-file=$SA_USER_PREFS --spam $DIR

# Delete these emails, redirecting stderr to /dev/null, to prevent silly error
# messages if there is no mail in there.
if [ `ls -l $DIR | wc -l` -gt 1 ]; then rm $DIR/*; fi
