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

#
##
### Configuration variables

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

#
##
### Sanity checks

DIR="$NONSPAMTRAP_MAILDIR/new"

if [ ! -d "$DIR" ]; then
    echo "$NONSPAMTRAP_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 --ham $DIR | mail -s 'sa-education-nonspamtrap' postmaster

# Delete these emails (if there are some).
if [ `ls -l $DIR | wc -l` -gt 1 ]; then rm $DIR/*; fi
