#!/bin/sh
# copy recording to videos
# version 0.3

# usage:
# first parameter must be %DIR%/%FILE% of the recording
# second parameter must be the desired base name of the output
# third parameter must be %CHANID% if you set USECUTLIST=Y
# fourth parameter must be %STARTTIME% if you set USECUTLIST=Y
# fifth parameter must be %JOBID% for the User Job status to be updated in MythTV
# in the mythtv setup screen invoke this script like this:
# MYTHTV User Job Command:
# /usr/bin/myth2videos "%DIR%/%FILE%" "%TITLE% - %SUBTITLE%" "%CHANID%" "%STARTTIME%" "%JOBID%"

# options:
USECUTLIST=Y		# Y or N

# where the video is stored
OUT_DIR=/data/storage/disk0/media/video


#------FUNCTIONS---------------
update_comment()
# Arg_1 = COMMENT
{
if [ $NO_JOBID -eq 0 ]; then
    `jobqueue_helper.py -j ${JOBID} -cs "${1}"`
fi
}

update_status()
# Arg_1 = status code
{
if [ $NO_JOBID -eq 0 ]; then
    `jobqueue_helper.py -j ${JOBID} -cs "${1}"`
fi
}

check_myth_jobcmds()
# check the myth database for stop pause or resume commands
{
if [ $NO_JOBID -eq 0 ]; then
    CURRENT_CMD=`jobqueue_helper.py -m "select cmds from jobqueue where id = ${JOBID}"`
    case "$CURRENT_CMD" in
	# JOB_RUN
	0) ;;
	# JOB_PAUSE
    1) `jobqueue_helper.py -j ${JOBID} -ss 6`;;
	# JOB_RESUME
    2) `jobqueue_helper.py -j ${JOBID} -ss 4`
       `jobqueue_helper.py -j ${JOBID} -cmds 0`;;
	# JOB_STOP
    4) `jobqueue_helper.py -j ${JOBID} -ss 5`
       `jobqueue_helper.py -j ${JOBID} -cmds 0`
       clean_up_files
       echo "Copy Cancelled" >> $LOGFILE
       `jobqueue_helper.py -j ${JOBID} -ss 320`
 	   exit ;;
    esac
fi
}

clean_up_files()
# clean up left over files
{
unlink $TMPFILE 2> /dev/null
unlink $TMPFILE.map 2> /dev/null
}

#-------MAIN SCRIPT------------

# check if %JOBID% is passed from command line
JOBID=${5}
if [ -z "$JOBID" ]; then
    NO_JOBID=1
else
    NO_JOBID=0
fi

# create temp filename so multiple instances won't conflict
TMPNAME=toVIDEOS-$$
TMPFILE=/data/storage/disk0/media/tmp/$TMPNAME.mpg
MENINPUTFILE=$1
TITLE=`echo $2 | sed 's/\//_/g'`

# log file location
LOGFILE=/var/log/mythtv/myth2videos.log
CDate="`date`"
echo "" >> $LOGFILE
echo $CDate >> $LOGFILE
echo "File to copy: $MENINPUTFILE     Name: $TITLE" >> $LOGFILE
echo "$2   $3   $4   $5" >> $LOGFILE

# start timer
beforetime="$(date +%s)"

check_myth_jobcmds

# check if using cutlist
if [ $USECUTLIST = Y ]; then
    MYTHCOMMFRAMES=`mythutil --getcutlist --chanid "$3" --starttime "$4" | grep 'Cutlist:' | cut -d \  -f 2`
    echo $MYTHCOMMFRAMES
    if [ -n "$MYTHCOMMFRAMES" ]; then
        echo "Extracting Cutlist..." >> $LOGFILE
        update_comment "Extracting Cutlist..."
        /usr/bin/nice -n19 /usr/bin/mythtranscode --chanid "$3" --starttime "$4" --outfile "$TMPFILE" --mpeg2 --honorcutlist
    else
        update_comment "Copying Recording..."
        cp "$MENINPUTFILE" "$TMPFILE"
    fi
elif [ $USECUTLIST = N ]; then
    update_comment "Copying Recording..."
    cp "$MENINPUTFILE" "$TMPFILE"
fi

# make output filename unique
OUTPUTFILE=$OUT_DIR/$TITLE.mpg
i=1
while [ -e "$OUTPUTFILE" ]
do
    OUTPUTFILE=$OUT_DIR/$TITLE-$i.mpg
    i=`expr $i + 1`
done

# move temp file to output location
chown mythtv "$TMPFILE" && mv "$TMPFILE" "$OUTPUTFILE"

# stop timer
aftertime="$(date +%s)"
seconds="$(expr $aftertime - $beforetime)"

ERROR=$?
if [ $ERROR -eq 0 ]; then
    echo "File Encoded Successfully: $OUTPUTFILE" >> $LOGFILE
    hours=$((seconds / 3600))
    seconds=$((seconds % 3600))
    minutes=$((seconds / 60))
    seconds=$((seconds % 60))
    echo "Encoding took $hours hour(s) $minutes minute(s) $seconds second(s) @ $current_FPS fps." >> $LOGFILE
    update_status 272
    update_comment "Encode Successful. Encoding Time: $hours hour(s) $minutes minute(s) $seconds second(s)"
else
    update_status 304
    update_comment "Encode Failed.  Exit status: $ERROR"
    echo "ERROR: $ERROR" >> $LOGFILE
fi

clean_up_files
