#!/bin/sh
#
# mailnotify.sh - (C) 2002 Malte J. Wetz <mail@malte-wetz.de>
#
# This script uses fetchmail to count new (unseen) mail on your
# mail server and then reports the result using a TTS-engine
# (currently via the script 'speak' from the same author).
#
# Script is compatible with gkrellm's checkmail command,
# but should be better used as the notification program.
#

######################################################################
## Change these values to meet your requirements
######################################################################

# what command to execute before and after TTS?
COMMAND_PRE_TTS="play /usr/share/sounds/wav/im-mailnotify.wav"
COMMAND_POST_TTS=""

# what message to synthesize via TTS if new mail is waiting?
# note that we distinguish between singular and plural messages.
# a tribute to the german language :-/
MESSAGE_SINGULAR="Sie haben eine neue Nachricht!"
MESSAGE_PLURAL="Sie haben %n neue Nachrichten!"

# what command to use for checking for mail?
# NOTE: fetchmail-like output required
MAILCHECK_COMMAND="/usr/bin/fetchmail -c -f ${HOME}/.fetchmailrc"

# where can I find the 'speak' script?
SPEAK=/usr/local/bin/speak

# auto-detect some required GNU-Tools, usually no need to override
SED=`which sed`
AWK=`which awk`

# where can I store some temp stuff?
TMPFILE=/tmp/mailnotify.$$

######################################################################
## There should be nothing to change below this point
######################################################################

BASENAME=`basename $0`
VERSION="0.0.3"

# evaluate some command line parameters
while getopts rh switch; do
  case "$switch" in
    h) cat<<EOF
$BASENAME Version .$VERSION (C) 2002 Malte J. Wetz <mail@malte-wetz.de>

This script uses fetchmail to count new (unseen) mail on your
mail server and then reports the result using a TTS-engine
(currently via the script 'speak' from the same author).

Usage:
  $BASENAME [-r] [-h]

Options:
  -h    Print this screen and exit.
  -r    Re-Print fetchmail s check result to console so that other programs
        (like gkrellm) can handle it.
EOF
    ;;
    r) REPORT=yes	;;
  esac
done

# execute the check-mail command
new_messages=0
$MAILCHECK_COMMAND | while read result; do

  # parse output and get message statistics
  msg_total=`echo "$result" | $AWK -F ' ' '{print $1}'`
  msg_seen=`echo "$result" | $AWK 'gsub(/\(|\)/,"")' | $AWK '{print $3}'`
  msg_new=`expr $msg_total - $msg_seen`
  new_messages=`expr $new_messages + $msg_new`
  
  [ -z "$REPORT" ] || echo "$result"
  
  echo $new_messages > $TMPFILE
done

read new_messages < $TMPFILE

# if there is new mail, do your job!
if [ "$new_messages" -gt 0 ]; then

  # execute the pre-TTS command
  [ -z "$COMMAND_PRE_TTS" ] || $COMMAND_PRE_TTS

  # say it!
  if [ "$new_messages" -gt 1 ]; then
    say_this=`echo "$MESSAGE_PLURAL" | $SED -e "s/%n/$new_messages/"`
  else
    say_this=`echo "$MESSAGE_SINGULAR" | $SED -e "s/%n/$new_messages/"`
  fi
  echo "$say_this" | $SPEAK -a -n -p -l 60

  # execute the post-TTS command
  [ -z "$COMMAND_POST_TTS" ] || $COMMAND_POST_TTS
fi


