#!/bin/bash
# ModemMon - a really cheezy script to monitor a modem and pause the
# jukebox when the phone rings.

function usage
{
	echo "Usage: $0 <device>"
	echo "Alternately, ModemDevice may be specified in x10iod.ini file."
	exit 1
}

verbose=false
call_log=""

if [ "$1" = "-?" ]
then
	echo "Usage: $0 <device>"
elif [ "$1" = "-v" ]
then
	verbose=true
	shift
fi

if [ -f ~/x10iod.ini ]
then
	device=$(grep '^ModemDevice=' ~/x10iod.ini | cut -d= -f2)
	[ "$device" = "" ] && usage
	call_log=$(grep '^CallerLog=' ~/x10iod.ini | cut -d= -f2 |
		   awk '{print $1}')
else
	[ "$1" = "" ] && usage
	device="$1"
fi

[ "$verbose" = "true" ] && echo "Device: $device."

stty 9600 -echo cread clocal < "$device"

( sleep 3; echo "ATX7H0#cid=1" > "$device") &
while read stuff
do
	[ "$verbose" = "true" ] && echo "Serial: $stuff"
	case "$stuff" in
		RING*)
			jukebox pause ;;
		NMBR*)
			if [ "$call_log" != "" ]
			then
				(date; echo "$stuff") >> $call_log
			fi ;;
		NAME*)
			[ "$call_log" != "" ] && echo "$stuff" >> $call_log ;;
	esac
done < "$device"
