#!/bin/sh
#
#	fdb	- freshmen database interface
#
#	purpose: extract info from freshmen 
#	  usage: fdb [ options ]
#
#	description: 
#		with no options, fdb dumps all records in the freshmen
#		database to stdout in the form:  LN=xxx:LN=xxx:CL=xx:AD=xxx
#		Command line options affect selection and format of records.
#
#	options: 
#
#	FLD=VAL		any argument of this form restricts the output to
#			records that fit that criterion. e.g. "fdb FN=Jane" 
#			outputs only those records where FN = Jane.  Note, 
#			multiple restrictions may appear on the command 
#			line, each further restricts output.
#
#	 -p		"Pretty-print" output in form '92 Smith, John
#
#	 -f filename	Read from datafile `filename' instead of freshmen
#
#	 -s		Sort output by class then lastname.  This automa-
#			tically sets the -p option.
#
#	to be implemented later
#
#	 -c		Count number of records 
#
#	 -v fld=value	Reverse the sense of a condition.  e.g.
#	 		fdb -v FN=Fred  excludes all records where
#			FN = Fred.  The -v option affects only the
#			subsequent argument.  Thus, 
#				  fdb -v FN=Fred CL=90 will output all
#			members of the class of 90 not named fred
#	bugs:
#
#	fdb FN=Mary	will catch Mary, Mary-Jane, Mary-Jo ...
#			It shouldn't do that.

# -----------------------------------------------------------------
# 0: set up some vars
# -----------------------------------------------------------------

    WORDCOUNT=NO                            # a flag
	PRETTY=NO				                # a flag
	DATAFILE=freshmen			            # a file
	SORTIT=NO				                # a flag
    INVERSE=NO                              # a flag
    XML=NO                                  # a flag
	CONDLIST=""				                # conditions
	TEMPFILE1=/tmp/$$.1			            # scratch
	TEMPFILE2=/tmp/$$.2			            # more scratch

# -----------------------------------------------------------------
# 1: run the command line
# -----------------------------------------------------------------

    while [ $# -gt 0 ] ; do			            # while argc>0
		case "$1" in
			*=*) CONDLIST="$CONDLIST $1"		 ;;
			-p)	 PRETTY=YES                      ;;
			-s)	 SORTIT=YES
				 PRETTY=YES			             ;;
			-f)	 shift
				 if [ "x$1" = "x" ] ; then
				    echo "-f flag needs arg"
			        exit 1
			     fi
			     DATAFILE=$1                     ;;
            -c)  WORDCOUNT=YES                   ;;
            -v)  CONDLIST="$CONDLIST -v"         ;;
            -x)  XML=YES                         ;;
			*)	 echo "Unknown option" ; exit 2  ;;
		esac
		shift				                # to next arg
    done

#
# check for XML v PRETTY clash
#
    if [ $PRETTY = YES ] && [ $XML = YES ] ; then
        echo "Cannot pprint and output to XML"
        exit 2
    fi

# -----------------------------------------------------------------
# 2: process the stuff
#	each condition runs grep on an ever smaller set of data
#	then if there is pretty printing, do that
#	then if there is sorting do that
# -----------------------------------------------------------------

    # sed 's/$/:/' $DATAFILE > $TEMPFILE1    # work with copy + ':' at end
    
    # NOTE: my fl does not accept colons (:) at the end of lines so... 
    #   I can't use sed cmd (see # above), and must use...
    #   grep -i -E "$condition:|$condition$"
    #   when checking, looking for both end of line ($), and colon (:),
    #   instead of just...
    #   grep -i "$condition:"
    
    cp $DATAFILE $TEMPFILE1                  # work with copy
        
	if [ "$CONDLIST" != "" ] ; then          # prepare for filtering
		for condition in $CONDLIST ; do
            if [ $condition = "-v" ] ; then
                INVERSE=YES
            elif [ "$INVERSE" = YES ] ; then
                grep -i -v -E "$condition:|$condition$" $TEMPFILE1 > $TEMPFILE2
                INVERSE=NO
                mv $TEMPFILE2 $TEMPFILE1
            else
                grep -i -E "$condition:|$condition$" $TEMPFILE1 > $TEMPFILE2
                mv $TEMPFILE2 $TEMPFILE1
            fi
		done
	fi

	# ... format nicely? ..
	if [ "$PRETTY" = YES ] ; then
        :
        pprint < $TEMPFILE1 > $TEMPFILE2     # format
        mv $TEMPFILE2 $TEMPFILE1             # rename
	fi

	# ... and sort it? ...
	if [ "$SORTIT" = YES ] ; then
		sort $TEMPFILE1
	fi

    # ... output as XML? ...
    if [ "$XML" = YES ] ; then
        echo "<?xml version = \"1.0\"?>"
        echo "<list>"
            ./fl sample.fmt < $TEMPFILE1
        echo "</list>"
    else
        cat $TEMPFILE1
    fi
    
    # ... give wordcount? ...
    if [ "$WORDCOUNT" = YES ] ; then
        echo "Word Count ="`wc -l < $TEMPFILE1`
    fi

# -----------------------------------------------------------------
# 3: clean up scratch files
# -----------------------------------------------------------------

	rm -f $TEMPFILE1 $TEMPFILE2

