#!/bin/bash

# PURPOSE: Script to convert MythTV Recordings into a different format
#
# AUTHOR: feedback[AT]HaveTheKnowHow[DOT]com

# Expects three arguments:
# NOTE: These variables are populated automatically by the MythTV backend
#    1. %FILE%      -> this is the file to convert
#    2. Main folder -> destination main folder
#    3. Sub-folder  -> destination sub folder

#example: ConvertAndMove.sh "%FILE%" "%RECGROUP%" "%TITLE%"

SourceFilename=$1
MainFolder=$2
SubFolder=$3

#This is where the MythTV Recordings live
SourceFilePath="/media/ST3750640NS/Myth_RawTV/"

#This is where the converted recording will end up
OutputFilePath="/media/ST3750640NS/TV Shows/$MainFolder/$SubFolder"

#First check whether file we want to convert actually exists
if [ -r "$SourceFilePath$SourceFilename" ]
then
  #file exists so let's go for it

  #Create the destination folder(s) if required
   if [ ! -d "$OutputFilePath" ]
   then 
     echo 'Path' $OutputFilePath 'does not exist so creating it'
     mkdir -p "$OutputFilePath"
   fi

   nuvexport-xvid --nice 19 --input="$SourceFilename" --path="$OutputFilePath" >/dev/null

else 
  echo "ERROR: Input File " "$SourceFilePath$SourceFilename" "does not exist"
  exit 2
fi

exit 0