#!/bin/bash
# mod/modplay script to go with mikmod
# Version 1.2
#============================================================================
#    If invoked as "mod", will search the $MOD_PATH and grep for the rest of
# the command line arguments, then play the resulting mods in a random 
# order, looping and re-randomising when the list is exhausted. Also, if 
# run in an xterm it will change the title to "modplay".
#    "modplay" will just play the specified mod files, in a random order if 
# the first argument is "-r".
# new hacked mikmod will now deal with mods in zip files so long as there
# is just the mod in it... 
#
# Released v1.0 under GPL Steve McIntyre <93sam@eng.cam.ac.uk> 26/7/96, 
#      along with MikMod v2.11-
#
# Updated to version 1.1 18/10/96 using patch from Martin Stjernholm
#      <mast@lysator.liu.se>
# 
# Updated to version 1.2 20/11/96 with help from Joey Hess <joey@kite.ml.org>.
#	Should now handle mods with spaces in their filenames, but not in 
#	random mode yet.
# 
# Thanks to Jon Rabone <93jkr@eng.cam.ac.uk> for the awk randomiser script.
#============================================================================

# XXX - this does not currently work correctly on SunOS unless bash
# is installed

# Set this correctly to point to the directory containing the mod files...
#
if [ "$MOD_PATH" = "" ] ; then
	MOD_PATH="/sounds/mod"
fi

# Default sound quality settings. Only used by drv_vox so far, although
# may become more integrated later. ..
#
export MM_FRAGSIZE=17
export MM_NUMFRAGS=100

# Mixing freuqency and other flags, default.
#
quality="-i"

#============================================================================
#No user-serviceable parts below!!!	   No user-serviceable parts below!!!
#============================================================================

# These _should_ be unique!
#
NEWNDX="/tmp/$UID.newmod"
FULLNDX="/tmp/$UID.fullmod"
quit=0
random=$1

if [ "`basename $0`" = "mod" ] ; then
	if [ $TERM = xterm ] ; then
	      echo -e -n "\033]0;modplay\007"
	fi
	random="-r"
	echo Indexing...
	find $MOD_PATH |grep "\." > $FULLNDX
	mv $NEWNDX $NEWNDX.1
	> $NEWNDX
	if [ $# -gt 0 ] ; then
		while [ $# -gt 0 ] ;
		do
			echo "Finding \"$1\"..."
			cat $FULLNDX |grep $1 >> $NEWNDX
			shift
		done
	mv $NEWNDX $FULLNDX
	fi
else
	if [ "$random" = "-r" ] ; then
		shift
		> $FULLNDX
		for file in "$@"
		do
			echo $file >> $FULLNDX
		done
	fi
fi

function RANDOM ()
{
	echo Randomising...
	cat $FULLNDX | awk '
	BEGIN { lines=1; srand(); }

	{ fname[lines] = $0
	  lines+=1 }

	END { for (i=1; i<lines; i++) {
			x=int((lines-1) * rand())+1
			while (used[x] == 1) {
			 	x=int((lines-1) * rand())+1
		 	}
			used[x]=1
			printf ("%s ",fname[x]);
		}
	}
	'>$NEWNDX
}

case $random in

-r)	while [ $quit==0 ]
        do
                RANDOM
		if (mikmod $quality `cat $NEWNDX`) ; then
			quit=1;
		fi
                if [ $quit = 1 ] ; then
	                echo "quit!"
                        exit 1;
                else
                        echo Repeating......;
                fi
        done;;
  
*)	if (mikmod $quality "$@") ; then
		quit=1;
	fi
esac
