#!/bin/bash
# Play MODs from your RAR files. 
# Version 1
#============================================================================
#
# 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.
#
# First version of MODR by Donncha O Caoimh <xeer@hotmail.com>. 31/08/97
#	This version creates the MOD index faster by calling the decruncher
#	just once. Doesn't matter how many mod files are in each archive.
#		
#============================================================================

# 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=$1
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 frequency and other flags, default.
#
quality1=""          # extra options for MIDP.
quality2=""          # extra options for MIKMOD.

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

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

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
}


if [ $# -gt 0 ] ; then
	echo Now indexing files in $MOD_PATH.... PLEASE WAIT.
	unrar l -c- $MOD_PATH/'*.rar' *.xm *.it *.669 *.mod *.uni *.ult *.stm *.s3m *.mtm > $MODLST

cat $MODLST | awk '
                /Archive/               { rararc = $2 }
                NF==10 && $1 !="Name"   {printf ("%s %s\n", rararc, $1)}
                ' >$FULLNDX 
fi


if [ -a $FULLNDX ] ; then
	echo MOD index found.
else
	echo MOD index not found! Please pass the location of your MODs directory to the program next time.
	exit 1
fi

while [ $quit==0 ]
	do
	RANDOM
	for i in `cat $NEWNDX`
		do
		if [ -a $i ] ; then
			MUSIC_ARC=$i
		else
			echo $MUSIC_ARC $i
			if [ ! -a $EXTMOD ] ; then
				mkdir $EXTMOD
			fi
			unrar x $MUSIC_ARC $i $EXTMOD
			if [ `ls $EXTMOD/*.IT 2> /dev/null` ] ; then
				if (midp $quality1 $EXTMOD/$i) ; then
	                		quit=1
	        		fi
			else
				if (mikmod -s $quality2 $EXTMOD/$i) ; then
	                		quit=1
	        		fi
			fi
			rm -rf $EXTMOD
			if [ $quit = 1 ] ; then
	                        echo "quit!"
	                        exit 1;
	                fi
		fi
		done
	done
reset             
