#!/bin/sh 
#
#	xkal-automail : Schedules an at job for the time given as
#		argument. The job will basically do what xkal-mail
#		does, namely mail the day's appointments if there are
#		any non-zero-level ones. The job reschedules itself
#		for the next day.
#
#	Note that the first automatic mailing will be "tomorrow" at
#	the given time, not today, even it could be.
#
#	George Ferguson, ferguson@cs.rochester.edu, 19 Feb 1991.
#

xkal=xkal
mailer=Mail
tmp=/tmp/xkal-automail$USER

case $# in
    1) when=$1 ; rec=${USER:?} ;;
    2) when=$1 ; rec=$2 ;;
    *) echo 'usage: xkal-automail when [recipient]' 1>&2; exit 1
esac

at -s $when tomorrow >/dev/null 2>&1 <<END_OF_SCRIPT
trap "rm -f $tmp; exit 1" 1 2 3 15
if $xkal -listOnly -exitUsesLevels -date +1d >$tmp
then
    :
else
    $mailer -s "Reminders from xkal for \`head -1 $tmp\`" $rec <$tmp
fi
rm -f $tmp
xkal-automail $when $rec
END_OF_SCRIPT
