#!/bin/sh

CERN=$HOME/tmp/cernlib
CERN_LEVEL=2002
CERN_ROOT=$CERN/2002
CVSCOSRC=$CERN_ROOT/src
PATH=$PATH:/$CERN_ROOT/bin
LOG=$CERN_ROOT/build/log
export CERN CERN_ROOT CVSCOSRC PATH CERN_LEVEL

default="scripts packlib pawlib graflib mathlib phtools"
#default="$default packpgm pawpgm grafpgm geant321 mclibs "

build(){
	[ ! -r $1/Makefile ] && gmake $1/Makefile >> $LOG/$3.`date +%m%d` 2>&1
	time gmake -C $1 $2 >> $LOG/$3.`date +%m%d` 2>&1 
	rc=$?
	[ $rc -eq 0 ] && echo Remade $2 in $1 \
		     || echo Failed to make $2 in $1

}

[ $# -eq 0 ] && todo=$default

while [ $# -gt 0 ] ; do
	todo="$todo $1"
	shift
done

# Make log dir

mkdirhier $CERN_ROOT/build/log

# Copy the Imakefile and the config for the compiler

cd $CVSCOSRC
cp $CERN/utils/Imakefile $CVSCOSRC/
cp $CERN/utils/linux.cf.ifc $CVSCOSRC/config/linux.cf

# To correct bug in cernlib2002
cd $CVSCOSRC/packlib/kernlib/kerngen
ln -s kerngen/klnx.h klnx.h
cd $CVSCOSRC

# Create the Makefiles 
$CVSCOSRC/config/imake_boot
make bin/kuipc
make tree


for target in $todo  ; do
case $target in 
tree)		build . tree  tree
	;;
scripts)	build scripts install.bin scripts
	;;
packlib)	build packlib install.lib packlib
	;;
pawlib)		build pawlib install.lib  pawlib
	;;
graflib)	build graflib install.lib graflib
	;;
mathlib)	build mathlib install.lib mathlib
	;;
packpgm)	build packlib install.bin packpgm
	;;
pawpgm)		build pawlib install.bin  pawpgm
	;;
grafpgm)	build graflib install.bin  grafpgm
	;;
geant321)	build geant321 install.lib geant321
	;;
mclibs)		build mclibs install.lib mclibs
		build mclibs install.bin mcpgms
	;;
mcpgms)		build mclibs install.bin mcpgms
	;;
phtools)	build phtools install.lib phtools
	;;
patchy)		build patchy install.bin patchy
	;;
*)	echo Ignoring unknown build target $target
	;;
esac
done
