#!/bin/sh
# get-sources.sh - retrieve list of source files
# usage: get-sources [-t] [-m] [-x]
#
# The idea of this script is that it suppresses .cpp and .h files that
# correspond to .syn files.

usage() {
    echo "Usage: $0 [-t] [-m] [-x] topdir" 1>&2
    echo "  -m    show main sources" 1>&2
    echo "  -t    show tools sources" 1>&2
    echo "  -x    show examples sources" 1>&2
}

DO_TOOLS=0
DO_MAIN=0
DO_EXAMPLES=0

DSET=0
FSET=0
while [ "x$1" != x ]; do
    case "$1" in
	-t) DO_TOOLS=1; FSET=1;;
	-m) DO_MAIN=1; FSET=1;;
	-x) DO_EXAMPLES=1; FSET=1;;
	-*) usage; exit 1;;
	*)
	    if [ -d "$1" -a $DSET = 0 ]; then
		TOPDIR="$1"
		DSET=1
	    else
		usage
		exit 1
	    fi
	;;
    esac
shift
done

if [ $FSET = 0 ]; then
    DO_TOOLS=1
    DO_MAIN=1
    DO_EXAMPLES=0
fi

if [ $DSET = 1 ]; then
    cd "$TOPDIR" || exit 1
    TOPDIR="${TOPDIR}/"
fi

############################################################

get() {
    (
	cd "$1"
	ls *.syn 2>/dev/null
	ls *.cpp *.hpp *.c *.h 2>/dev/null
	true
    ) | awk '
	{ basename=$1; sub("\\.[^\\.]*$", "", basename); }
	/\.syn$/ { hide[basename] = 1; doprint($1); next; }
	{ if (!hide[basename]) doprint($1); }
	function doprint(f) { printf "%s/%s\n", dir, f; }
    ' "dir=$1"
}

if [ $DO_TOOLS = 1 ]; then
    for DIR in cgbigen checksum helpgen insertsums; do
	get $DIR
    done
fi

if [ $DO_MAIN = 1 ]; then
    for DIR in \
		support agcore \
		guisupport icons dummygui vaclgui \
		ag1 \
		ag agcl \
		run \
    ; do
	get anagram/$DIR
    done
fi

if [ $DO_EXAMPLES = 1 ]; then
    for DIR in \
		include source \
    ; do
	get oldclasslib/$DIR
    done
    for DIR in \
		dsl fc ffcalc hw mpp rcalc \
    ; do
	get examples/$DIR
    done
fi

exit 0

