#!/bin/sh

set -e

# Makefile commands that don't need to be kept in the Makefile

for cmd in $*; do
case $cmd in

#############################################################################
# Code-size info:
#############################################################################

print_sizes)
	wc `find $SRC$SPATH -name "*" -xtype f -print`
	;;

print_semi_sizes)
	cat `find $SRC$SPATH -name "*" -xtype f -print` | grep ";" | wc
	;;

#############################################################################
# Searching for bugs/etc:
#############################################################################

find_old)
	grep -n "//[A-Z]//" `find $SRC$SPATH -name "*" -xtype f -print` \
		| tee errlist
	;;

find_fixes)
	grep -n -w "XXX " `find $SRC$SPATH -name "*" -xtype f -print` \
		| tee errlist
	;;

find_bugs)
	grep -n -w "XXX/BUG" `find $SRC$SPATH -name "*" -xtype f -print` \
		| tee errlist
	;;

find_adds)
	grep -n -w "XXX/ADD" `find $SRC$SPATH -name "*" -xtype f -print` \
		| tee errlist
	;;

find_opts)
	grep -n -w "XXX/OPT" `find $SRC$SPATH -name "*" -xtype f -print` \
		| tee errlist
	;;

find_cleans)
	grep -n -w "XXX/CLN" `find $SRC$SPATH -name "*" -xtype f -print` \
		| tee errlist
	;;

find_deps)
	grep -n -w "XXX/DEP" `find $SRC$SPATH -name "*" -xtype f -print` \
		| tee errlist
	;;

find_questions)
	grep -n -w "XXX/???" `find $SRC$SPATH -name "*" -xtype f -print` \
		| tee errlist
	;;

find_xxx)
	$0 find_fixes find_bugs find_adds find_opts find_cleans \
		 find_deps find_questions
	;;

#############################################################################
# Cleaning up:
#############################################################################

tidy)
	rm -rf *.ofs.* *.ii *.rtl* *.tmp *.s *.s86 *.sym 
	;;
 
clean)
	$0 tidy
	rm -rf *.o *.bak $TARGETS $XFILES $RXFILES $DXFILES $LXFILES
	;;

cleanconf)
	$0 clean
	grep "^Makefile:" Makefile | cut -d : -f 2 >> Makefile.parts
	rm -f Makefile.append
	for mkf in *.mak; do
		if [ -r $mkf ] ; then
			echo `basename $mkf .mak`: config >> Makefile.append
			echo "	\$(MAKE) \$@" >> Makefile.append
		fi
	done
	rm -f Makefile *.mak
	for part in `cat Makefile.parts`; do
		if [ -r $part ] ; then
			echo "Makefile: $part" >> Makefile
			cat $part >> Makefile
		fi
	done
	if [ -r Makefile.append ] ; then
		cat Makefile.append >> Makefile
	fi
	rm -f Makefile.parts Makefile.append
	rm -f *.mak *.[cChHsSyY] *.S86 *.ofs
	;;

cleantemp)
	$0 tidy
	for file in $TARGETS *.ofs *.[Hh]; do
		if [ -r $file ] ; then	
			mv $file $file.save
		fi
	done
	$0 cleanconf
	for file in *.save; do
		if [ -r $file ] ; then	
			mv $file `basename $file .save`
		fi
	done
	;;

cleanconf_all)
	$0 cleanconf
	for i in $SUBDIRS; do 
		if [ -d $i ] ; then 
			pushd $i; make cleanconf_all; popd
		fi 
	done 
	;;

cleantemp_all)
	$0 cleantemp
	for i in $SUBDIRS; do 
		if [ -d $i ] ; then 
			pushd $i; make cleantemp_all; popd
		fi 
	done 
	;;

rmtree)
	$0 cleanconf
	for i in $SUBDIRS; do 
		if [ -d $i ] ; then 
			pushd $i; make rmtree; popd
			rm -f $i/Makefile
			rmdir $i  
		fi 
	done
	;;


#############################################################################
# informative commands:
#############################################################################

purpose)
	if [ -f $SRC$SPATH/.purpose ] ; then cat $SRC$SPATH/.purpose; \
	else echo "Unknown: missing .purpose"; fi
	;;
	
showsrctree)
	$BINDIR/showtree $SRC $SPATH
	;;

showtree)
	$BINDIR/showtree $BASE $SPATH
	;;

