#!/bin/ksh
# KSH shell script to walk through directories
# invoke scripts in all sub-directories pointed by DIRECTORIES variable

DIRS=`cat dirs.ls`

if [ -z "${SCRIPTS}" ]
then
	SCRIPTS=`pwd`/scripts
fi

if [ -z "${LOGS}" ]
then
	LOGS=`pwd`/logs
fi

if [ -z "${FUNCTION}" ]
then
	FUNCTION=r2h
fi

if [ -z "${WINNTDIR}" ]
then
	WINNTDIR=./WisdomNT
fi

export SCRIPTS
export LOGS
export FUNCTION
export WINNTDIR

#echo "Starting in `pwd` directory,"
#echo "performing compilation in all pointed sub-directories..."
#echo "ENVIRONMENT:"
#echo "    SCRIPTS=${SCRIPTS}"
#echo "    LOGS=${LOGS}"
#echo "    WINNTDIR=${WINNTDIR}" 
#echo ""

for i in ${DIRS}
do
	LASTDIR=`pwd`
	cd ${i}
	#echo "Checking action in `pwd` directory..."
	if [ -d "${WINNTDIR}" ]
	then
		cd ${WINNTDIR}
		#echo "Performing action in `pwd` directory..."
		case $FUNCTION in
		r2h)	echo "Recompiling R2H in `pwd` directory."
			#echo "calling ./r2h.make in `pwd`"
			#rm -f r2h.out
			make -f ./r2h.make
			echo "" >>${LOGS}/r2h.stdout.log
			echo "" >>${LOGS}/r2h.stdout.log
			echo "" >>${LOGS}/r2h.stdout.log
			echo "" >>${LOGS}/r2h.stdout.log
			echo "" >>${LOGS}/r2h.stdout.log
			echo "R2H compilation in `pwd` directory." >>${LOGS}/r2h.stdout.log
			echo "Date: `date`." >>${LOGS}/r2h.stdout.log
			echo "" >>${LOGS}/r2h.stdout.log
			cat r2h.out >>${LOGS}/r2h.stdout.log
			;;

		s2p)	echo "Recompiling S2P in `pwd` directory."
			#echo "calling ./s2p.make in `pwd`"
			make -f ./s2p.make
			if [ -f s2p.out ]
			then
				echo "" >>${LOGS}/s2p.stdout.log
				echo "" >>${LOGS}/s2p.stdout.log
				echo "" >>${LOGS}/s2p.stdout.log
				echo "" >>${LOGS}/s2p.stdout.log
				echo "" >>${LOGS}/s2p.stdout.log
				echo "S2P compilation in `pwd` directory." >>${LOGS}/s2p.stdout.log
				echo "Date: `date`." >>${LOGS}/s2p.stdout.log
				echo "" >>${LOGS}/s2p.stdout.log
				cat s2p.out >>${LOGS}/s2p.stdout.log
			fi
			;;

		e2p)	echo "Recompiling E2P in `pwd` directory."
			#echo "calling ./e2p.make in `pwd`"
			make -f ./e2p.make
			if [ -f e2p.out ]
			then
				echo "" >>${LOGS}/e2p.stdout.log
				echo "" >>${LOGS}/e2p.stdout.log
				echo "" >>${LOGS}/e2p.stdout.log
				echo "" >>${LOGS}/e2p.stdout.log
				echo "" >>${LOGS}/e2p.stdout.log
				echo "E2P compilation in `pwd` directory." >>${LOGS}/e2p.stdout.log
				echo "Date: `date`." >>${LOGS}/e2p.stdout.log
				echo "" >>${LOGS}/e2p.stdout.log
				cat e2p.out >>${LOGS}/e2p.stdout.log
			fi
			;;

		aliases) echo "Removing aliases from <course.gcf> in `pwd` directory."
			#echo "calling ./alias.sh in `pwd`"
			./alias.sh
			if [ -f wwwalias.out ]
			then
				echo "" >>${LOGS}/alias.stdout.log
				echo "" >>${LOGS}/alias.stdout.log
				echo "" >>${LOGS}/alias.stdout.log
				echo "" >>${LOGS}/alias.stdout.log
				echo "" >>${LOGS}/alias.stdout.log
				echo "Aliases replacement in `pwd` directory." >>${LOGS}/alias.stdout.log
				echo "Date: `date`." >>${LOGS}/alias.stdout.log
				echo "" >>${LOGS}/alias.stdout.log
				cat wwwalias.out >>${LOGS}/alias.stdout.log
			fi
			;;

		clean)	echo "Cleaning in `pwd` directory."
			#echo "calling ./clean.sh in `pwd`"
			./clean.sh
			;;

		rmall)	echo "Removing all files from `pwd` directory..."
			rm -f *
			;;

		cpimgs)	echo "Trying to copy missing images to `pwd` directory..."
			#echo "calling ./cp_imgs in `pwd`"
			if [ -f ./cp_imgs ]
			then
				sh ./cp_imgs 2>cp_imgs.out
				cat cp_imgs.out
				echo "" >>${LOGS}/cp_imgs.stdout.log
				echo "" >>${LOGS}/cp_imgs.stdout.log
				echo "" >>${LOGS}/cp_imgs.stdout.log
				echo "" >>${LOGS}/cp_imgs.stdout.log
				echo "" >>${LOGS}/cp_imgs.stdout.log
				echo "Copying misssing images in `pwd` directory.">>${LOGS}/cp_imgs.stdout.log
				echo "Date: `date`." >>${LOGS}/cp_imgs.stdout.log
				echo "" >>${LOGS}/cp_imgs.stdout.log
				cat cp_imgs.out >>${LOGS}/cp_imgs.stdout.log
			else
				echo "Nothing to do!"
			fi
			;;

		copy)	echo "Copying files from ${SCRIPTS} to `pwd` directory."
			ln -s -f ${SCRIPTS}/prep.sh .
			ln -s -f ${SCRIPTS}/proc.sh .
			ln -s -f ${SCRIPTS}/alias.sh .
			ln -s -f ${SCRIPTS}/change_name .
			ln -s -f ${SCRIPTS}/r2h.make .
			ln -s -f ${SCRIPTS}/*.gif .
			if [ -f "../processed" ]
			then
				# neither play-list nor export if <processed> is present
				ln -s -f ${SCRIPTS}/s2p.null.make s2p.make
				ln -s -f ${SCRIPTS}/e2p.null.make e2p.make
			else
				# maybe play-list or export
				if [ -d "../seporgimagedir" ]
				then
					# play-list, no exports if <seporgimagedir> is present
					rm -f ./prep.sh
					rm -f ./proc.sh
					rm -f e2p.make
					rm -f s2p.make
					ln -s -f ${SCRIPTS}/e2p.null.make e2p.make
					ln -s -f ${SCRIPTS}/s2p.full.make s2p.make
					ln -s -f ${SCRIPTS}/prep.s2p.sh   ./prep.sh				
					ln -s -f ${SCRIPTS}/proc.s2p.sh   ./proc.sh				
				else
					# no play-list, exports
					rm -f ./prep.sh
					rm -f ./proc.sh
					rm -f e2p.make
					rm -f s2p.make
					ln -s -f ${SCRIPTS}/s2p.null.make s2p.make
					ln -s -f ${SCRIPTS}/e2p.full.make e2p.make
					ln -s -f ${SCRIPTS}/prep.e2p.sh   ./prep.sh
					ln -s -f ${SCRIPTS}/proc.e2p.sh   ./proc.sh
				fi
			fi
			ln -s -f ${SCRIPTS}/clean.sh .
			ln -s -f ${SCRIPTS}/report.sh .
			ln -s -f ${SCRIPTS}/r2h.ini .
			;;

		prep)	echo "Copying working files to `pwd` directory."
			#echo "calling ./prep.sh in `pwd`"		
			./prep.sh
			;;

		proc)	echo "Copying <PROCESSED> working files to `pwd` directory."
			#echo "calling ./proc.sh in `pwd`"		
			./proc.sh
			;;

		report)	echo "Summarize reports from `pwd` directory to log files."
			#echo "calling ./report.sh in `pwd`"
			./report.sh
			;;
	
		esac
		cd ..
	else
		#echo "Passing call to sub-directories..."
		./dirs.sh
	fi
	#echo "Exiting `pwd` sub-directory..."
	#echo ""
	cd ${LASTDIR}
done		

#echo ""
#echo "Recursive compilation completed for all sub-directories."
 
