#!/bin/bash

LANGUAGE=${1:-CPP}
CSCOPE_DIR="$PWD/cscope"

if [ $# -lt 1 ]; then
    LANG="C"
else
    LANG="${1}"
fi

if [ ! -d "$CSCOPE_DIR" ]; then
    mkdir "$CSCOPE_DIR"
fi

case ${LANGUAGE} in
	CPP)    	ftypes=('*.c' '*.h' '*.cpp' '*.hpp' '*.cc') ;;
	PYTHON) 	ftypes=('*.py') ;;
	JAVA)   	ftypes=('*.java') ;;
	JAVASCRIPT) ftypes=('*.js') ;;
	*)
		echo "Usage: ${0} [CPP|PYTHON|JAVA|JAVASCRIPT]"
		exit 1
		;;
esac

echo "Finding files ..."
echo -n '' > "$CSCOPE_DIR/cscope.files"
for ftype in ${ftypes[@]}; do
	find "$PWD" \
		-type f -name "${ftype}" \
		-not -path "*/mock/*" \
		-not -path "*.git/*" \
		-not -path "*/regs/*" \
		-not -path "*/Regs/*" \
		-not -path "*/reg/*" >> "$CSCOPE_DIR/cscope.files"
done

echo "Adding files to cscope db: $PWD/cscope.db ..."
cscope -b -i "$CSCOPE_DIR/cscope.files"

export CSCOPE_DB="$PWD/cscope.out"
echo "Exported CSCOPE_DB to: '$CSCOPE_DB'"
