#!/bin/bash

###############################################################
#
# The MIT License
#
# Copyright (c) 2006 Olof Holmgren
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
###############################################################

# Settings:
DESTDIR=/home/offe/lagring/film/tv/


if [ "$1" == "-h" ] || [ "$1" == "--help" ]
    then
    echo "If you specify an output directory when calling me,"
    echo "the recorded files will end up in that directory."
    echo "If you don't, the files will end up in the default"
    echo "directory (currently $DESTDIR)"
    exit 1
fi

if [ $# -eq 1 ]
    then
    DESTDIR=$1
fi


echo ">>"
echo ">> Welcome to TVrecord v1.0"	
echo ">>"
echo ">> Files will end up in $DESTDIR"
echo ">>"

abort=0
while [ $abort -ne 1 ]
do
    echo ">> Which channel should I record from? [svt1, svt2, tv3, tv4, kanal5, eurosport, ztv, mtv, discovery, tv4+, svt24]:"

    read CHANNELNAME
    abort=1

    if [[ ("$CHANNELNAME" == "svt1") ]]
	then
	CHANNEL=E6
    elif [[ ("$CHANNELNAME" == "svt2") ]]
	then
	CHANNEL=E8
    elif [[ ("$CHANNELNAME" == "tv3") ]]
	then
	CHANNEL=E10
    elif [[ ("$CHANNELNAME" == "tv4") ]]
	then
	CHANNEL=E9
    elif [[ ("$CHANNELNAME" == "kanal5") ]]
	then
	CHANNEL=E11
    elif [[ ("$CHANNELNAME" == "eurosport") ]]
	then
	CHANNEL=S11
    elif [[ ("$CHANNELNAME" == "ztv") ]]
	then
	CHANNEL=S14
    elif [[ ("$CHANNELNAME" == "mtv") ]]
	then
	CHANNEL=S13
    elif [[ ("$CHANNELNAME" == "discovery") ]]
	then
	CHANNEL=S36
    elif [[ ("$CHANNELNAME" == "tv4+") ]]
	then
	CHANNEL=U57
    elif [[ ("$CHANNELNAME" == "svt24") ]]
	then
	CHANNEL=S12
    else
	abort=0
    fi
done

echo ">> What day and time should I start? [hh:mm <mmddyy OR month-name day>]:"
read STARTTIME

echo ">> For how long should I record? (in minutes):"
read STOPTIME


################################################################
# Coding options for libavcodec: (-lavcopts)
#
# Fast                       vcodec=mpeg4:mbd=2:trell:v4mv:turbo
# Realtime                   vcodec=mpeg4:mbd=2:turbo
# for interlaced material    ildct:ilme:mbd=2
#
# adevice=hw.0 means that it uses the first sound device = first soundcard
#################################################################

###############################################################
# IMPORTANT
#
# For sound to work, remember to capture from the appropriate
# channel (probably line in, done for example from alsamixer)
#
# With these MEncoder settings, TVrecord can capture video
# in realtime on a P4 1,6 GHz with 768 Mb RAM.
#
# File size is ~450 Mb/h
#
###############################################################

DATETIME=`date +%F_%T`
 
echo "mencoder -tv driver=v4l2:device=/dev/video0:adevice=hw.0:alsa:chanlist=europe-west:norm=PAL:input=0:width=400:height=300:audiorate=48000 tv://$CHANNEL -of avi -lavcopts vcodec=mpeg4:turbo:ildct:ilme:mbd=2:acodec=ac3:abitrate=192 -ofps 25 -oac lavc -ovc lavc -endpos 00:$STOPTIME:00 -o $DESTDIR/$CHANNELNAME-$DATETIME.mpg" > /tmp/TVrecord.tmp

if ! at -f /tmp/TVrecord.tmp $STARTTIME
    then
    echo ">> ERROR: Couldn't add job to 'at' demon."
    exit 1
else
    echo ">> Job added to 'at' daemon"
fi

rm /tmp/TVrecord.tmp

exit