#!/bin/bash

# script to download various programs on BBC radio and make them ready for
# RockBox to play

# where script puts files
outputdir="/var/tmp/richard/iplayer-progs/"

# disk I/O niceness control
ionice="ionice -c 3"

# do required downloads, which are scheduled in get_iplayer
#/home/ra/Documents/data/ra/video/get_iplayer-2.79/get_iplayer --pvr
/home/ra/Documents/data/ra/video/get_iplayer-git20110716 --pvr

# by including the <mode> substitution in the PVR programs, we can separate out
# national radio (which is AAC) from regional radio (which is MP3), both
# packed in flash video containers. All downloads are set as raw files,
# because I don't entirely trust get_iplayer to do correct conversion
# by my lights

# files which are AAC in nature are named *.flashaacstd.flv
# files which are MP3 in nature are named *.flashaudio.flv

# MP3 files just need demuxing to a file (true streaming format)
#==================================================================
for file in ${outputdir}/*.flashaudio.flv
do
	if [[ ! -f "${file}" ]] ; then
		# not a file - probably none to process
		break;
	fi

	# for each MP3 flv file we find
	# strip extension (we have already configured get_iplayer to use nice file
	# names for it's output files)
	outfile="${file%%.flv}"

	# strip the mode information which we won't need in the output
	outfile="${outfile%%.flashaudio}"
	# add extension
	outfile="${outfile}.mp3"

	# and then the PID
	#namelen=${#outfile}
	# pid is 8 chars plus preeeding underscore
	#(( namelen = namelen - 9 ))
	#outfile="${outfile:0:${namelen}}"

	# convert it to mp3 format
	${ionice} ffmpeg -vn -i "${file}" -vn -acodec copy "${outfile}"
	ffst="${?}"
	if [[ $ffst -eq 0 ]] ; then
		# probably worked
		#echo "output is ${outfile}"
		# check file size
		dlsize=$(stat -L -c %s "${file}")
		cnvsize=$(stat -L -c %s "${outfile}")
		(( diff = dlsize - cnvsize ))
		#echo "size difference is ${diff}"
		# calculate the % size change
		outpc=$(echo "scale=6;${cnvsize}/${dlsize}*100" | bc)
		#printf "converted file is %.2f%% size of downloaded file\n" ${outpc}
		outpcint=$(printf %.0f ${outpc})
		if [[ ${outpcint} -lt 98 ]] ; then
			# less than 98% of downloaded file survived conversion - suspect
			printf "Error, only %.2f%% of downloaded file %s converted!\n" \
				${outpc} ${file}
		else
			# we are happy, take the .flv file away
			\rm "${file}"
		fi
		# finished dealing with converted file clean up
	else
		# oh dear
		echo "Error converting ${file}"
	fi
done
# end of mp3 re-packaging loop with paranoid error checking in it

# AAC files need demuxing then remuxing and possibly splitting (not true
# streaming format because there is a seek table at the start, and on long
# files it gets too big for the player)
#==================================================================
for file in ${outputdir}/*.flashaacstd.flv
do
	if [[ ! -f "${file}" ]] ; then
		# not a file - probably none to process
		break;
	fi

	# for each AAC flv file we find
	# strip extension (we have already configured get_iplayer to use nice file
	# names for it's output files)
	outfile="${file%%.flv}"

	# strip the mode information which we won't need in the output
	outfilebase="${outfile%%.flashaacstd}"
	# add extension
	outfile="${outfilebase}.mp4"
	# and create intermediate name
	intfile="${outfilebase}.aac"

	# and then the PID
	#namelen=${#outfile}
	# pid is 8 chars plus preeeding underscore
	#(( namelen = namelen - 9 ))
	#outfile="${outfile:0:${namelen}}"

	# If the file is less than 1 hour, we can just remux it into a standard 
	# MPEG4 container, and deliver. If it's more than 1 hour, we need to split
	# it up into sections to make them playable.
	# so we need to get the duration, and it's much better done on the
	# raw files than on the stripped stream
	fltduration="$(midentify "${file}" | grep ID_LENGTH | cut -d= -f2)"
	# remove decimal places
	duration=$(printf %.0f ${fltduration})

	# convert to raw AAC stream first
	${ionice} ffmpeg -vn -i "${file}" -vn -acodec copy "${intfile}"
	ffst="${?}"
	if [[ $ffst -eq 0 ]] ; then
		# probably worked
		#echo "output is ${outfile}"
		# check file size
		dlsize=$(stat -L -c %s "${file}")
		cnvsize=$(stat -L -c %s "${intfile}")
		(( diff = dlsize - cnvsize ))
		#echo "size difference is ${diff}"
		# calculate the % size change
		outpc=$(echo "scale=6;${cnvsize}/${dlsize}*100" | bc)
		#printf "converted file is %.2f%% size of downloaded file\n" ${outpc}
		outpcint=$(printf %.0f ${outpc})
		if [[ ${outpcint} -lt 96 ]] ; then
			# less than 96% of downloaded file survived conversion - suspect
			printf "Error, only %.2f%% of downloaded file %s converted!\n" \
				${outpc} ${file}
			# don't try and get any further with this file
			continue;
		else
			# we are happy, carry on, we will take the .flv file away later if
			# all goes well
			:
		fi
		# finished dealing with intermediate converted file clean up
	else
		# oh dear
		echo "Error converting ${file}"
		# go on to next file
		continue;
	fi

	# that's half the job, we have rid of the flash video file. Now we need to
	# do something with the aac file to make it playable.
	# as above, what we do depends on the duration
	# over what length do we split (and in what size pieces?)
	max_duration=7200

	echo "duration is ${duration}, max_duration is ${max_duration}"

	if [[ ${duration} -le ${max_duration} ]] ; then
		# short file, just mux it
		${ionice} MP4Box -new -hint -add "${intfile}" "${outfile}"
		ffst="${?}"
		if [[ $ffst -eq 0 ]] ; then
			# probably worked
			# check file size
			dlsize=$(stat -L -c %s "${intfile}")
			cnvsize=$(stat -L -c %s "${outfile}")
			(( diff = dlsize - cnvsize ))
			#echo "size difference is ${diff}"
			# calculate the % size change
			outpc=$(echo "scale=6;${cnvsize}/${dlsize}*100" | bc)
			#printf "converted file is %.2f%% size of demuxed file\n" ${outpc}
			outpcint=$(printf %.0f ${outpc})
			if [[ ${outpcint} -lt 100 ]] ; then
				# less than 98% of demuxed file survived conversion - suspect
				printf "Error, only %.2f%% of demuxed file %s converted!\n" \
					${outpc} ${file}
				# don't try and get any further with this file
				continue;
			else
				# we are happy, take the .aac file away
				\rm "${intfile}"
				# and the flash file
				\rm "${file}"
			fi
			# finished dealing with intermediate converted file clean up
		else
			# oh dear
			echo "Error converting ${intfile}"
			# next file
			continue;
		fi
		# end of short programs that don't need splitting
	else
		# long file, split it
		${ionice} MP4Box -new -hint -add "${intfile}"  -split ${max_duration} "${outfile}"
		ffst="${?}"
		if [[ $ffst -eq 0 ]] ; then
			# probably worked
			# check file size
			dlsize=$(stat -L -c %s "${intfile}")
			# multiple output files, but we can regex their names to get
			# a list of sizes
			splitsizes=$(stat -L -c %s ${outfilebase}_[0-9][0-9][0-9].mp4)
			cnvsize=0
			for sizev in ${splitsizes}
			do
				# for each size value, add on to total
				(( cnvsize = cnvsize + sizev ))
			done
			echo "total converted size is ${cnvsize}"
			(( diff = dlsize - cnvsize ))
			#echo "size difference is ${diff}"
			# calculate the % size change
			outpc=$(echo "scale=6;${cnvsize}/${dlsize}*100" | bc)
			printf "converted files are %.2f%% size of demuxed file\n" ${outpc}
			outpcint=$(printf %.0f ${outpc})
			if [[ ${outpcint} -lt 100 ]] ; then
				# less than 100% of demuxed file survived conversion - suspect
				printf "Error, only %.2f%% of demuxed file %s converted!\n" \
					${outpc} ${file}
				# don't try and get any further with this file
				continue;
			else
				# we are happy, take the .aac file away
				\rm "${intfile}"
				# and the flash file
				\rm "${file}"
			fi
			# finished dealing with intermediate converted file clean up
		else
			# oh dear
			echo "Error converting ${intfile}"
			continue;
		fi
		# end of long programs that need splitting
	fi
done
# end of AAC re-packaging loop with paranoid error checking in it

# now sync files out to MP3 player if possible
# (copied from sync-samung.sh)

# rsync music from mobile disk to samsung YH-925 MP3 player

# MP3 player mount point
dstmpnt="/media/mp3/"
# source disk mount point
srcmpnt="/"
# Directory to use on MP3 player (relative to mount point)
dstdir="tmp"
# Directory on source disk with music in (rel to mount point)
srcdir="${outputdir}"


# Part 1 - check devices are mounted correctly
#==============================================

mount | grep -q "${dstmpnt%/}"
dstmnt="$?"
if [[ ! $dstmnt -eq 0 ]] ; then
	# if not mounted, try to mount
	echo "Mounting MP3 player at ${dstmpnt}"
	mount "${dstmpnt}"
fi
# check again
mount | grep -q "${dstmpnt%/}"
dstmnt=$?
if [[ ! $dstmnt -eq 0 ]] ; then
	# still not mounted, error
	echo "MP3 player is not mounted at ${dstmpnt}"
	exit 1
fi

#mount | grep -q "${srcmpnt%/}"
#srcmnt=$?
#if [[ ! $srcmnt -eq 0 ]] ; then
#	# if not mounted, try to mount
#	echo "Mounting source at ${srcmpnt}"
#	mount "${srcmpnt}"
#fi
## check again
#mount | grep -q "${srcmpnt%/}"
#srcmnt=$?
#if [[ ! $srcmnt -eq 0 ]] ; then
#	# still not mounted, error
#	echo "Source disk is not mounted at ${srcmpnt}"
#	exit 1
#fi

# Part 2 - Check destination exists
#===================================
# This avoids run-away filling (or deletion) of other random media put in the
# same place

if [[ ! -d "${dstmpnt}${dstdir}" ]] ; then
	# destination directory AWOL
	echo "Destination directory ${dstmpnt}${dstdir} does not exist!"
	exit 2
fi

# Part 3 - Copy data over
#=========================
# Finally! - use rsync with FAT-friendly options to transport files to storage
# --modify-window=1 copes with FAT 2-second time stamps
# --recursive
# --times endevour to preserve times on copy, makes future syncs work better
# --progress show us what is going on
# --remove-source-files to remove what we have transfered once it is gone
# --exclude files we can't play
${ionice} rsync --modify-window=1 --recursive --times --remove-source-files --progress \
		--exclude="*.flv" --exclude="*.aac" \
		"${srcmpnt}${srcdir}/" "${dstmpnt}${dstdir}/"

