#!/bin/bash
#################################################################################
# save_dvb 20080221rm (minor revision making text bold on screen)
#
# An interactive script for saving DVB broadcasts using dvbstream. Option is
# provided to save immediately or schedule to save at a later time/date using atd.
#
# It requires an accessable channels.conf as generated by DVB-Utils' scan which
# it will parse for station names, frequencies, PID's, etc, but it will not
# handle station names with whitespace, e.g. "7 Digital".
#
# Actually the names in the first field of the channels.conf can be changed to 
# anything you want.
#
# This script is written to overlook any lines in channels.conf which are commented
# out.
#
# Modify the variable "chnl_conf" below to point towards your channels.conf
#
# Option is provided to save (record) now or to schedule (to record) sometime in
# the future.
# 
# Scheduling is via atd so see manual for correct time and date usage.
#
# This script is GPL. Use as you wish and don't blame me for your problems.
#################################################################################

# The command "date" will execute if sent to the script ~/.dvb_at_file with "echo".
# So "awk" is used on the following line to create variable $timestamp in this 
# temporary file. Edit the timestamp command below to suit but do not un-comment it.
# Timestamp uses: "`date +%a-%H:%M`"
# Timestamp uses: `date +%d-%b-%R`

# Declare variables; modify to suit.
chnl_conf=~/.channels.conf  # Where channels.conf with comments (#) is kept.
save_dir=./  # Default save direcory.
sched_show=./sched_show  # Nominate the direcory and temporary script name run by at.
						 # Normally this script would be ~/.sched_show
clear  # Make some room 


get_channel()  # Function used for channels selection
{
	echo -e "\033[1m \n\tEnter the number of the station you want to record:\n" # "\033[1m" is used to make stdout bold.
    cat $chnl_conf | awk -F ":" '{ print "\t\t\t" $1 }'
    echo -e "\t\t\t\c"
	read response_0
    channel=$response_0-
    }

parse_variables()  # Assign the variables required by dvbstream from channels.conf.
{
	qam=`grep -m 1 $channel $chnl_conf | awk -F ":" '{ print $7 }' | tr -d QAM_`
	
	coder=`grep -m 1 $channel $chnl_conf | awk -F ":" '{ print $5 }' | sed -s 's/FEC_//'`
	
	guardi=`grep -m 1 $channel $chnl_conf | awk -F ":" '{ print $9 }' |  sed -e 's/GUARD_INTERVAL_1_//'`
	
	bandw=`grep -m 1 $channel $chnl_conf | awk -F ":" '{ print $4 }' | sed -e 's/BANDWIDTH_//' | sed -e 's/_MHZ//'`

	tranmod=`grep -m 1 $channel $chnl_conf | awk -F ":" '{ print $8 }' | sed -e 's/TRANSMISSION_MODE_//' | sed -e 's/K//'`

	freq=`grep -m 1 $channel $chnl_conf | awk -F ":" '{ print $2 }' | cut -c1-6` 
	
	pid=`grep -m 1 $channel $chnl_conf | awk -F ":" '{ print $11, $12 }'`
}

get_duration()  # Function used to get how long to record dvb broacast.
{
	echo -e "\n\n\tEnter how long to record for as minutes (m) or"
	echo -e "\thours (h), e.g 90m or 1.5h:" 
	echo -e "\n\t\t\t\c"
		
	read response_1
	duration=$response_1
	
}

get_name ()  # Get a name for the recorded DVB broadcast
{
	echo -e "\n\n\tEnter a name for your recording. The day and"
	echo -e "\ttime will be appended to your name:"
	 echo -e "\n\t\t\t\c"
	
	
	read response_2
	name=$response_2
}

get_dir()  # Confirm default save directory and give option to save to a different directory.
{
    echo -e "\n\n\tThe default save directory is $save_dir Hit"
    echo -e "\t\"Enter\" to save to $save_dir or enter the path"
    echo -e "\tto different directory."
    echo -e "\n\t\t\t\c" 
    
	
    read response_3

    if  [ $response_3 ]; then
       path=$response_3
    else 
       path="$save_dir"
	fi
}