showsrc_sizes)
	$BINDIR/showtree -s $SRC $SPATH
	;;

showtree_sizes)
	$BINDIR/showtree -s $BASE $SPATH
	;;

list)
	if [ -n "$SUBDIRS" ] ; then echo SUBDIRS= $SUBDIRS; fi
	if [ -n "$XINCPATH" ] ; then echo XINCPATH= $XINCPATH; fi
	if [ -n "$XINCFILES" ] ; then echo XINCFILES= $XINCFILES; fi
	if [ -n "$INCFILES" ] ; then echo INCFILES= $INCFILES; fi
	if [ -n "$SRCFILES" ] ; then echo SRCFILES= $SRCFILES; fi
	if [ -n "$LINKFILES" ] ; then echo LINKFILES= $LINKFILES; fi
	if [ -n "$COPYFILES" ] ; then echo COPYFILES= $COPYFILES; fi
	if [ -n "$XFILES" ] ; then echo XFILES= $XFILES; fi
	if [ -n "$RXFILES" ] ; then echo RXFILES= $RXFILES; fi
	if [ -n "$DXFILES" ] ; then echo DXFILES= $DXFILES; fi
	if [ -n "$LXFILES" ] ; then echo LXFILES= $LXFILES; fi
	if [ -n "$TARGETS" ] ; then echo TARGETS= $TARGETS; fi
	if [ -n "$COMMANDS" ] ; then echo COMMANDS= $COMMANDS; fi
	;;

#############################################################################
# Archival commands:
#############################################################################

tar)
	cd $SRC; tar cvf ../sos.tar ./* 
	;;

tarz)
	cd $SRC; tar cvfz ../sos.tgz ./* 
	;;

ftarz)
	mount /fd0
	cd $SRC; tar cvfz /fd0/sos.tgz ./*
	umount /fd0
	;;

#############################################################################
# Distribution-creation commands:
#############################################################################

solo_dist)
	$BINDIR/mkdist solo
	#$BINDIR/mkdist -z solo
	;;

solo_bindist)
	$BINDIR/mkdist -b solo
	#$BINDIR/mkdist -z -b solo
	;;

sos_dist)
	$BINDIR/mkdist sos
	#$BINDIR/mkdist -z sos
	;;

sos_bindist)
	$BINDIR/mkdist -b sos
	#$BINDIR/mkdist -z -b sos
	;;

deco_dist)
	$BINDIR/mkdist deco
	#$BINDIR/mkdist -z deco
	;;

deco_bindist)
	$BINDIR/mkdist -b deco
	#$BINDIR/mkdist -z -b deco
	;;

#############################################################################
# Misc:
#############################################################################

listsrc)
	allow_null_glob_expansion=; \
	 find $SRC $SRC$SPATH/m-* \
		-name "*.[CcHh]" -printf "%P\n"
	;;

headers)
	echo "Saving source files to './old_src.tgz'..."
	tar cfz ./old_src.tgz -C $SRC .$SPATH
	echo "Updating headers under $SRC$SPATH:"
	set -e; allow_null_glob_expansion=; PATH=$PATH:`pwd`/$BINDIR; \
	 cd $SRC; find .$SPATH -regex ".*\.\([CcHhyS]\|S86\)"'$' \
		-exec stdheader {} \; 
	;;

help)
	echo "Make commands:"
	echo "  tar		create archive in $SRC/.."
	echo "  tarz		create compressed archive in $SRC/.."
	echo "  libs		build all libraries"
	echo "  showtree	show directory tree in build area"
	echo "  showsrctree	show directory tree in source area"
	echo "  showtree_sizes	show build tree with sizes"
	echo "  showsrc_sizes	show source tree with sizes"
	echo "  purpose	show purpose of current directory"
	echo "  list		list config of current directory"
	echo "  clean		remove all object files and executables"
	echo "  cleantemp	remove all temporary files (all but targets)"
	echo "  cleanconf	remove all configuration information"
	echo "  cleanconf_all	remove all config info from all subdirs"
	echo "  cleantemp_all	remove all temporary files from all subdirs"
	echo "  rmtree	delete all subdirs below this"
	echo "  tree		build all subdirs"
	echo "  headers	fix source headers for entire source tree"
	echo "  listsrc	list all source files from entire tree"
	echo "  print_sizes	print 'wc' output of all source files"
	echo "  print_semi_sizes  print semicolon line count"
	;;
esac
done

