if test -r /tmp/build_cg_$1
then
   echo Ci hanno pensato prima di te.
   exit 255
fi

btouch

touch /tmp/build_cg_$1
trap "rm /tmp/build_cg_$1;echo ok, basta;exit" 2
CCOMP=cc++
RCOMP=rescomp
MCOMP=mskcomp
LINK=linkdue
IP=$SRC_HOME/include/
if  [ "$EXEHOME" = "" ] ;
then
  EXEHOME=$P2HOME
fi
EP=$EXEHOME/campo/
LIBC="$P2HOME/lib/libdue.a $P2HOME/lib/libuno.a $P2HOME/lib/libgfm.a"
LIBD1="$P2HOME/lib/libdued.a $P2HOME/lib/libuno.a $P2HOME/lib/libgfm.a"
LIBD2="$P2HOME/lib/libdued.a $P2HOME/lib/libunod.a $P2HOME/lib/libgfm.a"
LIBD3="$P2HOME/lib/libdued.a $P2HOME/lib/libunod.a $P2HOME/lib/libgfmd.a"
DIR=`pwd | grep -v $P2HOME`
if [ "$DIR" != "" ];
then
	LIBC=
	LIBD1=
	LIBD2=
	LIBD3=
	if [ -f $SRC_HOME/lib/libdue.a ];
	then
		LIBC=$LIBC"$SRC_HOME"
	else
		LIBC=$LIBC"$P2HOME"
	fi
	if [ -f $SRC_HOME/lib/libdued.a ];
	then
		LIBD1=$LIBD1"$SRC_HOME"
		LIBD2=$LIBD2"$SRC_HOME"
		LIBD3=$LIBD3"$SRC_HOME"
	else
		LIBD1=$LIBD1"$P2HOME"
		LIBD2=$LIBD2"$P2HOME"
		LIBD3=$LIBD3"$P2HOME"
	fi
	LIBC=$LIBC"/lib/libdue.a "
	LIBD1=$LIBD1"/lib/libdued.a "
	LIBD2=$LIBD2"/lib/libdued.a "
	LIBD3=$LIBD3"/lib/libdued.a "
	if [ -f $SRC_HOME/lib/libuno.a ];
	then
		LIBC=$LIB"$SRC_HOME"
		LIBD1=$LIBD1"$SRC_HOME"
	else
		LIBC=$LIB"$P2HOME"
		LIBD1=$LIBD1"$P2HOME"
	fi
	if [ -f $SRC_HOME/lib/libunod.a ];
	then
		LIBD2=$LIBD2"$SRC_HOME"
		LIBD3=$LIBD3"$SRC_HOME"
	else
		LIBD2=$LIBD2"$P2HOME"
		LIBD3=$LIBD3"$P2HOME"
	fi
	LIBC=$LIBC"/lib/libuno.a "
	LIBD1=$LIBD1"/lib/libuno.a "
	LIBD2=$LIBD2"/lib/libunod.a "
	LIBD3=$LIBD3"/lib/libunod.a "
	if [ -f $SRC_HOME/lib/libgfm.a ];
	then
		LIBC=$LIBC"$SRC_HOME"
		LIBD1=$LIBD1"$SRC_HOME"
		LIBD2=$LIBD2"$SRC_HOME"
	else
		LIBC=$LIBC"$P2HOME"
		LIBD1=$LIBD1"$P2HOME"
		LIBD2=$LIBD2"$P2HOME"
	fi
	if [ -f $SRC_HOME/lib/libgfmd.a ];
	then
		LIBD3=$LIBD3"$SRC_HOME"
	else
		LIBD3=$LIBD3"$P2HOME"
	fi
	LIBC=$LIBC"/lib/libgfm.a "
	LIBD1=$LIBD1"/lib/libgfm.a "
	LIBD3=$LIBD2"/lib/libgfm.a "
	LIBD3=$LIBD3"/lib/libgfmd.a "
fi
CFLAGSC="-r -c "
CFLAGSD="-r -p "
LFLAGSC="-r "
LFLAGSD1="-r -d1 "
LFLAGSD2="-r -d2 "
LFLAGSD2="-r -d3 "
O=$SRC_HOME/objs
export CCOMP LINK IP EP LIB O CFLAGS LFLAGS RCOMP MCOMP LFLAGS2
export CCOMP LINK IP EP LIBC LIBD1 LIBD2 LIBD3 O CFLAGSD LFLAGSC RCOMP MCOMP LFLAGSD1 LFLAGSD2 LFLAGSD3 CFLAGSC
FLAGS=
DASH=`echo $1 | cut -c1 `
while [ "$DASH" = "-" ];
do
	FLAGS=$FLAGS" "$1
	shift
	DASH=`echo $1 | cut -c1 `
done
if [ "$1" = "" -o "$1" = "etc" ];
then
	gmake $FLAGS $1
else
	gmake $FLAGS $EP$1
fi
rm /tmp/build_cg_$1
