#!/bin/sh
#
# reviews voice mail
#
# ari edelkind (05/08/99)
# last modified 10/17/99

# base spool directory
VMSPOOL=/var/spool/voice

# incoming voicemail directory
VMNEW=${VMSPOOL}/incoming

# default received dir
DEF_RECEIVED=../received/

# default action
DEF_ACT=n

# char to use for substitutions
schar='='

function readln () {
	echo -n "[$DEFAULT] "
	read ans || ( echo "readln failed." && exit 1 )
	[ -z "$ans" ] && ans=$DEFAULT
}

function showmsg () {
	echo -n "$msg (`ls -l $msg |awk '{print $6" "$7" "$8}'`): "
}
	
function playmsg () {
	showmsg
	trap "echo -n; trap - SIGINT" SIGINT
	rmdtopvf $msg |pvfspeed -s 8000 |pvftobasic >/dev/audio
	trap - SIGINT
	actmsg
}

function actmsg () {
	DEFAULT=$DEF_ACT
     echo -n "[a]gain, [d]elete, [m]ove, [n]ext, [q]uit? "
     readln
	if [ "$ans" = "a" ]; then
		echo "replaying $msg."
		playmsg
     elif [ "$ans" = "d" ]; then
          echo "deleting $msg."
          rm $msg
     elif [ "$ans" = "m" ]; then
		movemsg
	elif [ "$ans" = "n" ]; then
          echo "keeping $msg."
	elif [ "$ans" = "q" ]; then
		echo "exiting."
		exit 0
     fi
}

function movemsg () {
	DEFAULT=$DEF_RECEIVED
	echo ""
	echo "path is `pwd`"
	echo -n "move $msg to: "
	TRAPPED=0
	trap "trap - SIGINT; TRAPPED=1" SIGINT

	# we have to read ans in a separate shell
	# for signal traps to function properly
	echo -n "[$DEFAULT] "
	ans=`(read ans && echo $ans)`
	[ -z "$ans" ] && ans=$DEFAULT

	trap - SIGINT
	if [ $TRAPPED -eq 0 ]; then
		newans=`echo "$ans" |sed "s|^${schar}|${DEFAULT}|"`
		[ "$newans" != "$ans" ] && ans=${newans}.rmd
		echo "mv $msg $ans"
		mv $msg $ans || movemsg
	else
		echo ""
		showmsg
		actmsg
	fi
}


cd $VMNEW

msgs=`ls -t *.rmd 2>/dev/null`

[ "$msgs" = "*.rmd" ] && echo "No messages." && exit

# don't use quotes in this echo,
# so the msgs will appear next to each other.
echo reading msgs: $msgs
echo ""

for msg in $msgs; do
	playmsg
done

