#!/bin/sh
:	'(c) Copyright	Empress Software Inc.	1983, 2003'

if test "$EMPRESSPATH" = "" 
then
	echo "EMPRESSPATH not set"
	exit 1

elif test ! -d $EMPRESSPATH
then
	echo "invalid EMPRESSPATH"
	exit 1
else
	RDBMSPATH=$EMPRESSPATH/rdbms
	export RDBMSPATH
fi

PROGNAME=`$EMPRESSPATH/common/sys_bin/basename $0`
USAGE="*** Usage ***  $PROGNAME  [-gr] [executable]"

. $RDBMSPATH/util_bin/banner

. $EMPRESSPATH/config/options

NLSECHO="$EMPRESSPATH/common/nls/nlsecho"

UE_E0_DIREXIST=10034
UI_E0_COMPILING=10118
UI_E0_ENTEREXE=10116
UI_E0_EXEEXIST=10117
UI_E0_LINKING=10119

GRAPH=
KEEP=
LOWER=$EMPRESSPATH/common/sys_bin/lower

if test -d $EMPRESSPATH/shlib
then
	SHLIB=yes
else
	SHLIB=no
fi
OPTS=

while	:
do
	OPTION=`$LOWER $1`

	case "$OPTION" in
	 -g)
		GRAPH=true
		shift
		;;

	 -r)
		KEEP=true
		shift
		;;

	 -gr|-rg)
		GRAPH=true
		KEEP=true
		shift
		;;

	 -noshlib)
		SHLIB=no
		OPTS="$OPTS $1"
		shift
		;;
	 -usemalloc | -nosbrk)
		OPTS="$OPTS $1"
		shift
		;;
	 -*)
		echo "$USAGE"
		exit 1
		;;
	 *)	break ;;
	esac
done

case $# in
 0)
	$NLSECHO -n "Enter name of executable: " $UI_E0_ENTEREXE
	read EXEC

	case $EXEC in
	 "")
		exit 0;
		;;
	esac
	;;
 1)
	EXEC=$1
	;;
 *)
	echo "$USAGE"
	exit 1
	;;
esac

TMP=empx

if test -d $EXEC
then
	$NLSECHO "*** Directory exists *** '%s'" $UE_E0_DIREXIST $EXEC
	exit 8
fi

if test -f $EXEC
then
	$NLSECHO -n "Executable already exists.  Is it okay to remove it? " $UI_E0_EXEEXIST
	read ANSWER

	case "$ANSWER" in
	 y*|Y*)
		rm -f $EXEC $TMP*.c $TMP*.o
		;;
	 *)
		exit 0
		;;
	esac
fi

LIB=$RDBMSPATH/lib

if test ! -d $LIB
then
	LIB=$RDBMSPATH/src_lib

	AP="
		$LIB/ap.a
		$LIB/ww.a
		$LIB/fded.a
		$LIB/fm.a
		$LIB/fmap.a
		$LIB/apcv.a
		$LIB/tt.a
	"
else
	AP="
		$LIB/ap.a
		$LIB/apcv.a
	"
fi

$RDBMSPATH/exec/emp4util -MSPROGNAME $0 -BLDX "$TMP"

case $? in
 0)
	;;
 *)
	rm -rf $TMP*.c
	exit 1
	;;
esac

$NLSECHO "Compiling ..." $UI_E0_COMPILING

set - $TMP*.c

for J
do
	$EMPRESSPATH/common/sys_bin/cc -c $J
done

$EMPRESSPATH/common/sys_bin/cc -c $TMP.c

CC=$EMPRESSPATH/bin/empcc

case "$GRAPH" in
 true)
	MAIN=apexmain.o
	MAINLIB=$LIB/apmain.a
	case "$MSSYS_4GLGRAPHLIBPATH" in
		"")
			L_PATH=
			;;
		*)
			L_PATH=${MSSYS_CCLIBDIROPT}${MSSYS_4GLGRAPHLIBPATH}
			;;
	esac

	MSCC_EXTRASYSLIBS="$L_PATH $MSSYS_4GLGRAPHLIB"
	export MSCC_EXTRASYSLIBS
	MSCC_EXTRASYSLIBS_WARNING=off
	export MSCC_EXTRASYSLIBS_WARNING

	case $SHLIB in
	   yes)
		case $MSSYS_PLATFORM in
		 SYS_OS_UNIX_AIX)
			LIBS="$LIB/apex.a $AP $LIB/scg.a 
				$LIB/apconfig.a"
			;;
		 *)
			LIBS="$LIB/apex.a $AP $LIB/scg.a $LIB/libusrtabs.a 
				$LIB/stdudf.a $LIB/apconfig.a"
			;;
		esac
		;;
	   *)
		LIBS="$LIB/apex.a $AP $LIB/scg.a $LIB/libusrtabs.a 
			$LIB/stdudf.a $LIB/apconfig.a"
			;;
	esac
	;;
 *)
	MAIN=apexmain.o
	MAINLIB=$LIB/apmain.a

	LIBS="$LIB/apex.a $AP $LIB/apconfig.a $LIB/sc.a"
	;;
esac

$NLSECHO "Linking ..." $UI_E0_LINKING

if $EMPRESSPATH/common/sys_bin/arlib -extract $MAINLIB $MAIN
then
	TMPTARGET="$EXEC.new"

	$CC -o $TMPTARGET $TMP*.o $MAIN $LIBS
	rm $MAIN
	mv $TMPTARGET $EXEC
fi

case "$KEEP" in
 true)
	exit 0
	;;
esac

rm -rf $TMP*.c $TMP*.o
