#!/bin/bash
############################################################################
#    Copyright (C) 2005 by David                                           #
#    david@dward.us                                                        #
#                                                                          #
#    This program is free software; you can redistribute it and#or modify  #
#    it under the terms of the GNU General Public License as published by  #
#    the Free Software Foundation; either version 2 of the License, or     #
#    (at your option) any later version.                                   #
#                                                                          #
#    This program is distributed in the hope that it will be useful,       #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#    GNU General Public License for more details.                          #
#                                                                          #
#    You should have received a copy of the GNU General Public License     #
#    along with this program; if not, write to the                         #
#    Free Software Foundation, Inc.,                                       #
#    59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             #
###########################################################################

# a menu driven application to output an MEncoder script to record TV.  
# Also makes an appropriate atd entry.  
# TV device parameters would need to be adjust according to users system.


function main_menu {
dialog --backtitle "Recording application" \
       --title "Main menu" \
       --menu "Make your choice.  Use arrow keys up and down to move menu selection and [ENTER] keys to select.  Use Arrows left and right to select Ok or cancel.  If you haven't enter any info but Cancel, you may get an error.  Please ignore." 20 80 6 \
        1 "Set time to start" \
        2 "Set length" \
        3 "Set file name" \
	4 "Commit" \
	5 "Exit" 2> .ans1
        output=`cat .ans1`
        rm -f .ans1

if [ "$output" -eq "1" ]
then
	set_start
elif [ "$output" = "2" ]
then
	set_length
elif [ "$output" = "3" ]
then
	set_file
elif [ "$output" = "4" ]
then
	comit
elif [ "$output" = "5" ]
then
	exit 0
else echo "Error!!!"

fi


}
# if [ "$output" = "1" ]; then

function set_start {
# rm -rf /tmp/dialog.ans
dialog --title "Set start time" \
	--backtitle "Recording application" \
	--title "Set Start Time" \
	--inputbox "Start time in 24 hours or am/pm and date in the format DD.MM.YY.  For example, 21:00 03.10.05 would set the start time to 9pm October the 3rd 2005 while 4pm 03.03.06 would be 4pm the 3rd of March next year.  Or if it going to be set for the same day then you can leave the date off and just issue the time to start, like so,  11pm" 20 70 2> .ans2
	start=`cat .ans2`
	rm -f .ans2
# 	return $output2
	main_menu
}

function set_length {
rm -rf .ans3
dialog --backtitle "Recording application" \
	--title "Set start time" \
	--title "Set Length of show" \
	--inputbox "Length in minutes like 70:00 for 70 mins" 10 50 2> .ans3
	end=`cat .ans3`
	rm -f .ans3
# 	return $output2
	main_menu
}

function set_file {
rm -rf .ans4
dialog --backtitle "Recording application" \
	--title "Set start time" \
	--title "Set Length of show" \
	--inputbox "Type file name, no spaces" 10 50 2> .ans4
	file=`cat .ans4`
	rm -f .ans4
# 	return $output2
	main_menu


}


function comit {
echo mencoder tv:// -tv driver=v4l2:input=1:width=384:height=288 -endpos $end -oac copy -ovc lavc -lavcopts vcodec=mjpeg:vbitrate=2000 -o /capture/$file.avi > $file.record
chmod 755 $file.record
at $start -f $file.record

main_menu
}

main_menu

exit 1
