#! /bin/sh
#
export log="/tmp/recording.log"
#export log="/dev/null"
export RecordAction=$1
export RecordFolder=$2

echo "">>$log
echo "">>$log
echo `date +"%Y-%m-%d %T"` " - $0 started">>$log
echo "Parameter 1 - $1">>$log
echo "Parameter 2 - $2">>$log
echo "">>$log

#. /etc/vdr/cutdb.conf
export TimerFile="/usr/local/src/VDRtmp/timers.conf"

# check for valid setup
if [ "$TimerFile" == "" ] ; then
	cutdb=no
fi
echo "$TimerFile">>$log

case "$1" in
"before")
	recinfo $1 $2
	export TimerRaw=`echo $RecordFolder|awk -F/ '{print $(NF-1)}'`
	echo "TimerRaw		=$TimerRaw">>$log
	export TimerStr=`echo $TimerRaw|sed 's/#2F/\//g'|sed 's/_/ /g'`
	echo "TimerStr		=$TimerStr">>$log
	export TimerDay=`echo $RecordFolder|awk -F/ '{print substr($(NF),9,2)}'`
	echo "TimerDay		=$TimerDay">>$log
	export TimerDate=`echo $RecordFolder|awk -F/ '{print substr($(NF),1,10)}'`
	echo "TimerDate		=$TimerDate">>$log
	export TimerStart=`echo $RecordFolder|awk -F/ '{print substr($(NF),12,2) substr($(NF),15,2)}'`
	echo "TimerStart		=$TimerStart">>$log


	export ChannelID=`cat $TimerFile|grep -e "^[^:]*:[^:]*:$TimerDay:$TimerStart:[^:]*:[^:]*:[^:]*:[^:]*$TimerStr:.*$"|awk -F: '{print $2}'`
	echo "ChannelID		=$ChannelID">>$log
	export TimerEnd=`cat $TimerFile|grep -e "^[^:]*:[^:]*:$TimerDay:$TimerStart:[^:]*:[^:]*:[^:]*:[^:]*$TimerStr:.*$"|awk -F: '{print $5}'`
	echo "TimerEnd		=$TimerEnd">>$log

	# write to files
	echo "$TimerDate">$1/cutdb.TimerDate.vdr
	echo "$TimerStart">$1/cutdb.TimerStart.vdr
	echo "$TimerEnd">$1/cutdb.TimerEnd.vdr
	echo "$ChannelID">$1/cutdb.ChannelID.vdr
	;;
"after")
	recinfo $1 $2
	#start noad?
	;;
"edited")
	recinfo $1 $2
	echo "ed"
	OldFolder=`echo $RecordFolder|awk -F/ '{ ORS = "" 
						POS=1 
						while ( POS <= NF )
							{ print"/"
							 if ( POS == (NF-1)) {print substr($(NF-1),2)} else {print $(POS)}
						POS++ }  }'`
	echo $OldFolder>>$log

	cp $OldFolder/sharemarks.conf $RecordFolder
	cp $OldFolder/ptsmarks.vdr $RecordFolder
	#check if new or retrived marks
	#if new send to cutdb
	;;
esac





echo `date +"%Y-%m-%d %T"` " - $0 ended">>$log
