#! /bin/zsh


SELF_CMD=$0

# if [ -z ${PLAY} ];

# not used anymore

# if [ ${UID} = 0 ];
#     then
# #    PLAY="nice -n -19 amp -b 10000 "
#     PLAY="nice -n -19 mpg123 -b 1000"
#     else
# if which amp;
# then
#     PLAY="mpg321 --aggressive -q -b 10000"
#     # " #alsa!
#     #amp -b 10000
# else
#     PLAY="mpg123 -b 10000"
# fi
# fi


color_echo ()
{
    if which color >/dev/null
    then
        echo `color green`  `color bd` "$1" `color off`
    else
        echo "$1"
    fi
}



if [ $# -gt 0 -a -d ${1} ] ;
then
    cd ${1}
    shift
else
    echo "no directory specified!"
fi

START=1

if [ $# -ge 1 ];
then
    START=$1
    echo "skipping to $START"
fi


#
#`<space><tab><newline>''

##
old_ifs="${IFS}"
# IFS="\n"
# songs=`ls |sort -n|sed -e "s/'/\'/g;s/^/'/;s/$/'/"`

if [ -f order ];
then
    songs=$(cat order)
else
    songs=`ls -v`
#|sed -e "s/~/\\~/g;s/$/~/"`
fi


# IFS='\n \t'
# echo Going to play ${songs}
IFS="
"
setopt SH_WORD_SPLIT
#typeset -A sep_songs
#sep_songs=(${songs})
#unsetopt SH_WORD_SPLIT

for song in $songs;
do
    unsetopt SH_WORD_SPLIT
# We must return to use commonly accepted syntax:
    IFS="${old_ifs}"
    if [ $START -gt 1 ];
    then
        START=`expr $START - 1`
            # echo $START
        else
            if [ -d "$song" ];
            then
                pushd "$song"
                $SELF_CMD
                popd
            else
               play_one  "$song"
            fi
    fi
done
