#!/bin/bash
################################################################################
# /usr/local/bin/stream-tv
#
# Uses dialog for interface sonce it is run on server without X.
#
# Uses dvbstream to broadcast a dvb-t program on lan for a pre-set period of time.
# 
# Requires a /usr/local/etc/stream-dvb.conf edited for my location
#
# Requires dumprtp on client. Usage is "dumprtp > [filename].mpg" and view with xine, etc.
#
# Check out companion script watch-tv which uses dumrtp to do the above plus more.
#
# This script will generate a 2nd script ./stream-prog, chmod +x the script
# and then execute it using nohup [command] &
# This will allow the console and ssh session on the server to be closed/terminated
# without terminating the dvbstream broadcast session.

# Create a variable for location of config file.
config=./stream-dvb.conf

# A temporary file is needed to set variables when using dialog
TEMP=answer

# Get information from the config file
. $config

################################################################################
# Check to see if /dev/dvb/adapter0/dvr0 is busy. Can be expanded for additional devices
################################################################################
check_device()
{
	if [ "`lsof /dev/dvb/adapter0/dvr0`" != "" ]; then
		dialog --title 'Stream-TV' --msgbox 'The TV device is in use. Try again later'  6 28   	
		exit
	fi
}

################################################################################
# Get the channel
################################################################################
get_channel() 
{
	dialog --title "Stream-TV" --radiolist "Select a station:" 12 30 6 \
    	0 "SBS" 'on_off' \
    	1 "ABC" 'on_off' \
    	2 "ABC2" 'on_off' \
    	7 "Seven Digital" 'on_off' \
    	9 "Nine Digital" 'on_off' \
   	10 "Ten Digital" 'on_off' 2> $TEMP

   response_0=`cat $TEMP`
	case $response_0 in
    	    0) channel=$station_0
			name=$name_0;;
			1) channel=$station_1
			name=$name_1;;
        	2) channel=$station_2
			name=$name_2;;
			7) channel=$station_7
			name=$name_7;;
			9) channel=$station_9
			name=$name_9;;
			10) channel=$station_10
			name=$name_10;;
			*) dialog --title "Stream-TV" \
			--msgbox "You did not nominate a station. Try again." 6 25
			clear
			exit;;
	esac
}

################################################################################
# Get duration of show
################################################################################
get_duration()
{
	dialog --title "Stream-TV" --inputbox "Enter the time in minutes to stream the program:" 8 30 2>$TEMP
	duration=`cat $TEMP`
}

###################
# Confirm streaming settings #
##################
confirm_settings()
{
	dialog --title "Stream_TV" --yesno \
	"$name has been selected for $duration minutes of viewing\n\nDo you want to proceed?" 8 35

	if [ $? -eq 1 ];then
  		exit
	else
		sched_show=yes
	fi
}

################################################################################
# Create a script named /home/~/stream-prog to be run by "at" #
################################################################################
stream ()
{
if [ "$sched_show" = "yes" ]; then
	echo -e "#!/bin/bash" > stream-prog
	echo -e "dvbstream $channel &" >> stream-prog
	echo -e "dvdstream_pid=\$!" >> stream-prog
	echo -e "sleep "$duration"m" >> stream-prog
	echo -e "kill \$dvdstream_pid" >> stream-prog
fi
}

#################################################################################
# Clean up 
#################################################################################
clean_up() 
{
	clear
	rm -f $TEMP
	rm -f nohup.out
}

#################################################################################
# Final message and exit
################################################################################# 
final_msg()
{
	dialog --infobox "Proceeding to stream channel\n\nYou may close this terminal now." 5 36 ; sleep 6
	clear
	exit
}

################################################################################
# Begin the main part of script
################################################################################
check_device
get_channel
get_duration
confirm_settings
stream
chmod +x stream-prog
nohup ./stream-prog &
clean_up
final_msg

#End of script
