# !/bin/sh

VERSION=13.8.7

#
# Counter
#
COUNTER=${TOOLS:-/opt/exp}/bin/expcounter
if [ -x $COUNTER ]
then
	$COUNTER -p cscope -n cscalls
fi

USAGE="usage: $0 [-d|f|g|I|l|P|p|S|t] [-s] [-F field_sep] [cscope.out_file]"
set -- `getopt HVdfF:gIlpPsSt $*`
if [ $? -ne 0 ]
then
	echo $USAGE
	exit 1
fi
calling=true
defines=false
functions=false
globals=false
includes=false
locals=false
paramvalues=false
parameters=false
skip=false
structures=false
typedefs=false
for i in $*
do
	case $i in 
	-d)	defines=true; shift;;
	-f)	functions=true; shift;;
	-F)	fieldsep=$2; shift 2;;
	-g)	globals=true; shift;;
	-I)	includes=true; shift;;
	-l)	locals=true; shift;;
	-p)	paramvalues=true; shift;;
	-P)	parameters=true; shift;;
	-s)	skip=true; shift;;
	-S)	structures=true; shift;;
	-t)	typedefs=true; shift;;
	-H)	echo $USAGE; exit 0;;
	-V)	echo "$0: version $VERSION"; exit 0;;
	--)	shift; break;;
	esac
done

if [ $# -ne 0 ]
then
	reffile=$1
	if [ ! -s $reffile ]
	then
		echo "$0: error: Can't open input file: $reffile"
		echo $USAGE
		exit 1
	fi
	CSCOPEOPTIONS="-f $reffile"
	export CSCOPEOPTIONS
else
	reffile=cscope.out
fi

if [ ! -n "`sed '1!D' $reffile | grep '^cscope [0-9]*'`" ]
then
	echo "$0: error: Can't get cscope file version: $reffile"
	echo $USAGE
	exit 1
fi

if $skip
then
	opts="-xcs"
else
	opts="-xc"
fi

if $includes
then
	cscope -d $opts -L -8.* | cut -f1,4 -d' ' | tr " " "$fieldsep"
elif $defines
then
	cscope -d $opts -L -1.* | cut -f1,4 -d' ' | tr " " "$fieldsep"
elif $typedefs
then
	cscope -d $opts -L -0.* | cut -f1,4 -d' ' | tr " " "$fieldsep"
elif $functions
then
	cscope -d $opts -L -:.* | cut -f1-4 -d' ' | tr " " "$fieldsep"
elif $globals
then
	cscope -d $opts -L -4.* | cut -f1,4 -d' ' | tr " " "$fieldsep"
elif $locals
then
	cscope -d $opts -L -5.* | cut -f2,4 -d' ' | tr " " "$fieldsep"
elif $structures
then
	cscope -d $opts -L -6.* | cut -f4,5 -d' ' | tr " " "$fieldsep"
elif $parameters
then
	cscope -d $opts -L -2.* | cut -f2,4 -d' ' | tr " " "$fieldsep"
elif $paramvalues
then
	cscope -d $opts -L -9.* | cut -f2,4 -d' ' | tr " " "$fieldsep"
else
	cscope -d $opts -L -3.* | cut -f2,4 -d' ' | tr " " "$fieldsep"
fi
