#!/bin/sh
TMPFILE=/tmp/timlzh
TMPEXT=/tmp/timlzh.dir
TMPLIST=/tmp/timlist
#DEVICE='-o /dev/audio'

for FILE in $* ; do

        case $FILE in
                -*) TIMOPT=$TIMOPT' '$FILE ; export TIMOPT
                    echo Change option $TIMOPT
		    continue;;
                --optclear) unset TIMOPT
		    continue;;
                *)

	TITLE=$FILE

	unset FILEOPT
	if [ -f $FILE.timopt ] ; then
		echo FILE Option : `cat $FILE.timopt`
		#echo FILE Option : `sed -e 's/-D *[0-9][0-9]*//g' $FILE.timopt`
		FILEOPT=`cat $FILE.timopt`
		#FILEOPT=`sed -e 's/-D *[0-9][0-9]*//g' $FILE.timopt`
	fi

	if [ ! -d $TMPEXT ] ; then mkdir $TMPEXT ; chmod 777 $TMPEXT
        else \rm -rf $TMPEXT/* ; fi

	case $FILE in
		*.lzh|*.LZH) LZH=1;;
		*) unset LZH
		   BASENAME=`echo $FILE | sed -e 's/\(.*\)\.[^\.][^\.]*/\1/g'`
		   case $FILE in
			*.gz) BASENAME=`echo $BASENAME |\
			       sed -e 's/\(.*\)\.[^\.][^\.]*/\1/g'`
		   esac
	esac

	echo 
	echo ----------- $FILE

	echo ==================== > $TMPFILE
	chmod 666 $TMPFILE
	echo FileName : $FILE > $TMPFILE
	echo ==================== >> $TMPFILE

	if [ $LZH ] ; then
		LOG=`dirname $FILE`/`basename $FILE .lzh`.log
		if [ -f $LOG ] ; then
			echo =========== >> $TMPFILE
			echo   LogFile   >> $TMPFILE
			echo =========== >> $TMPFILE
			echo >> $TMPFILE
			#cat $LOG |coco \*euc-japan\*unix >> $TMPFILE
			cat $LOG | nkf -ex >> $TMPFILE
		fi

		echo >> $TMPFILE

		echo ================ >> $TMPFILE
		if demac $FILE $TMPEXT/.MacLHA.lzh ; then
			echo create .MacLHA.lzh
			echo ---- MacLHA ---- >> $TMPFILE
			FILE=$TMPEXT/.MacLHA.lzh
			chmod 666 $FILE
		fi

		lha l $FILE > $TMPLIST.raw
		#cat $TMPLIST.raw | coco \*euc-japan\*unix >> $TMPFILE
		cat $TMPLIST.raw | nkf -ex >> $TMPFILE
		sed -e 's/^\[.*\]//g' $TMPLIST.raw | sed -e 's/  */ /g' |\
			cut -d' ' -f7- > $TMPLIST

		MIDLIST=`cat $TMPLIST|egrep -i '(mid|rcp|r36|std)$'`
		DOCLIST=`cat $TMPLIST|egrep -i '(log|doc|txt|me|hed|nif|up)$'`
		WRDLIST=`cat $TMPLIST|egrep -i 'wrd$'`

		if [ -z "$MIDLIST" ] ; then
			echo Don\'t have available format file \
				or invalid filename!
			cat $TMPLIST.raw
			sleep 4
			continue
		fi

		rm $TMPLIST $TMPLIST.raw
	else
		if [ "$BASENAME" = "`basename $BASENAME .rcp.gz`.rcp.gz" ];then
			gunzip -c $FILE > $TMPEXT/`basename $BASENAME .gz`
			chmod 666 $TMPEXT/`basename $BASENAME .gz`
		fi
		unset DOCLIST
		for i in log LOG doc DOC txt TXT hed HED nif NIF \
			me ME up UP 1st 1ST me ME lst LST ; do
			if [ -f $BASENAME$i.gz ] ; then
				cp $BASENAME$i.gz $TMPEXT
				DOCLIST=$DOCLIST' '`basename $BASENAME`$i
			fi
			if [ -f $BASENAME$i ] ; then
				cp $BASENAME$i $TMPEXT
				DOCLIST=$DOCLIST' '`basename $BASENAME`$i
			fi
		done
	fi

	if [ ! -z "$DOCLIST" ]; then
		echo ================ Documents ==================>> $TMPFILE
		if [ $LZH ];then lha xiw=$TMPEXT $FILE $DOCLIST > /dev/null;fi
		for i in $TMPEXT/* ; do
			echo --------------- `basename $i` ---------------->>\
				 $TMPFILE
			if [ "`basename $i .gz`" = "`basename $i`" ] ; then
				#demac $i|coco \*euc-japan\*unix >>$TMPFILE
				demac $i |\
				 tr '\r' '\n' | tr -s '\n' | tr -d '\0' |\
				nkf -ex >>$TMPFILE
			else
				gunzip $i
				demac $TMPEXT/`basename $i .gz` |\
				 #coco \*euc-japan\*unix >>$TMPFILE
				 tr '\r' '\n' | tr -s '\n' | tr -d '\0' |\
				 nkf -ex >>$TMPFILE
			fi
		done
		( cd $TMPEXT; rm -f $DOCLIST ) 
	fi

	echo >> $TMPFILE

	if [ ! $LZH ] ; then 
		unset WRDLIST
		for i in .wrd .WRD ; do
			if [ -f $BASENAME$i.gz ] ; then
				cp $BASENAME$i.gz $TMPEXT
				WRDLIST=$WRDLIST' '`basename $BASENAME`$i
			fi
			if [ -f $BASENAME$i ] ; then
				cp $BASENAME$i $TMPEXT
				WRDLIST=$WRDLIST' '`basename $BASENAME`$i
			fi
		done
	fi

	if [ ! -z "$WRDLIST" ]; then
		echo ==================== Words =====================>> $TMPFILE
		if [ $LZH ];then lha xiw=$TMPEXT $FILE $WRDLIST > /dev/null;fi
		for i in $TMPEXT/* ; do
			echo --------------- `basename $i` ---------------->>\
				$TMPFILE

			if [ "`basename $i .gz`" = "`basename $i`" ] ; then
				#demac $i|coco \*sjis\*dos \*euc-japan\*unix |\
				demac $i | nkf -ex |\
				 egrep -v '^@' >>$TMPFILE
			else
				gunzip $i
				demac $TMPEXT/`basename $i .gz` |\
				 #coco \*sjis\*dos \*euc-japan\*unix |\
				 nkf -ex |\
				  egrep -v '^@' >>$TMPFILE
			fi
		done
		( cd $TMPEXT; rm -f $WRDLIST ) 
	fi

	echo >> $TMPFILE

	echo ------------------------------ >> $TMPFILE
	echo Push F to get TiMidity output! >> $TMPFILE
	echo ------------------------------ >> $TMPFILE

	if [ $LZH ];then lha xiw=$TMPEXT $FILE $MIDLIST > /dev/null
	else cp $FILE $TMPEXT ; gunzip $TMPEXT/`basename $FILE`
	fi

	if [ -f $TMPEXT/.MacLHA.lzh ] ; then rm $TMPEXT/.MacLHA.lzh ; fi
	for i in $TMPEXT/*[Rr]36 ; do if [ -f $i ] ;then mv $i $i.rcp ;fi;done

	for i in $TMPEXT/* ; do
		if demac $i $i.demac ; then rm $i ; mv $i.demac $i ; fi
		case $i in
			*rcp|*RCP)
				rcptomid -S $i | smf1to0 > $i.mid
				#rcptomid $i | smf1to0 > $i.mid
				rm $i
				echo rcptomid
		esac
	done

	#(timidity -idvt "$TMPEXT/*" | tr '\r' '\n' >> $TMPFILE 
	#   kill `ps lwww | grep "less /tmp/timlzh" |\
	#   sed -e 's/  */ /g' | cut -d' ' -f4 ` >& /dev/null )&
	(xterm -sl 1000 -geometry +0+0 -fn 5x7 -bg darkblue -fg yellow \
             -T "TiMidity:$TITLE" \
	     -e timidity -U -inqt $TIMOPT $FILEOPT "$TMPEXT/*"
	   kill `ps awww | grep "less /tmp/timlzh" |\
	   sed -e 's/^ *//g' | cut -d' ' -f1 ` >& /dev/null )&

	less $TMPFILE
	rm $TMPFILE
	rm -rf $TMPEXT

	/bin/killall timidity >& /dev/null
	/bin/killall smf1to0 >& /dev/null

	;;
	esac

echo 
echo '################################################################'
echo '   Loading Next File ......  ( type Control-C to interrupt ) '
echo '################################################################'

sleep 2
done 
