#! /bin/sh
# @configure_input@
#
#  This script creates clheplib, which provides a list of libararies to link
#

prefix=@prefix@
exec_prefix=@exec_prefix@
uselibdir=$exec_prefix/lib

usage()
{
    echo "Usage: ./build-clheplib <list of subdirs>"
    exit $1
}

getdeplist()
{
    # recursive check for library dependencies
    # packages are listed multiple times, but tsort takes care of this
    dpkg=$1
    dfile=$2
    oldlist=`cat $dfile`
    if test -f $dpkg/$dpkg-deps; then 
	# strip out the package name and add it to $dfile
	newlist=`tsort $dpkg/$dpkg-deps | sed 's/CLHEP-//' | sed 's/-@VERSION@//'`
	echo $newlist >> $dfile
	# this is the recursive part
	for x in $newlist; do
            if test "$x" != "$dpkg"; then
                getdeplist $x $dfile
	    fi
	done
    fi
}

makeliblist()
{
    pkg=$1
    pkgfile="$pkg-library-list"
    rm -f $pkgfile
    touch $pkgfile
    depfile="$pkg-dep-list"
    rm -f $depfile
    touch $depfile
    # get relevant lists
    if test -f $pkg/$pkg-deps; then
	getdeplist $pkg $depfile
	list=`cat $depfile`
	for entry in $list; do
	    if test -f $entry/$entry-deps; then
	       cat $entry/$entry-deps >> $pkgfile
	    fi
	done
    fi
}

if test $# -eq 0; then
    usage 1
fi

while test $# -gt 0; do
    sublist="$sublist $1"
    shift
done

# create the concatenated library list
libfile="library-list"
rm -f $libfile
touch $libfile
for subdir in $sublist; do
    if test -f $subdir/$subdir-deps; then 
        cat $subdir/$subdir-deps >> $libfile
    fi
done

rm -f clheplib
echo "#! /bin/sh" > clheplib
echo "# " >> clheplib
echo "# " >> clheplib
echo " " >> clheplib
echo "usage() " >> clheplib
echo "{ " >> clheplib
echo "    echo \"Usage: clhepib [package]\" " >> clheplib
echo "    exit 1 " >> clheplib
echo "} " >> clheplib
echo " " >> clheplib
echo "if test \$# -eq 0; then " >> clheplib
echo "    pkg="all" " >> clheplib
echo "else " >> clheplib
echo "    pkg=\$1 " >> clheplib
echo "fi " >> clheplib
echo " " >> clheplib
echo "libdir=\"$uselibdir\" " >> clheplib
echo "if test -n \"\$CLHEP_DIR\"; then " >> clheplib
echo "    libdir=\"\$CLHEP_DIR/lib\" " >> clheplib
echo "fi " >> clheplib
echo " " >> clheplib
echo "case "\$pkg" in " >> clheplib

clibs=" "
tlist=`tsort $libfile`
for lib in $tlist; do
    clibs="$clibs -l$lib"
done
echo "all) " >> clheplib
echo "  if test -f \$libdir/libCLHEP-@VERSION@.a || test -f \$libdir/libCLHEP-@VERSION@.so " >> clheplib
echo "  then   " >> clheplib
echo "    echo -L\$libdir -lCLHEP-@VERSION@ " >> clheplib
echo "  else " >> clheplib
echo "    echo -L\$libdir $clibs" >> clheplib
echo "  fi " >> clheplib
echo "  ;; " >> clheplib

for sub in $sublist; do
    if test -f $sub/$sub-deps; then 
	makeliblist $sub
	tlist=`tsort $pkg-library-list`
	clibs=" "
	for lib in $tlist; do
	    clibs="$clibs -l$lib"
	done
	echo "$sub) " >> clheplib
	echo "  echo -L\$libdir $clibs" >> clheplib
	echo "  ;; " >> clheplib
    else
	echo "$sub) " >> clheplib
	echo "  echo -L\$libdir " >> clheplib
	echo "  ;; " >> clheplib
    fi
done

echo "*) " >> clheplib
echo "  echo unknown package \$pkg " >> clheplib
echo "  ;; " >> clheplib
echo "esac " >> clheplib
echo " " >> clheplib
echo "exit 0 " >> clheplib

chmod +x clheplib

exit 0
