#!/bin/ksh
# Version: @(#)$Revision: 1.14 $   $Date: 89/03/02 14:34:10 $
# 	
# Synopsis:
#	
# Author: Scott J. Warren
# Date:	  2/27/89
#
# Description:
#	Assumes catfiles, cstrip, ssize, ckfiles are in path.



# 	******************************************************************
# 	*() 			   quit					**
# 	******************************************************************
# 
# 	Quit is called when a trappable error is detected.  Any temporary
#	files that may have been created are removed.

quit() {
	rm -rf $TEMPDIR
	exit  $1
} 



# 	******************************************************************
# 	*() 			   main 				**
# 	******************************************************************
#
##	Set up the quit routine as the interrupt handler and initialize
##	variables.

	echo "Initializing" 1>&2
	trap "quit 1" 1 2 3 4 6 7 12 13 15 16 17 19

	TEMPDIR="/tmp/LinCNt$$"
	rm -rf $TEMPDIR
	mkdir $TEMPDIR
	INPUT="$TEMPDIR/input"
	BAD="$TEMPDIR/bad"
	GOOD="$TEMPDIR/good"
	TEMP="$TEMPDIR/temp"
	STRIP="$TEMPDIR/strip"

	MODE="all"
	CHECK="on"
	SOPT="-S"
	STRIPPER="cstrip -r"
	TITLE=" "
	BOPT=" "

##	Parse the command line parameters.  Stop as soon as anything that
##	is not a recognized parameter is found.  This is assumed to be an
##	input file name.

	DONE="false"
	while [ $DONE = "false" ] ; do
	    if [ $# -gt 0 ] ; then		# Something left to parse.
		case $1 in
		    "-bcoff"	) MODE="bcoff"
	    			  shift;;	
		    "-bconly"	) MODE="bconly"
	    			  shift;;	
		    "-ckonly"	) MODE="ckonly"
	    			  shift;;	
		    "-ckoff"	) CHECK="off"
	    			  shift;;	
		    "-P"	) STRIPPER="pstrip -r"
				  shift;;
		    "-T"	) shift
				  if [ $# -ne 0 ] ; then
				      TITLE=$1
				      shift
				  else
				      echo "linecounter: -T option specified but no title provided."
				      quit 1
				  fi ;;
		    "-b"	) BOPT="-b"
				  shift;;
		    "-s"	) shift
				  if [ $# -ne 0 ] ; then
				      STRIPPER="$1 -r"
				      SOPT="-s $1"
				      shift
				  else
				      echo "linecounter: -s option specified but no stripper provided."
				      quit 1
				  fi ;;
		    *		) DONE="true"
		esac
	    else
		DONE="true"
	    fi
	done

