#!/bin/sh
## Music Shuffler
MS_sort="/mnt/us/musicshuffle/sort"
MS_dir="/mnt/us/music"
MS_i=1

IFS_BKP="${IFS}"
IFS=':'

[ -x ${MS_sort} ] || chmod +x ${MS_sort}
for MS_file in $( find $MS_dir -type f -print0 | $MS_sort -R -z | tr '\0' ':' ) ; do
		[ -f "${MS_file}" ] || continue
#		MS_padding=""
#		for MS_padi in 10000 1000 100 10 ; do
#				if [ ${MS_i} -ge ${MS_padi} ] ; then
#						MS_j="${MS_padding}${MS_i}"
#				else
#						MS_padding="${MS_padding}0"
#						MS_j="${MS_padding}${MS_i}"
#				fi
#		done

		MS_filename="${MS_file##*/}"

		# Then, try to extract the current index of our file
		MS_index="${MS_filename%%_*}"

		# Check if it's really an int, to see if this is the first randomizer round...
		if is_integer ${MS_index} ; then
				MS_real_filename="${MS_filename##${MS_index}_}"
		else
				# First run, our full name is our real name
				MS_real_filename="${MS_filename}"
		fi

		# And rename our files
		[ "${MS_filename}" != "${MS_i}_${MS_real_filename}" ] && mv -f "${MS_dir}/${MS_filename}" "${MS_dir}/${MS_i}_${MS_real_filename}"

		# Increment our index
		MS_i=$(( MS_i + 1 ))
done
# Restore IFS
IFS="${IFS_BKP}"
