: gather up statistics on code

gotsccs=0
gotsource=0
gotroutine=0
gotfile=0
switched=0
files=""

while [ $# != 0 ]
do
	case $1 in
	-sccs)
		gotsccs=1
		sccsbase=$2
		sccsfiles="$3"
		shift
		shift ;;
	-source)
		gotsource=1
		sourcebase=$2
		sourcefiles="$3"
		shift
		shift ;;
	-routine)
		gotroutine=1
		routineout=$2
		shift ;;
	-file)
		gotfile=1
		fileout=$2
		shift ;;
	*)
		echo "usage: $0 -routine <routine-output-file> -file <file-output-file> -source <path> <filespec> -sccs <path> <filespec>"
		exit 1
	esac
	shift
done

if [ ${gotsccs} = 0 -a ${gotsource} = 0 ]
then
	echo "usage: $0 -routine <routine-output-file> -file <file-output-file> -source <path> <filespec> -sccs <path> <filespec>"
	exit 1
fi
if [ ${gotroutine} = 0 -o ${gotfile} = 0 ]
then
	echo "usage: $0 -routine <routine-output-file> -file <file-output-file> -source <path> <filespec> -sccs <path> <filespec>"
	exit 1
fi

t_hal=/tmp/$$.halstead
t_kdsi=/tmp/$$.kdsi
t_chg=/tmp/$$.changes
temp=/tmp/$$.temp

trap '/bin/rm -f ${t_hal} ${t_kdsi} ${t_chg} ${temp}; exit 1' 1 2 15

cd ${sourcebase}


# halstead output is Filename, Length, Volume, Level, and
# Effective mental discriminations;  in halstead terms, name, N, V, L, E^
#
halstead ${sourcefiles} | sort > ${t_hal}
#
# altkdsi output is actual source lines (DSI), comment count
#
altkdsi ${sourcefiles} | sort > ${t_kdsi}



cd ${sccsbase}
#
# altparse.prs output is filename, change count
#
altparse.prs ${sccsfiles} | sort > ${t_chg}
cd ${sourcebase}


# joining halstead and kdsi info, note the tab seperator
join -j1 1 -j2 1 -a1 -a2 -e '%%%' '-t	' -o 1.1 1.3 2.2 2.3 ${t_hal} ${t_kdsi} > ${temp}

fgrep -s '%%%' ${temp}
if [ $? != 1 ]
then
	echo "$0: unable to matchup files on join of halstead and kdsi"
	echo "  see files ${t_hal} ${t_kdsi} ${temp} (output)"
	/bin/rm -f ${t_chg}
	exit 1
fi

# joining halstead/kdsi and sccs info, note the tab seperator
join -j1 1 -j2 1 -a1 -a2 -e '%%%' '-t	' -o 1.1 2.2 1.2 1.3 1.4 ${temp} ${t_chg} > ${fileout}

fgrep -s '%%%' ${temp}
if [ $? != 1 ]
then
	echo "$0: unable to matchup files on join of halstead/kdsi and changes"
	echo "  see files ${temp} ${t_chg} ${fileout}"
	/bin/rm -f ${t_hal} ${t_kdsi}
	exit 1
fi

mccabe -n ${sourcefiles} | sort > ${routineout}

echo "file ${fileout} contains:"
echo "filename changecount volume kdsi comments"
echo " "
echo "file ${routineout} contains:"
echo "filename routinename mccabe returns adjusted/mccabe"

/bin/rm -f ${t_hal} ${t_kdsi} ${t_chg} ${temp}
exit 0
