#!/bin/bash

LDIR=/home/admin/log
LFN="${LDIR}/${USER}.log"

## Retrieve and Process Spam & Ham from 'spam-learn' & 'spam-unlearn' folders
/usr/bin/fetchmail -a -s -n --folder spam-learn -m '/usr/bin/vendor_perl/sa-learn --spam' &>/dev/null
mss=$?
sleep 2
/usr/bin/fetchmail -a -s -n --folder spam-unlearn -m '/usr/bin/vendor_perl/sa-learn --ham' &>/dev/null
mhs=$?

[[ -d "$LDIR" ]] || mkdir -p "$LDIR"

if [[ -w "$LDIR" ]]; then

  if [[ $(($mhs+$mss)) -le 2 ]]; then
    echo "$(date +'%b %e %R:%S') $HOSTNAME ${0##*/}: sa-learn completed successfully for user $USER" >>$LFN
  else
    echo "$(date +'%b %e %R:%S') $HOSTNAME ${0##*/}: sa-learn FAILED for user $USER" >>$LFN
  fi

fi

