#!/bin/bash
#
# [mp3 player script] =
# [plays all mp3 files recursively under certain directory]
#
# "bugs" : quotation-marks needed around wildcard
#
# Stefan van der Walt  1998/03/31

echo
echo "MP3 PlayLister 1.0.0 (trax@the-force.ml.org)"
echo

# start config section

echo Loading configuration...
player='mpg123 -@ '; # replace with your player's playlist mode
listdir='.'; # default directory containing mp3s (relative or precise)

# end config section -- nothing else needs to be changed

# warning messages

defaultdir=`dirs`; # this should work fine on most systems
                   # just make sure you have the dirs binary

# Just ignore this...was messing around
#
#case $defaultdir in
#'~') echo "[32;1mWarning! Still testing performance in home directory.[0m" ;;
#esac
#

# end warnings

param1=$1 # just load all the parameters
param2=$2 # into variables
param3=$3

echo Creating file-list home in ~/.PlayList
mkdir -p ~/.PlayList  # we don't need errormessages do we?

case $param2 in
'') param2='*';; # by default, play 'em all
esac

case $param3 in
'') ;;                # already defined in config section
*) listdir=$param3;   # but can be specified in parameter
esac

# I don't think this is a very good idea,
# but hey, it works (most of the times :)

echo
echo Press Control-C once to skip to next song, more times to abort.
echo Press Control-Z to pause, and type 'fg' at the prompt to
echo continue listening.
echo 

# Now starts the real fun...

case $param1 in
play) echo Creating file-list...
      find $listdir/* -name "$param2.mp3" > ~/.PlayList/PlayList.tmp;
      $player ~/.PlayList/PlayList.tmp ;;
list) echo Creating file-list...
      find $listdir/* -name "$param2.mp3" > ~/.PlayList/PlayList.tmp;
      cat ~/.PlayList/PlayList.tmp | less ;;
*) echo "[37;1mUsage: playlist {play|list} {expression-in-mp3name} {listdir}"
   echo
   echo "Example: playlist list 'b*'"
   echo "         playlist play 'metallica*' (remember quotes)"
   echo "         playlist play '*' mp3dir"

   echo "[0m" ;; esac

echo
echo Done : deleting temporary files
rm -rf ~/.PlayList

# Sad, but if you aborted the script, that file
# just sits there - forever.

# And not to confuse the user, let's switch
# back to the original directory.

case $defaultdir in
'~') cd ;;
*) cd $defaultdir ;;
esac

# Nothing wrong with a friendly bye-bye is there? :)

echo
echo Thank you for using MP3 PlayLister.
echo
