#!/bin/sh -e
# Checks diary entries for today and produce sound if there are any;
# Date: 2010-01-07;

DIARY_FILE=~/diary 
SOUND_FILE='/usr/share/sounds/voiceman/diary.ogg'

DAY="$(LANG=C date +%d)"
DAY="${DAY#0}"
MONTH="$(LANG=C date +%b)"
YEAR="$(LANG=C date +%Y)"
TODAY="$MONTH $DAY, $YEAR"
TODAY_REGEXP="^ *$MONTH *$DAY, *$YEAR"

print_help()
{
cat <<EOF
$(basename $0): The script to check emacs diary and produce speech notifications

Usage:
    $(basename $0) [OPTIONS]

Valid options are:
    --help - print this help and exit;
    --sound - play sound if matched diary entries found;
    --messages - say corresponding diary entries with voiceman.
EOF
}

PLAY_SOUND=0
SAY_MESSAGES=0

while [ -n "$1" ]; do
    if [ "$1" == '--help' ]; then
	print_help
	exit 0
    elif  [ "$1" == '--sound' ]; then
	PLAY_SOUND=1
    elif [ "$1" == '--messages' ]; then
	SAY_MESSAGES=1
    else
	echo "$(basename $0):Unknown command line argument: $1"
	exit 1
    fi
    shift
done


exec > /dev/null 2> /dev/null

if [ "$PLAY_SOUND" == 1 ]; then
    egrep -q "$TODAY_REGEXP" "$DIARY_FILE" && /usr/bin/ogg123 "$SOUND_FILE"
fi

if [ "$SAY_MESSAGES" == 1 ]; then
    egrep "$TODAY_REGEXP" "$DIARY_FILE" | sed s/"$TODAY_REGEXP"// |
    while read l; do
	/usr/bin/voiceman --say "$l"
    done
fi
