#!/bin/sh
# ugproject
if test "$1" = "-help"
then
	echo "usage: $0 [options]";
	echo "purpose: compile all ug libraries and applications";
	echo "Options are";
	echo " ug2, ug3, appl2, appl3 "
	exit 1;
fi

if test "x$UGROOT" = "x"
then
	echo "$0: to use $0 set shell environment variable UGROOT!";
	exit 1;
fi

EXECUTABLES=""

ug2 () 
{
	ugconf 2
	ugmake ug uglib
	ugmake dom
}

ug3 () 
{
	ugconf 3
	ugmake ug uglib
	ugmake dom
}

appl2 () 
{
    if test -d $UGROOT/../simple
	then
		ugmake simple
	fi
	if test -x $UGROOT/../simple/simple
	then
		EXECUTABLES="$EXECUTABLES simple"
	fi

    if test -d $UGROOT/../diff2d
	then
		ugmake diff2da
	fi
	if test -x $UGROOT/../diff2d/appl/diff2da
	then
		EXECUTABLES="$EXECUTABLES diff2da"
	fi

    if test -d $UGROOT/../fe
	then
		ugmake fe
	fi
	if test -x $UGROOT/../fe/appl/fe2d
	then
		EXECUTABLES="$EXECUTABLES fe2d"
	fi

    if test -d $UGROOT/../ns
	then
		ugmake ns
		ugmake ns2d
	fi
	if test -x $UGROOT/../ns/appl2d/ns2d
	then
		EXECUTABLES="$EXECUTABLES ns2d"
	fi

    if test -d $UGROOT/../sc
	then
		ugmake sc2d
	fi
	if test -x $UGROOT/../sc/appl2d/sc2d
	then
		EXECUTABLES="$EXECUTABLES sc2d"
	fi

    if test -d $UGROOT/../df
	then
	    ugmake dom LGM
		ugconf LGM_DOMAIN
		ugmake df
	    ugmake dom STD
		ugconf STD_DOMAIN
	fi
}

appl3 () 
{
    if test -d $UGROOT/../fe
	then
		ugmake fe
	fi
	if test -x $UGROOT/../fe/appl/fe3d
	then
		EXECUTABLES="$EXECUTABLES fe3d"
	fi

    if test -d $UGROOT/../sc
	then
		ugmake sc3d
	fi
	if test -x $UGROOT/../sc/appl3d/sc3d
	then
		EXECUTABLES="$EXECUTABLES sc3d"
	fi

    if test -d $UGROOT/../ns
	then
		ugmake ns3d
	fi
	if test -x $UGROOT/../ns/appl3d/ns3d
	then
		EXECUTABLES="$EXECUTABLES ns3d"
	fi
}

if test "$1" = "ug2"
then
	ug2;
	exit 0;
fi

if test "$1" = "ug3"
then
	ug3;
	exit 0;
fi

if test "$1" = "appl2"
then
	appl2;
	echo $EXECUTABLES 'created'
	exit 0;
fi

if test "$1" = "appl3"
then
	appl3;
	echo $EXECUTABLES 'created'
	exit 0;
fi

# default

# save ug.conf
cp $UGROOT/ug.conf $UGROOT/ug.conf.tmp

ugclean all
ugconf 2
ugconf NONETGEN
ug2;
ugmake dev
appl2;

ugconf 3
if test "$1" = "netgen"
then
    ugconf NETGEN
fi

# clean 2d opjects

ugmake ug -i ifdef
if test -d $UGROOT/../fe
then
	ugmake fe -i ifdef
fi
if test -d $UGROOT/../sc
then
	ugmake sc -i clean
fi
if test -d $UGROOT/../ns
then
	ugmake ns -i clean
fi
if test -d $UGROOT/../df
then
	ugmake df -i clean
fi

ug3;
appl3;

# restore ug.conf
mv $UGROOT/ug.conf.tmp $UGROOT/ug.conf

ugmake tools
ugmake ug -i ifdef

if test "x$EXECUTABLES" = "x"
then
	echo "$0: no executable created"
	exit 1;
fi

echo $EXECUTABLES 'created'
