#!/bin/bash
#
#*******************************************
#
#@(#) GXINT   link (and run) interactive GEANT.
#@(#) $Header: /afs/cern.ch/asis/share/cern/CVS/scripts/gxint,v 1.3 2005/04/18 15:23:13 mclareni Exp $
#
# Author:   G.Folger   92/02/25
#
#  Modifications:
#      8 June 93  fix @ to + for patchy
#     Jan/Feb 94  use cernlib, adapt to geant316
#      7 Apr 94   correct for Ultrix ( no set -- in sh),
#                 add ; in -o option handling
#     following mods are kept in the RCS log
#
#*******************************************
line="******************************************************"
 
print_help() {	
cat << EoD

 GXINT [options] file(s)
  ld an interactive Geant application and run it. Your compiled
  routines are given in file(s).
  options:
 
  -d driver
        specify the driver for the graphics output. Possible
        values are X11, Motif, GKS, or GDDM. Not all are available
        on all systems. X11 is the default.
  -v version
        version of the program library to use. Possible values
        include pro, new and old. This option only makes sense if you
	have installed a non-Debian version of Cernlib. In that case
	you must have defined the environment variable \$CERN.
  -g geant_version
        which version of geant to use. Current default is 321. This
	also makes sense only if you have installed a non-Debian version
	of Cernlib.
  -h host
        useful with the -d X11 option only. Specify the hostname
        used for the display. Syntax identical to that of DISPLAY.
        If no hostname is specified, the value of DISPLAY is used.
  -o outputfile
        Do not immediately run GEANT, but produce an executable
        file named outputfile.
  -L path
        additional loader search path. Useful if additional libraries
        are specified with -l option. This option may be repeated to
        more than one pathname.
  -l library
        specify additional libraries. This option may be repeated to
        add several libs.
  -m    do NOT use main program from cernlib area.

EoD
}

 
if [ $# -eq 0 ] ; then 
	print_help
fi

if [ -f /usr/bin/uname ] ; then
	system=`/usr/bin/uname`
	if [ "$system" = "ULTRIX" -a -z "$sh5" ] ; then
		sh5="yes"
		export sh5
		/bin/sh5 $0 $*
		exit
	fi
fi

 SHOPT="d:v:g:h:o:L:l:m"
# Check option set and give default values
  if [ -f /bin/getopt -o -f /usr/bin/getopt ] ; then
     set -- `getopt $SHOPT $*` ; cc=$?
     if [ $cc -ne 0 ] ; then
	echo ""
	echo "Usage:"
	print_help ; exit 0
     fi
  fi

if [ -z "$TMPDIR" ] || [ "$TMPDIR" = "/tmp" ] ; then
  TMPDIR="$HOME"
fi
drv="/X11" ; ver="" ; OUT="$TMPDIR/GEANT$$"
gxint=""
 
while [ $# -gt 0 ]
do case $1 in
     -d) drv="/$2";                  shift ;;
     -v) ver=$2;                     shift ;;
     -g) gvs=$2;                     shift ;;
     -h) DISPLAY=$2;export DISPLAY;  shift ;;
     -o) OUT=$2; norun="1";          shift ;;
     -L) userpath="$userpath -L$2";  shift ;;
     -l) userlibs="$userlibs -l$2";  shift ;;
     -m) _m="1"				   ;;
     --)                     shift ; break ;;
     --help|-\?) print_help ; exit 0	   ;;
      *)                             break ;;
   esac
   shift
done
 
if [ $# -gt 0 ] ; then
   pgm="$@"
 else
   echo "gxint_Warning: no user program specified"
fi
 
 
[ -z "$CERN" ] && export CERN="/usr"
 
if [ "$drv" = "/GKS" ] ; then
  if [ -z "$GKSR" ] ; then
    if [ ! -d $CERN/gks/$ver ] ; then
      echo "gxint_Error: shell variable GKSR must be set or "
      echo "           :  $CERN/gks/$ver must exist"
      exit 1
    fi
    GKSR=$CERN/gks/$ver
  fi
  GKSP=$GKSR/lib
fi
 
  LDN="f77"
  _o="o"

  system="x"
  [ -x /bin/uname ]        && system=`/bin/uname`
  [ -x /usr/bin/uname ]    && system=`/usr/bin/uname`
  [ -x /sys5.3/bin/uname ] && system=`/sys5.3/bin/uname`

case $system in
	AIX)		LDN="xlf"   ;;
	ConvexOS)       LDN="fc"    ;;
	DomainOS)	LDN="ld"    ;_o="bin"  ;;
	HP-UX)		LDN="fort77";;
esac
 
CLIB="geant${gvs} pawlib graflib$drv packlib mathlib"
 
[ -z "$_m" ] && gxint="${CERN}/${ver}/lib/gxint${gvs}.$_o" 
[ "$CERN" = "/usr" -a -z "$ver" ] && gxint=/usr/share/geant321/gxint.f
 
cat << EoD
$line
* loading interactive Geant
*   creating          : $OUT
*   loader used       : $LDN
*   user library path : $userpath
*   user libraries    : $userlibs
*   Cern libraries    : $CLIB
*   gxint used        : ${gxint:-user supplied}
$line
EoD

[ -z "$ver" ] && verflag="" || verflag="-v"
$LDN -o $OUT $gxint $pgm $userpath $userlibs `cernlib $verflag $ver $CLIB` ; \
cc=$?
 
[ $cc -ne 0 ] && exit $cc
 
if [ ${norun-0} -eq 0 ] ; then
 $OUT ; cc=$?
fi
exit $cc
