#!/bin/bash
## This is the third, probably final, iteration for the JB Live Stream recorder.
## It is going to use the UTC time stamp so you don't have to use more than one loop, for now, to record a single show.
## You will need to make sure that you have a downloads/jupiter_broadcasting folder in your home folder, or edit the script to write the files to where you want.
## If you don't want a particular show recorded just comment out that section.
## This script has no license and is should be used with care as a single recording can produce a file over 1GB.

lastrt=1630 ## *trt is the start time(utc) of a show. Default is 30 mins prior.
lastop=1830 ## *top is the stop time(utc) of a show. Default varies but judging from *trt you can see this will record 1.5 hours after the regular start time of LAS
codtrt=1530  ## Coder Radio start time
codtop=1730  ## Coder Radio stop time
scitrt=200  ## SciByte start time
scitop=400  ## SciByte stop time
nrstrt=230  ## FauxShow start time
nrstop=430  ## FauxShow stop time
tsntrt=1930  ## TechSNAP start time
tsntop=2130  ## TechSNAP stop time
unftrt=130  ## UnFilter start time
unftop=400  ## UnFilter stop time

## This is the main function, the 'meat' of this script.
bacon(){
        echo "Now recording $1$2"
	zm=`expr 60 \* \( \( \( $3 / 100 \) \- \( $4 / 100 \) \) \- 1 \)`
	zn=`expr 60 \+ \( \( $3 % 100 \) \- \( $4 \% 100 \) \)`
	mh=`expr \( $zm \+ $zn \) \/ 60`
	mm=`expr \( $zm \+ $zn \) \% 60`
	`mencoder -really-quiet http://videocdn-us.geocdn.scaleengine.net/jblive-iphone/live/jblive.stream/playlist.m3u8 -oac mp3lame -ovc lavc -lavcopts vcodec=msmpeg4 -ofps 30 -endpos 0$mh:$mm:00 -o ~/downloads/jupiter_broadcasting/$1\-$5.mp4`
	killall mencoder
	sleep 5
}

tcalc(){
s=`date -u +%H%M`       ## Grab time variable with no place padding.
t=`expr $s / 1`         ## Expr to drop the 0 off time variable for times less than 1000.
}

while true

do

tcalc	## Time calculation
mh=0    ## Mencoder hour calc set.
mm=0    ## Mencoder minute calc set.
d=`date -u +%w`         ## Date veriable 0=Sunday 3=Wednesday.
ot=`date +%c`           ## Time and date stamp for the script's nothing to record output.

        while [ $t -ge $lastrt ] && [ $t -lt $lastop ] && [ $d -eq 0 ]; do
                clear
		bacon LAS '.' $lastop $t `date +%d%b%Y-%H%M%S`
		tcalc
		clear
        done

	while [ $t -ge $codtrt ] && [ $t -lt $codtop ] && [ $d -eq 1 ]; do
		clear
		bacon Coder 'Radio.' $codtop $t `date +%d%b%Y-%H%M%S`
		tcalc
		clear
	done

        while [ $t -ge $scitrt ] && [ $t -lt $scitop ] && [ $d -eq 3 ]; do
		clear
		bacon SciByte '.' $scitop $t `date +%d%b%Y-%H%M%S`
		tcalc
		clear
	done

        while [ $t -ge $nrstrt ] && [ $t -lt $nrstop ] && [ $d -eq 4 ]; do
		clear
		bacon Faux 'Sho.......ITS NOT A REAL SHOW!!!!!' $nrstop $t `date +%d%b%Y-%H%M%S`
		tcalc
		clear
	done

	while [ $t -ge $tsntrt ] && [ $t -lt $tsntop ] && [ $d -eq 4 ]; do
		clear
		bacon TechSNAP '.' $tsntop $t `date +%d%b%Y-%H%M%S`
		tcalc
		clear
	done

	while [ $t -ge $unftrt ] && [ $t -lt $unftop ] && [ $d -eq 5 ]; do
		clear
		bacon UnFilter '.' $unftop $t `date +%d%b%Y-%H%M%S`
		tcalc
		clear
	done

        killall mencoder
        clear
        echo "Nothing to record. $ot"
        sleep 900	# 15 minutes

done


syntax highlighted by Code2HTML, v. 0.9.1