#!/bin/sh
#
# neospeak: Create .mpf, .mps and .mpl files for Neo 35 MP3 player
#
# See: http://www.ssiamerica.com/products/neo35/
#      http://www.ssiamerica.com/products/neo35/speech.shtml
#      http://www.vdberg.org/~richard/neospeak.html
#
# Requires text2wave from the Festival package available at
# http://www.festvox.org/festival/index.html
#
# 24/10/2002 v1.0 by Richard van den Berg <richard@vdberg.org>
# 26/10/2002 v1.1 added "playlist" and "folder" to spoken text
#                 added -c option

TEXT2WAVE="text2wave-us1"
EXCLUDE="/(tmp|bak|backup|mp3index)"

TXT=/tmp/nsp$$.txt
RIF=/tmp/nsp$$.riff
MP3=/tmp/nsp$$.mp3

usage() {
echo "Usage: neospeak [-f] [-d] [-s] [-p] [-c] directory"
echo "         -f  force recreation of output files"
echo "         -d  create .mpf files for directories"
echo "         -s  create .mps files for songs"
echo "         -p  create .mpl files for playlists"
echo "         -c  clean up any dangling .mpf/.mps/.mpl files"
}

dir=no
song=no
pl=no
force=no
clean=no

while getopts fdspch opt ; do
	case "$opt" in
		f) force=yes ;;
		d) dir=yes ;;
		s) song=yes ;;
		p) pl=yes ;;
		c) clean=yes ;;
		h) usage; exit ;;
		*) break ;;
	esac
done

shift `expr $OPTIND - 1`

if [ "x$*" = "x" ]; then
	usage
	exit
fi

if [ $dir = "no" -a $song = "no" -a $pl = "no" -a $clean = "no" ]; then
	echo "At least one of -d , -s, -p or -c is required"
	echo
	usage
	exit
fi

[ $dir = "yes" ] && find "$*" -type d | grep / | egrep -v "$EXCLUDE" | \
	while read line ; do
		if [ ! -f "$line.mpf" -o "$force" = "yes" ]; then
			echo "Processing directory $line"
			echo "$line" | sed -e 's#.*/##g' > /tmp/nsp$$.txt
			echo "... Folder" >> /tmp/nsp$$.txt
			$TEXT2WAVE $TXT -scale 2 -o $RIF > /dev/null
			lame --quiet $RIF $MP3
			mv $MP3 "$line.mpf"
		fi
	done

[ $pl = "yes" ] && find "$*" -name \*.m3u | egrep -v "$EXCLUDE" | \
  sed -e 's#.m3u$##g' | \
	while read line ; do
		if [ ! -f "$line.mpl" -o "$force" = "yes" ]; then
			echo "Processing playlist $line"
			echo "$line" | sed -e 's#.*/##g' > /tmp/nsp$$.txt
			echo "... Playlist" >> /tmp/nsp$$.txt
			$TEXT2WAVE $TXT -scale 2 -o $RIF > /dev/null
			lame --quiet $RIF $MP3
			mv $MP3 "$line.mpl"
		fi
	done

[ $song = "yes" ] && find "$*" -name \*.mp3 | egrep -v "$EXCLUDE" | \
	sed -e 's#.mp3$##g' | \
	while read line ; do
		if [ ! -f "$line.mps" -o "$force" = "yes" ]; then
			echo "Processing song $line"
			echo "$line" | sed -e 's#.*/##g' -e 's#.* - ##' > /tmp/nsp$$.txt
			$TEXT2WAVE $TXT -scale 2 -o $RIF > /dev/null
			lame --quiet $RIF $MP3
			mv $MP3 "$line.mps"
		fi
	done

[ $clean = "yes" ] && find "$*" -name \*.mpf | sed -e 's#.mpf$##g' | \
	while read line ; do
		if [ ! -d "$line" ]; then
			echo "Cleaning up $line.mpf"
			rm -f "$line.mpf"
		fi
	done

[ $clean = "yes" ] && find "$*" -name \*.mpl | sed -e 's#.mpl$##g' | \
	while read line ; do
		if [ ! -f "$line.m3u" ]; then
			echo "Cleaning up $line.mpl"
			rm -f "$line.mpl"
		fi
	done

[ $clean = "yes" ] && find "$*" -name \*.mps | sed -e 's#.mps$##g' | \
	while read line ; do
		if [ ! -f "$line.mp3" ]; then
			echo "Cleaning up $line.mps"
			rm -f "$line.mps"
		fi
	done

rm -f $TXT $RIF $MP3
