#!/bin/sh
# Referenzzentrum fuer multimediale Teledienste (MMRZ), TU Dresden
# email: mmt-ref@tu-dresden.de 
# Christoph Fleck (17/03/99) Version 0.4
# need rtptools version 1.7 or later!
case "$1" in
     "help"|"Help"|"HELP"|"" )
	echo "record_session - recording and playing sessions with sdr"
	echo "Valid values at the 'File to save to:' inputbox:"
	echo "  filename [OPTION]"
	echo "  stop"
	echo "OPTION:"
	echo "    record_duration  [start_time]"
	echo "    play"
	echo "with: record_duration - in minutes"
	echo "      start_time - valid time syntax for at"
        echo "Examples:"
	echo "  shuttle 120 16:45jul10 starts recording for 120 minutes on july 10 at 16:45"
        echo "  shuttle 120 16:45      starts recording for 120 minutes at 16:45"
        echo "  shuttle 120            starts recording for 120 minutes immediately"
        echo "  shuttle                creates a sh-script shuttle.rec for manual start"
        echo "  shuttle play           starts play-back immediately"
        echo "  stop                   stop recording or playing immediately"
# comment next line if you don't like to get a tcl-error-box with help in sdr
	echo "Your input is wrong -\nField 'File to save to:' not one of:\n  filename [duration] [start_time]\n  filename play\n  stop" 1>&2
	exit
     ;;

     "stop"|"Stop"|"STOP" )
	while read LINE ; do
		KEY=`echo $LINE | cut -f1 -d=`
		DATA=`echo $LINE | cut -f2 -d=`
		case $KEY in
			m)	MEDIANAME=`echo $DATA | cut -f1 -d' ' `
				PORT=`echo $DATA | cut -f2 -d' ' `
				read LINE2
				KEY=`echo $LINE2 | cut -f1 -d=`   
				DATA=`echo $LINE2 | cut -f2 -d=`
				IP=`echo $DATA | cut -f3 -d' ' | cut -f1 -d/`
				PID=`/usr/bin/ps -ef | grep rtpdump | grep "$IP/$PORT" | grep -v grep | awk '{print $2}'`
				if [ ! -z "$PID" ]; then
					echo "stopping rtpdump $IP/$PORT"
					/usr/bin/kill -INT ${PID}

				fi
				PID=`/usr/bin/ps -ef | grep rtpplay | grep "$IP/$PORT" | grep -v grep | awk '{print $2}'`
				if [ ! -z "$PID" ]; then
					echo "stopping rtpplay $IP/$PORT"
					/usr/bin/kill ${PID}
				fi	
				;;
		esac
	done
	echo "stop executed"
	exit
     ;;
esac	
case "$2" in
     "play"|"Play"|"PLAY" )
	#echo play
	fname=$1
	index=0
	ALLMEDIA=""
	while [ -f $fname.play ] ; do
#		echo "vorhanden: $fname.play"
		index=`expr $index + 1`
		fname=$1"_$index"  
	done

	while read LINE ; do
		KEY=`echo $LINE | cut -f1 -d=`
		DATA=`echo $LINE | cut -f2 -d=`
		#echo KEY=$KEY DATA=$DATA
		case $KEY in
			m)	MEDIANAME=`echo $DATA | cut -f1 -d' ' `
				for i in $ALLMEDIA 
				do
					if [ $i = $MEDIANAME ] ; then
						MEDIANAME=${MEDIANAME}I
					fi
				done
				ALLMEDIA="$ALLMEDIA $MEDIANAME"
				#echo MEDIANAME=$MEDIANAME ALLMEDIA=$ALLMEDIA
				PORT=`echo $DATA | cut -f2 -d' ' `
				#echo MEDIANAME=$MEDIANAME PORT=$PORT
				read LINE2
				KEY=`echo $LINE2 | cut -f1 -d=`   
				DATA=`echo $LINE2 | cut -f2 -d=`
				IP=`echo $DATA | cut -f3 -d' ' | cut -f1 -d/`
				TTL=`echo $DATA | cut -f3 -d' ' | cut -f2 -d/`
				#echo IP=$IP/$TTL
				if [ -f $1.$MEDIANAME ]; then
					#echo "starting rtpplay  $IP/$PORT/$TTL < $1.$MEDIANAME &"
					echo "rtpplay $IP/$PORT/$TTL < $1.$MEDIANAME &" >> $fname.play
				fi
				;;
		esac
	done
	if [ -r $fname.play ]; then
		chmod a+x $fname.play
		echo "starting $fname.play"
		$fname.play
	else
		echo "No Matching Media Files Found!"
	fi
	exit
     ;;

     * )
	if [ -f $1.rec ] ; then
		rm  $1.rec
	fi
	echo "#!/bin/sh" > $1.rec
	echo "#USAGE: $1.rec [record_duration]" >> $1.rec
	ALLMEDIA=""
	while read LINE ; do
		KEY=`echo $LINE | cut -f1 -d=`
		DATA=`echo $LINE | cut -f2 -d=`
		#echo KEY=$KEY DATA=$DATA
		case $KEY in
			m)	MEDIANAME=`echo $DATA | cut -f1 -d' ' `
				for i in $ALLMEDIA 
				do
					if [ $i = $MEDIANAME ] ; then
						MEDIANAME=${MEDIANAME}I
					fi
				done
				ALLMEDIA="$ALLMEDIA $MEDIANAME"
				#echo MEDIANAME=$MEDIANAME ALLMEDIA=$ALLMEDIA
				PORT=`echo $DATA | cut -f2 -d' ' `
				#echo MEDIANAME=$MEDIANAME PORT=$PORT
				read LINE2
				KEY=`echo $LINE2 | cut -f1 -d=`   
				DATA=`echo $LINE2 | cut -f2 -d=`
				IP=`echo $DATA | cut -f3 -d' ' | cut -f1 -d/`
				#echo IP=$IP
				#echo "starting rtpdump -Fdump -t \$1 $IP/$PORT > $1.$MEDIANAME &"
				echo "rtpdump -Fdump -t \$1 $IP/$PORT > $1.$MEDIANAME &" >> $1.rec
				;;
		esac
	done
	chmod a+x $1.rec
	echo "$1.rec written."
	if [ $2 ] && [ $2 -gt 0 -a $2 -lt 525600 ] ; then
		if [ $3 ] ; then
			echo "$1.rec $2" | at -s $3 1> /dev/null 2>&1
			if [ $? != "0" ] ; then
			   echo "Error: AT Command \"at $3 1.rec $2\" was unsuccessful!"
			else
			   echo "Starting $1.rec At $3"
			fi
			exit
		else
			echo "starting $1.rec now"
			$1.rec $2
			exit
		fi
	fi
     ;;
esac