#!/bin/bash
# rbdirtalk - A script to prepare a folder for rockbox spoken folder names.
# For more information about rockbox see their website at:
# http://www.rockbox.org
# This is GPL software, licensed under GPL version 2.
# This script is written by Julien Claassen

# General variables
PROGRAM=`basename $0`
VERSION=1.0

# The default text-to-speech engine
TTS=espeak
# The default voice for espeak
ESPEAK_VOICE="en-rp"
# The default voice for festival
FESTIVAL_VOICE="voice_rab_diphone"
# More internal variables
PID="$$"
OUTPUT="rbspeech-${PID}.list"
I=1
J=0

# Basic commandline parsing - do we have no parameter?
if [[ -z "$1" ]]; then
	echo "${PROGRAM} Version ${VERSION}"
	echo "You must specify a folder to prepare for your rockbox."
	echo "For more information see ${PROGRAM} --help"
	exit 0;
fi

# Parsing of parameter one
case "$1" in
	-h|--help)
		echo "${PROGRAM} ${VERSION}"
		echo "Prepare folders for Rockbox to be spoken."
		echo "To run ${PROGRAM} you need espeak or festival and the Rockbox rbspeexenc."
		echo "SYNTAX: ${PROGRAM} < FOLDERNAME | --help > [ -e | -f | -c ] [ voice ]"
		echo -e "\n--help - Display this help."
		echo "-c - Clean up the folder to render all new speech files."
		echo "-f - Use Festival TTS to render the speech output."
		echo "-e - Use espeak to render the speech output (default)"
		echo "voice - name of a festival or espeak voice."
		echo -e "\t defaults are: en-rp for espeak and voice_rab_diphone for festival."
		echo "FOLDERNAME - The name of the parent directory of all your music"
		echo -e "\tand audio folders."
		echo -e "\nExample:"
		echo -e "\t${PROGRAM} audiobooks"
		echo -e "\tWill prepare all subfolders of audiobooks to be announced."
		exit 0;
		;;
esac

# Parameter one is no help, so it must be a folder. Does it exist?
if [[ ! -e "$1" ]]; then
	echo "Couldn't find folder $1 ."
	echo "Please check your spelling."
	echo "Exiting..."
	exit 1;
fi

# Parsing of further parameters.
if [[ -n "$2" ]]; then
	case "$2" in
		# Clean up all _dirname.talk files in all subfolders.
		-c)
			echo "Cleaning up folder $1 ."
			find "${1}" -name _dirname.talk &> ${OUTPUT}
			ENTRIES=`wc -l ${OUTPUT} | awk '{ print $1 }'`
			while [[ $I -le $ENTRIES ]]; do
				REALFILE=`sed -n ${I}p ${OUTPUT}`
				echo "Removing ${REALFILE} ..."
				rm "${REALFILE}"
				let I=I+1;
			done
			rm ${OUTPUT} &> /dev/null
			let I=I-1
			echo "Cleaned up $I folders, done!"
			exit 0;
			;;
		# Set the text-to-speech engine and optionally the used voice.
		-e)
			echo "Using espeak explicitly."
			TTS="espeak"
			if [[ -n "$3" ]]; then
				ESPEAK_VOICE="$3";
			fi;
			;;
		-f)
			echo "Using festival."
			TTS="festival"
			if [[ -n "$3" ]]; then
				FESTIVAL_VOICE="$3";
			fi;
			;;
		*)
			echo "Unrecognised option $2 ."
			echo "continuing normally..."
			;;
	esac;
fi

# Normal operations. Find all subfolders.
ls -Rp -I . -I .. $1 | grep / | grep ":$" > ${OUTPUT}
ENTRIES=`wc -l ${OUTPUT} | awk '{ print $1 }'`
# Prepare each found folder to be spoken (by replacing _ and - by spaces).
while [[ $I -le $ENTRIES ]] ; do
	REALDIR=`sed -n ${I}p ${OUTPUT} | awk '{ len = length($0); $realdir = substr($myname,0,(len-1)); printf("%s",$realdir); }'`
	if [[ ! -e "${REALDIR}/_dirname.talk" ]]; then
	  LASTDIR=`echo "${REALDIR}" | awk '{ $num = split($0,myarr,"/"); print myarr[$num] }'`
	  TALKDIR=`echo "${LASTDIR}" | sed -e "+s/_/ /g; +s/-/ /g"`
	  if [[ -n "${TALKDIR}" ]]; then
	    echo "Rendering ${TALKDIR} ..."
		 case "${TTS}" in
		   espeak)
	        echo "${TALKDIR}" | espeak --stdin -w out-${PID}.wav -v ${ESPEAK_VOICE};
			  ;;
			festival)
			  echo "${TALKDIR}" | text2wave -eval "(${FESTIVAL_VOICE})" -o out-${PID}.wav;
			  ;;
	    esac
	    rbspeexenc out-${PID}.wav "${REALDIR}/_dirname.talk"
	    let J=J+1;
	  fi;
	else
	  echo "Omitting ${REALDIR}...";
	fi
	let I=I+1;
done

# Final cleanup.
rm ${OUTPUT} out-${PID}.wav &>/dev/null
echo "Rendered $J foldernames, done!"
