#!/bin/sh
# simcomp  for Sun-4 Solaris 2.4 with Motif 
# simcomp (=simld=simgld=simgld1=simgld2)    - for dynamic linking
#         (=tsimld=tsimgld=tsimgld1=tsimgld2)- for static linking
#
# C language interface to the SIMSCRIPT compiler.
#
#-------------------------------------------------------------------
prog=`basename $0`
STANDARD_INSTALL=y

INST_INC="-I$SIMHOME/include"
INST_LIB="-L$SIMHOME/lib"
RELEASE='2.0'
WINSYS='Motif'
SG1_INST=y
SG2_INST=y

#
#
#-- check if SIMHOME is used and defined
if [ "$STANDARD_INSTALL" = "y"  -a  "$SIMHOME" = "" ]; then
  echo "$prog ERROR: Env. var SIMHOME undefined. Cannot run. Aborted."
  exit 1
fi
#

if [ $# -eq 0  ]; then
  if [ "$prog" = "simcomp" ]; then
    echo "simcomp for SIMSCRIPT II.5, release $RELEASE for $WINSYS"
  else
    echo "Usage: $prog object_files ..."
    echo 
    echo "  Link SIMSCRIPT object files to executable. "
  fi
  echo
  exit 0
fi
#
#
#---- make simgld2 be the default SIMGRAPHICS ----
if [ "$prog" = simgld ]; then
  echo "simgld: *** SIMGRAPHICS II is now default SIMGRAPHICS ..."
  prog=simgld2
fi

#---- check if the selected SIMGRAPHICS was installed ----
if [ "$prog" = simgld2  -a  "$SG1_INST" = n ]; then
  echo "simgld2: *** SIMGRAPHICS II not installed. Using SIMGRAPHICS I ..."
  prog=simgld1
fi
if [ "$prog" = simgld1  -a  "$SG2_INST" = n ]; then
  echo "simgld1: *** SIMGRAPHICS I not installed. Using SIMGRAPHICS II ..."
  prog=simgld2
fi
#--- static link (total link) ----
if [ "$prog" = tsimgld ]; then
  echo "tsimgld: *** SIMGRAPHICS II is now default SIMGRAPHICS ..."
  prog=tsimgld2
fi
if [ "$prog" = tsimgld2  -a  "$SG2_INST" = n ]; then
  echo "tsimgld2: *** SIMGRAPHICS II not installed. Using SIMGRAPHICS I ..."
  prog=tsimgld1
fi
if [ "$prog" = tsimgld1  -a  "$SG1_INST" = n ]; then
  echo "tsimgld1: *** SIMGRAPHICS I not installed. Using SIMGRAPHICS II ..."
  prog=tsimgld2
fi


#--------- libraries -----------
sg1libs="-lsimg1 -lSIL1 -lX11"
tsg1libs="-Bstatic -lsimg1 -lSIL1 -Bdynamic -lX11"

sg2libs="-lsimg2 -lSGCI -lSG2_S -lModSim_S -lModSimS_S -lSIL_S -lXm -lXt -lX11"
tsg2libs="$SIMHOME/lib/libsimg2.a -Bstatic -lSGCI -lSG2_S -lModSim_S -lModSimS_S -lSIL_S -Bdynamic -lXm -lXt -lX11"

simlib="-lsim1 -lsims -lsim1 -lsims"
tsimlib="$SIMHOME/lib/libsim.a"
#tsimlib="lsim"

#simlib="-lsim"
#mlib="-lsunmath -lm"
mlib="-lm"

#--------- handle special options -----------
for i in $*; do
    case $i in
    -p|-pg)  tsimlib="$SIMHOME/lib/libsim_p.a /usr/lib/libdl.so"
	     ;;
    esac
done

#---- select libraries according to SG1/2 and dynamic/total(static)--------
#-- dynamic link is default, sim, simg1 and simg2 are shipped only static
#-- always link system and window libs dynamic

if [ "$prog" = "simgld2" ]; then
    libs="$sg2libs $simlib $winlibs $mlib"

elif [ "$prog" = "tsimgld2" ]; then
    libs="$tsg2libs $tsimlib $winlibs $mlib"

elif [ "$prog" = "simgld1" ]; then
    libs="$sg1libs $simlib $sg1libs $winlibs $mlib"

elif [ "$prog" = "tsimgld1" ]; then
    libs="$tsg1libs $tsimlib $tsg1libs $winlibs $mlib"

elif [ "$prog" = "tsimld" ]; then
    libs="$tsimlib  $mlib"

elif [ "$prog" = "simld" ]; then
    libs="$simlib $mlib"

else
    libs="$simlib $mlib"

fi

#libs="$libs -lsocket -lX"
#libs="$libs -lsocket -lX /usr/ucblib/libucb.a"
libs="$libs -lsocket -lX -lucb"

#
# for dynamic link (sun) we must make sure that the SET_R, TIMO_R, and simain
# are in the static part of the executable (pulled out of sharable).
# simain, main_dbginit and preamble_dbginit are there to make sure all
# the user code gets loaded when user objects are in library.
#
# -Dsolar is needed to distinguish between Solaris and sun
# -R... is needed to tell the loader where to find the SIMSCRIPT and
# usercode DLLs (for CMR)
#
CWD=`pwd`
LDDEFS="-u simain -u preamble_dbginit -u HSET_R -u HTIM0_R -R$SIMHOME/lib:$CWD"
#exec cc -g -fnonstd -Dsolar -Xs -w -t -KPIC  $LDDEFS $INST_INC $INST_LIB $* $libs

if [ "$prog" = "simgld2" ]; then
  exec CC -w -t $LDDEFS $INST_INC $INST_LIB $* $libs
elif [ "$prog" = "tsimgld2" ]; then
  exec CC -w -t $LDDEFS $INST_INC $INST_LIB $* $libs
else
  exec cc -Dsolar -Xs -w -t  $LDDEFS $INST_INC $INST_LIB $* $libs
fi

#
# warning -- this statement is never reached due to previous 'exec'
#
echo "'exec' of cc compiler/linker failed. System error."
exit 1

