#!/bin/sh
# $Id$

# TODO:
#
# - makeoptions _only_ work when exactly one ugmodule-option is
#   passed. That's not very nice if no module is passed or if the
#   (undocumented!) mode with two args is used. [Thimo 30.8.2004]

# terminate script if any subcommands fail. This implicitly passes
# make-errors to the outside
set -e

if test "$1" = "-help"
then
	echo "usage: $0 [<ugmodule>] [makeoptions]";
	echo "purpose: compile by calling \"make\" with the right Makefile[.<ugmodule>]";
	echo "Options are";
	echo " <ugmodule>: the module which you want to compile";
	echo "             where <ugmodule> is one of ug, dev, meta, xif, gm";
	echo "             graph, low, numerics, ui, gg2, gg3, dom, std, netgen";
	echo "             diff2d, diff2da,cd, cda, fem, fema, ns2d, ns2da, simple";
	echo "             scalar, scalar2d, scalar3d, tools";
	echo " <makeoptions>: the options which you want to pass to \"make\"";
	exit 1;
fi

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

# search for the correct Makefile in current dir
get_makefile () {
    PWD=`pwd`
    DIRNAME=`basename $PWD`
    # according to UG-policy the suffix should be the name of the directory
    if test -e Makefile.$DIRNAME ; then
	MAKEFILE="Makefile.$DIRNAME"
    else
	# first fallback: if "Makefile" exists, use that needed for
	# $UGROOT/Makefile the applications and for upward
	# compatibility
	if test -e Makefile ; then
	    MAKEFILE=Makefile
	else
	    # last fallback: use the single "Makefile*"
	    if test `ls Makefile*|wc -l` -eq 1 ; then
		MAKEFILE=`ls Makefile*`
	    else
		echo "$0: Makefile not found or not unique!"; 
		exit 1
	    fi
	fi
    fi
}

# if no parameter is specified make in current directory
if test $# -eq 0 
then
    get_makefile
    make -f $MAKEFILE
fi

if test $# -eq 2 
then
	if test "$2" = "appl"
	then
	    ugmake $1;
	    ugmake;
		exit 0;
	fi
fi

# if parameter are specified make the specified module
if test $# -ge 1 
then
	# check whether first  option is ug module 
	case $1 in
	ug)
		if test -d $UGROOT 
		then
			cd $UGROOT;
		else
			exit 1;
		fi
		;;
	dev | dom | gm | graphics | low | numerics | parallel | ui | np)
		if test -d $UGROOT/$1
		then
			cd $UGROOT/$1;
		else
			exit 1;
		fi
		;;
	meta | xif | ps | sif)
		if test -d $UGROOT/dev/$1
		then
			cd $UGROOT/dev/$1;
		else
			exit 1;
		fi
		;;
	udm | algebra | procs | field)
                if test -d $UGROOT/np/$1
                then
                        cd $UGROOT/np/$1;
                else
                        exit 1;
                fi
		;;
	std)
                if test -d $UGROOT/dom/$1
                then
                        cd $UGROOT/dom/$1;
                else
                        exit 1;
                fi
		;;
	gg2 | gg3)
                if test -d $UGROOT/gm/$1
                then
                        cd $UGROOT/gm/$1;
                else
                        exit 1;
                fi
		;;
	netgen)
                if test -d $UGROOT/gm/gg3/$1
                then
                        cd $UGROOT/gm/gg3/$1;
                else
                        exit 1;
                fi
		;;
	uggraph | grape)
                if test -d $UGROOT/graphics/$1
                then
                        cd $UGROOT/graphics/$1;
                else
                        exit 1;
                fi
		;;
	dddif)
                if test -d $UGROOT/parallel/$1
                then
                        cd $UGROOT/parallel/$1;
                else
                        exit 1;
                fi
		;;
	ddd)
                if test -d $UGROOT/parallel/dddobj
                then
                        cd $UGROOT/parallel/dddobj; 
                else
                        exit 1;
                fi
		;;
	chaco)
                if test -d $UGROOT/parallel/chaco
                then
                        cd $UGROOT/parallel/chaco;
                else
                        exit 1;
                fi
		;;
	links)
		ugmakelinks;
		exit 0;
		;;
	man)
		ugmakeman;
		exit 0;
		;;
	simple)
                if test -d $UGROOT/../simple
                then
                        cd $UGROOT/../simple;
                else
                        exit 1;
                fi
		;;
	diff2d)
                if test -d $UGROOT/../diff2d/pclib
                then
                        cd $UGROOT/../diff2d/pclib;
                else
                        exit 1;
                fi
		;;
	diff2da)
                if test -d $UGROOT/../diff2d/appl
                then
                        cd $UGROOT/../diff2d/appl;
                else
                        exit 1;
                fi
		;;
	ns)
                if test -d $UGROOT/../ns/pclib
                then
                        cd $UGROOT/../ns/pclib;
                else
                        exit 1;
                fi
		;;
	ns2d)
                if test -d $UGROOT/../ns/appl2d
                then
                        cd $UGROOT/../ns/appl2d;
                else
                        exit 1;
                fi
		;;
	ns3d)
                if test -d $UGROOT/../ns/appl3d
                then
                        cd $UGROOT/../ns/appl3d;
                else
                        exit 1;
                fi
		;;
	fe)
                if test -d $UGROOT/../fe/appl
                then
                        cd $UGROOT/../fe/appl;
                else
                        exit 1;
                fi
		;;
	sc)
                if test -d $UGROOT/../sc/pclib
                then
                        cd $UGROOT/../sc/pclib;
                else
                        exit 1;
                fi
		;;
	sc2d)
                if test -d $UGROOT/../sc/appl2d
                then
                        cd $UGROOT/../sc/appl2d;
                else
                        exit 1;
                fi
		;;
	sc3d)
                if test -d $UGROOT/../sc/appl3d
                then
                        cd $UGROOT/../sc/appl3d;
                else
                        exit 1;
                fi
		;;
	tools)
                if test -d $UGROOT/tools 
                then
                        cd $UGROOT/tools; 
                else
                        exit 1;
                fi
		;;
	df)
                if test -d $UGROOT/../df/gen
                then
                        cd $UGROOT/../df/gen;
                else
                        exit 1;
                fi
		;;
	*)
		echo "target $1 not found";
		exit 1;
		;;
	esac;

	# call make with the right makefile
	get_makefile
	shift
	make -f $MAKEFILE $*
fi

exit 0;
