#!/bin/sh   
# Copyright Ilkka Tuohela <hile@iki.fi>, 2005.
# This script is in public domain, i.e. do whatever you want
# with it.
# Example script to create a m3u file with relative pathnames. 
#function shuffle () {
#cat "${@}"|grep -v "^#" | \
#while read mp3 ; do
#echo "$RANDOM $mp3"
#done | sort | cut -d" " -f2-
#}

IFS="
"
pl="$(zenity --file-selection --save).m3u"


if [ -r "$pl" ]
then
    if ! $(zenity --question --text "File $pl exists, append playlist?") 
    then
        exit 0
    fi
fi
if ! $(touch $pl) 
then
    zenity --error --text "Can't write to file $pl"    
    exit 0
fi

remove_prefix() {
    echo "$1" | sed -e 's/.*MUSIC/MUSIC/g' 
}

for f in $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS 
do
    if [ -f "$f" ]
    then
        remove_prefix "$f" >> ${pl}
    fi
    if [ -d "$f" ]
    then
        for n in $(find "$f" -name '*.ogg' -o -name '*.mp3')
        do  
            remove_prefix "$n" >> ${pl}
        done
    fi
    
done