##	Build an input file, either by merging all of the user specified
##	files together, or by reading from stdin.

	if [ $# -gt 0 ] ; then		# user has specified file(s).
	    while [ $# -gt 0 ] ; do
		cat $1 >> $INPUT
		shift
	    done
	else				# read from stdin.
	    DONE="false"
	    while read LINE ; do
		echo $LINE >> $INPUT
	    done
	fi

##	Run the given file names through the existance checker, capturing
##	the bad names in $BAD.  Then remove the bad input lines from 
##	$INPUT.  Terminate if the "check only" flag is set.

	if [ $CHECK = "on" ] ; then
	    echo "Checking files" 1>&2

	    # >>>>>>>>>>Use tee in the real thing. <<<<<<<<<<<

#	    cp bogus $BAD
#
#   	    cat $BAD |& 
#   	    while read -p X1 X2 X3 NAME ; do	# ignore first 3 fields.
#		fgrep -v "$NAME" $INPUT > $TEMP
#		mv $TEMP $INPUT
#	    done

	    if [ $MODE = "ckonly" ] ; then
		quit 0
	    fi
	fi
	
##	Count the number of blank lines and whole comment lines are in the
##	good files.

	if [ $MODE != "bcoff" ] ; then
	    echo "Counting blank lines and whole comment lines." 1>&2
	    cat $INPUT | awk '{print $2 " " $3}' | catfiles | $STRIPPER > $STRIP

	    if [ $MODE = "bconly" ] ; then
		cat $STRIP
		quit 0
	    fi
	fi

##	Determine the number of added/deleted/modified lines by running the
##	input through ssize.  Post process the output from ssize to show 
##	NISS (Newly Integrated Source Statements).

	echo "Counting added, deleted and modified lines." 1>&2
	cat $INPUT | ssize $SOPT $BOPT > $TEMP

	cat $TEMP |&
	read -p JUNK			# skip past first two lines.
	read -p JUNK
	read -p NEW UNCHANGED ADDED DELETED MODIFIED NCSL

	if [ $NEW = "." ] ; then
	    NEW=0
	fi
	if [ $UNCHANGED = "." ] ; then
	    UNCHANGED=0
	fi
	if [ $ADDED = "." ] ; then
	    ADDED=0
	fi
	if [ $DELETED = "." ] ; then
	    DELETED=0
	fi
	if [ $MODIFIED = "." ] ; then
	    MODIFIED=0
	fi
	if [ $NCSL = "." ] ; then
	    NCSL=0
	fi

	NISL=`expr $NEW + $ADDED + $MODIFIED`
	REUSED=`expr $NCSL - $NISL`
	TOTAL=0

	echo "$TITLE" | adjust -c
	echo "------------------------------------------------------------------------------"
	echo " "
	echo Lines Percentage of Total | awk '{printf "\t\t\t     %s\t%s %s %s\n", $1, $2, $3, $4}'

	if [ $MODE != "bcoff" ] ; then
	    cat $STRIP |&
	    read -p TOTLINES
	    read -p COMLINES
	    read -p BLINES
	    TOTAL=`echo $TOTLINES    | awk '{print $3}'`
	    if [ $TOTAL -ne 0 ] ; then
		echo $COMLINES $TOTAL | \
		awk '{printf "    %s %s %s%9d\t     %7.2f\n", $1, $2, $3, $4, ($4/$5)*100 }'
		echo $BLINES $TOTAL | \
		awk '{printf "    %s %s\t%9d\t     %7.2f\n", $1, $2, $3, ($3/$4)*100 }'
	    else
		echo $COMLINES | awk '{printf "    %s %s %s%9d\t      -\n", $1, $2, $3, $4 }'
		echo $BLINES | awk '{printf "    %s %s\t%9d\t      -\n", $1, $2, $3 }'
	    fi
	fi

	if [ $TOTAL -ne 0 ] ; then
	    echo NCSL [1]: $NCSL $TOTAL | \
	    awk '{printf "    %s %s\t\t%9d\t     %7.2f\n", $1, $2, $3, ($3/$4)*100}'
	else
	    echo NCSL [1]: $NCSL | awk '{printf "    %s %s\t\t%9d\t      -\n", $1, $2, $3}'
	fi

	if [ $MODE != "bcoff" ] ; then
	    echo $TOTLINES $TOTAL    | awk '{printf "    %s %s\t%9d\n", $1, $2, $3 }'
	fi

	echo " "
	echo Lines Percentage of NCSL | awk '{printf "\t\t\t     %s\t%s %s %s\n", $1, $2, $3, $4}'

	if [ $NCSL -ne 0 ] ; then
	    echo New lines [2]: $NEW $NCSL | \
	    awk '{printf "    %s %s %s\t%9d\t     %7.2f\n", $1, $2, $3, $4, ($4/$5)*100}'
	    echo Added lines: $ADDED $NCSL  | \
	    awk '{printf "    %s %s \t%9d\t     %7.2f\n", $1, $2, $3, ($3/$4)*100}'
	    echo Deleted lines: $DELETED $NCSL | \
	    awk '{printf "    %s %s \t%9d\t     %7.2f\n", $1, $2, $3, ($3/$4)*100}'
	    echo Modified lines: $MODIFIED $NCSL  | \
	    awk '{printf "    %s %s \t%9d\t     %7.2f\n", $1, $2, $3, ($3/$4)*100}'
	    echo Unchanged lines: $UNCHANGED $NCSL | \
	    awk '{printf "    %s %s \t%9d\t     %7.2f\n", $1, $2, $3, ($3/$4)*100}'
	    echo " "
	    echo NISL [3]: $NISL $NCSL | \
	    awk '{printf "    %s %s \t\t%9d\t     %7.2f\n", $1, $2, $3, ($3/$4)*100}'
	    echo Reused:   $REUSED $NCSL | \
	    awk '{printf "    %s\t\t%9d\t     %7.2f\n", $1, $2, ($2/$3)*100}'
	else
	    echo New lines [2]: $NEW     | awk '{printf "    %s %s %s\t%9d\t      -\n", $1, $2, $3, $4}'
	    echo Added lines:   $ADDED   | awk '{printf "    %s %s\t%9d\t      -\n", $1, $2, $3}'
	    echo Deleted lines: $DELETED | awk '{printf "    %s %s\t%9d\t      -\n", $1, $2, $3}'
	    echo Modified lines: $MODIFIED   | awk '{printf "    %s %s\t%9d\t      -\n", $1, $2, $3}'
	    echo Unchanged lines: $UNCHANGED | awk '{printf "    %s %s\t%9d\t      -\n", $1, $2, $3}'
	    echo " "
	    echo NISL [3]: $NISL  | awk '{printf "    %s %s\t\t%9d\t      -\n", $1, $2, $3}'
	    echo Reused:   $REUSED| awk '{printf "    %s \t\t%9d\t      -\n", $1, $2, $3}'
	fi
	echo "------------------------------------------------------------------------------"
	echo " "
	cat <<-!
	Notes:

	[1] NCSL (Non Commented Source Lines) is defined as Total - (Comments + Blanks)
	    which equals New + Added + Modified + Unchanged.

	[2] 'New' lines are only those lines found in 'new' files, i.e. files with
	    type-code 'n'.  Added lines are those lines that have been added to 
	    files containing reused code, i.e. files with type-code 'r' or 'u'.

	[3] NISL (Newly Integrated Source Lines) is defines as New + Added + Modified.

!

	quit 0
