#! /bin/sh
# make an xmms playlist with songs from $1 in random order
# depends on ruby
# 

me="`basename $0`"

d="${1?}"			# directory with sound files to play
if test -z "$d"; then
    echo no dir 1>&2
    exit 1
fi

if test ! -d "$d"; then
    echo "$d not a directory" 1>&2
    exit 1
fi

cd "$d" || {
    echo "could not change to $d" 1>&2
    exit 1
}

pwd="`/bin/pwd`"
tmpf="`tempfile --prefix=$me`"
ls > $tmpf
n=`wc -l $tmpf | awk '{ print $1 }'`
indices="`tempfile --prefix=$me-idx`"
cat <<EOF
[playlist]
NumberOfEntries=$n
EOF

j=1
ruby -e "a=[];(1..$n).each { |i| a << i }; while a.length > 0; i=(a.length * rand).to_i; puts(a[i]); a.delete_at(i); end" | while read i; do 
    printf "File$j=%s/%s\n" "$pwd" "`sed -n ${i}p $tmpf`"
    j=`expr $j + 1`
done
rm -f $indices $tmpf