display_select()  # Display recording variables and select to save now or later
{
	while true; do
		clear
		echo -e "\n\tHere are your selections:\n"
		echo -e "\tSaving from:\t`grep $channel $chnl_conf | awk -F ":" '{ print $1 }'`"
		echo -e "\tRecording to:\t$path/$name-[timestamp].mpg"
		echo -e "\tRun time:\t$duration"
	
		echo -e "\n\n\tTo schedule saving at a later time hit \"s\""
		echo -e "\n\tTo begin recording immediately hit \"n\""
		echo -e "\n\tTo quit this script and start again hit \"q\""
		echo -e "\n\t\t\t\c"
		read response_4
		echo -e "\n"
    		case $response_4 in
    			S)when="later"
    			break;;
    			s)when="later"
    			break;;
    			N)when"now"
    			break;;
    			n)when="now"
    			break;;
    			Q)when="quit"
    			break;;
    			q)when="quit"
    			break;;
    		esac
	done
}	

save_now()  # Use this function when saving immediately
{
	tput sgr0  #revert from bold to normal console display settings
	
	fuser -k /dev/dvb/adapter0/frontend0 # If frontend0 is active, kill it.

	echo -e "\ndvbstream -qam $qam -cr $coder -gi $guardi -bw $bandw -tm $tranmod -f $freq $pid -ps -o > $path/$name-$timestamp.mpg"
	echo -e "Recording $path/$name"
	dvbstream -qam $qam -cr $coder -gi $guardi -bw $bandw -tm $tranmod -f $freq $pid -ps -o > $path/$name-$timestamp.mpg 2> /dev/null &	
}

save_later()  # Use this function when scheduling to save.
{
	echo -e "\tIt is now `date +%A-%B-%R`.\n"
	echo -e "\tEnter when you want to save the DVB show. within"
	echo -e "\tthe next 24 hours as hour:minute, e.g 23:30. See"
	echo -e "\t\"man at\" for other time and format options."
	echo -e "\n\t\t\t\c" 
	
	read response_5
	save_time=$response_5
	
	# The following will make up a temporary script to be run by at.
	echo -e "#!/bin/bash" > $sched_show
	echo -e "##########################################################" >> $sched_show
	echo -e "# Created on: $(date)" >> $sched_show
	echo -e "# This temporary script created when saving dvb shows" >> $sched_show
	echo -e "# with the script dvb-save. This script is overwritten" >> $sched_show
	echo -e "# every time dvb-save is run and another dvb-t show is" >> $sched_show
	echo -e "# scheduled to be saved" >> $sched_show 
	echo -e "##########################################################n" >> $sched_show

	echo -e "\n# Declare variables" >> $sched_show
	#echo -e "exec_path=/usr/local/bin/save-dvb" >> $sched_show
	echo -e "path=$path" >> $sched_show
	echo -e "name=$name" >> $sched_show
	echo -e "duration=$duration" >> $sched_show
	grep -m 1 '# Timestamp*' /usr/local/bin/save-dvb | awk '{print "timestamp=" $4 " " $5 }' >> $sched_show
	echo -e "\n# Begin giving commands" >> $sched_show
	echo -e "saving a movie to \$path/\$name-\$timestamp.mpg" >> $sched_show

	echo "fuser -k /dev/dvb/adapter0/frontend0" >> $sched_show
	echo -e "dvbstream -qam $qam -cr $coder -gi $guardi -bw $bandw -tm $tranmod -f $freq $pid -ps -o > \$path/\$name-\$timestamp.mpg &" >> $sched_show
	echo -e "dvbstream_pid=\$!" >> $sched_show
	echo -e "sleep \$duration" >> $sched_show
	echo -e "kill \$dvbstream_pid" >> $sched_show

	chmod +x $sched_show  # Make sure the script is executable.
	
	at $save_time -f $sched_show &  # Use at to run the script at the nominated time.
}

# Begin main
tput sgr0  # Revert from bold to normal display settings.
get_channel
parse_variables
get_duration
get_name
get_dir
display_select

case $when in
	now) save_now;;
	later) save_later;;
esac

sleep 2 # This creates a delay will at returns message, otherwise script will not end on a command prompt
tput sgr0  # Revert from bold to normal display settings.
#End of Script