#!/bin/bash

#This is a fix for the really daft broken playlist support in rmmlite.jar, which looks in the WRONG directory for the files.
#The script looks for all .m3u playlists in the MUSIC directory, and creates copies named <NAME>.riokarma.m3u which are karma compatible.
#To use the result, simply transfer these .riokarma.m3u files (with rmmlite.jar), and all will be well. The latest (2004-09-29) version
#of rmmlite.jar with which this was tested will now find the matching ogg files, and transfer them automatically.

#NOTE This MUST be used with at least version 2004-09-28 of rmmlite.jar, available from https://rmml.dev.java.net/
#The version shipped with the karma (2004-01-15) is known NOT to work.

#TODO:
#Change the hardcoding of $MUSIC_DIR
#See also: http://www.freakysoft.de/html/libkarma/   which may do everything required, when it is updated a bit more.
#The playlist modification should IGNORE blank lines (and maybe commented lines?)


#----------------------------------------------
#Essentially the problem is this. Consider the following directory listing.
#
#	~/oggs/Bruch/ViolinMvt1.ogg
#	~/oggs/Bruch/ViolinMvt2.ogg
#	~/oggs/Bruch/ViolinMvt3.ogg
#	~/oggs/Bruch/ViolinConcerto.m3u

#The m3u file would, one might expect, contain the following. (which will make e.g. XMMS work just fine)
# 	--------------------
# 	ViolinMvt1.ogg
# 	ViolinMvt2.ogg
# 	ViolinMvt3.ogg
# 	--------------------

#BUT oh no! That's far too sane. What is actually required is this:
# 	--------------------
# 	Bruch/ViolinMvt1.ogg
# 	Bruch/ViolinMvt2.ogg
# 	Bruch/ViolinMvt3.ogg
# 	--------------------

#See here for more: https://rmml.dev.java.net/issues/show_bug.cgi?id=26
#-----------------------------------------------------

#WARNING: this will do unpredictable things if you include spaces in your directory/filenames !
MUSIC_DIR=$HOME/media/music/ogg_copy_reduced_bitrate    	# <-- where to look for playlists

echo "This will add new playlists into all the subdirectories of $MUSIC_DIR"
echo "WARNING: the behaviour of this script may be UNDEFINED if your files/directories contain spaces, or weird meta-characters."
echo "Read the source for more details"
echo -n "Continue ? (y/n): "
read REPLY
if [ "$REPLY"  != "y" ];then
	echo "Quitting"
	exit 1
fi

# Find all playlists (files ending in .m3u) except the already modified .riokarma.m3u ones.
for m3u_playlist in `find "$MUSIC_DIR" -name \*.m3u ! -name \*.riokarma.m3u` ;do

	#The new playlist we want to create.
	riom3u_playlist=`dirname "$m3u_playlist"`/`basename "$m3u_playlist" .m3u`.riokarma.m3u

	#This needs to be prepended to each line in the new playlist. (an extra / also needs to be added - see below)
	prepend=$(basename `dirname "$m3u_playlist"`)

	#Copy the original playlist, prepending the $prepend line (and an extra /)
	#Output to the new playlist. (This will overwrite any .riokarma.m3u files, which is deliberate)
	cat "$m3u_playlist" | sed s/^/"$prepend"\\//  > "$riom3u_playlist"

	#Progress report
	echo "Completed copying .m3u playlist to new .riokarma.m3u playlist by prepending directory. Respectively:"
	echo -e "\t$m3u_playlist"
	echo -e "\t$riom3u_playlist"
	echo -e "\t$prepend"
	echo ""

done

echo "Finished. Now, use rmmlite.jar to transfer 'Folder of songs' (or just the .riokarma.m3u files) to the karma. "
echo "The music files referenced will be transferred automatically."
echo "One way to do this is to make the ordinary m3u files unreadable. From the ogg directory:"
echo 'find . -name "*.m3u"  -a ! -name "*.riokarma.m3u"  | xargs chmod 000'
echo ""
echo "To undo this, do: "
echo 'find . -name "*.m3u" | xargs chmod 644'
echo "And maybe do this: but WARNING: DELETION without confirmation"
echo 'find . -name "*.riokarma.m3u" | xargs rm'

echo ""
echo "Troubleshooting? Make sure you have at least version 2004-09-28 or rmmlite.jar, from https://rmml.dev.java.net/."
echo "The version shipped with the karma (2004-01-15) is known NOT to work."
echo ""

exit 0
