#!/bin/sh
# mkpls - Create an audio playlist of files based on MIME type.
#
# Note: You must associate FILEEXT with your prefered application in GNOME.
#    Examples: .vpls opens with "totem", "xine -P", "mplayer -playlist"
#              .m3u opens with "xmms", "totem", "rhythmbox"
#
# Copyright 2004-2005 David L Norris <dave@webaugur.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.


# File types to find
MIMETYPE="audio/*"
# Playlist File Extension
FILEEXT="m3u"


# Always answer OK when asked to create playlist on Desktop?
ALWAYSDESK=0

################### DOOMED BE THOSE WHO EDIT BELOW HERE ##################

# We really should check all the arguments and handle files by looking at their parent.
if [ ! -d "$1" ]; then
    echo "usage: mkpls <folder 1> <folder 2> ...";
    exit
fi

# Run the entire script on every argument (FIXME: Yes, this should be a function.)
for DIRINLIST in ${*}; do
    # First, make sure this is a directory.  If not skip it.
	if [ ! -d "$DIRINLIST" ]; then
		continue
	fi 

	# Strip trailing / so we don't write hidden files.
	ROOTDIR="`dirname "${DIRINLIST}"`/`basename "${DIRINLIST}"`"
	PARENTDIR=`dirname ${ROOTDIR}`
	PLAYLIST="${ROOTDIR}.${FILEEXT}"
	
	# Create a temp playlist file or present an error.
	TMPFILE=`mktemp "${PARENTDIR}/.playlist.XXXXXXXXXX" 2> /dev/null`
	
	# Make sure we created a file.
	if [ ! -f "$TMPFILE" ]; then
		if [ $ALWAYSDESK = 0 ]; then
			zenity --question --text="A playlist cannot be created in the `basename ${PARENTDIR}` folder because you do not have permission.  Would you like to create the playlist on your desktop?" --title="Create Playlist on Desktop?"
			ANSWER=$?
		else
			ANSWER=0
		fi
	
	   # OK = 0
	   if [ $ANSWER = 0 ]; then
	#       echo "Answered OK, continue"
		   # Is the Desktop the Home Dir?
		   DESKHOME=`gconftool -g "/apps/nautilus/preferences/desktop_is_home_dir"`
		   if [ "x${DESKHOME}" = "xtrue" ]; then
			   TMPFILE=`mktemp "${HOME}/.playlist.XXXXXXXXXX" 2> /dev/null`
			   PLAYLIST="${HOME}/`basename ${ROOTDIR}`.${FILEEXT}"
	#           echo "HOME is Desktop"
		   else
			   TMPFILE=`mktemp "${HOME}/Desktop/.playlist.XXXXXXXXXX"`
			   PLAYLIST="${HOME}/Desktop/`basename ${ROOTDIR}`.${FILEEXT}"
	#           echo "HOME is not Desktop"
		   fi
	
		   # We can't write to the HOME dir.  Something is insane.
		   if [ ! -f "$TMPFILE" ]; then
			   zenity --info --text="The temporary playlist ${TMPFILE} cannot be created.  You don't seem to have permission in your own HOME.  I give up trying to create a playlist." --title="Error writing to HOME."
			   exit
		   fi
	
	   # Cancel = 1
	   else
	#       echo "Answered Cancel, exiting"
		   exit
	   fi
	fi
	
	# Field seperation on newline only
	OLDIFS=$IFS
	IFS="
	"
	
	# Found file counter init.
	FOUND=0
	
		# Empty file contents to be double-paranoid.
		echo > "${TMPFILE}"
	
		# Find all files of type MIMETYPE
		for i in `find ${ROOTDIR} -type f | sort`;
		do
		   # If the file is not really a video don't bother
		   ISMIME=1
		   file -bi "${i}" | grep "^${MIMETYPE}" > /dev/null
		   ISMIME=$?
		   # File does not contain MIMETYPE
		   if [ $ISMIME != 0 ]; then
			   continue;
		   fi
		   
		   # Keep count of found files
		   FOUND=`echo ${FOUND} + 1 | bc`
	
		   # Write filename to temp playlist
		   echo "${i}" >> "${TMPFILE}"
	
		done
	
	# Temp playlist exists
	if [ -f "${TMPFILE}" ]; then
	
		# No files of type MIMETYPE were found.
		if [ ${FOUND} -lt 1 ]; then
			gdialog --infobox "I found ${FOUND} ${MIMETYPE} files in the `basename ${ROOTDIR}` folder." 100 100
			rm -f "${TMPFILE}"
	
		# audio/* files found, move temp playlist into place.
		else
			echo "Found ${FOUND} ${MIMETYPE} files."
			mv -f "${TMPFILE}" "${PLAYLIST}"
		fi
	
	# If this happens someone has removed the temp 
	# file before it could be moved.  Bad mojo!
	else
		gdialog --infobox "The temporary playlist `basename ${TMPFILE}` I created has gone missing." 100 100
		rm -f "${TMPFILE}"
	fi

done
