#!/bin/sh
#
# Run cscope for PCP source (src and qa)
#

tmp=/var/tmp/cscope.run.$$
trap "rm -f $tmp.*; exit 0" 0 1 2 3 15

rebuild=false
if [ $# -eq 1 ]
then
    case "$1"
    in
	-b)	# rebuild files list and index
		rebuild=true
		shift
		;;
	-x)	# rebuild and exclude /usr/include
		rebuild=true
		export INCDIR=
		shift
		;;
    esac
fi

if [ $# -ne 0 ]
then
    echo >&2 "Usage: cscope.run [options]"
    echo >&2 "Options:"
    echo >&2 "-b	rebuild files list and index"
    echo >&2 "-x	exclude /usr/include, implies -b"
    exit 1
fi


if $rebuild || [ ! -f cscope.files ]
then
    rm -f cscope.out cscope.files
    git ls-files >$tmp.tmp
    find src qa -type f -a \( -name "*.[chly]" -o -name "*.h.in" -o -name "*.cpp" \) \
    | while read file
    do
	if grep "^$file\$" $tmp.tmp >/dev/null
	then
	    echo "$file" >>cscope.files
	fi
    done
    cscope -b -i cscope.files
fi

cscope -i cscope.files
