#!/bin/sh
#################################################################
#
#  *****  creates starting script for RESTRAX  *****
#
#  arguments are 
#  1) installation directory
#  2) filename of executable file"
#  3) path  to PGPLOT library
#
##################################################################


if [ "x$1" = "x" -o "x$2" = "x" -o "x$3" = "x" ]; then 
	echo "$0: Some arguments are missing:"
	echo 
	echo " 1) installation directory"
	echo " 2) filename of executable file"
	echo " 3) path  to PGPLOT library "
	echo "    (use 'auto' if PGPLOT is installed in default paths) "
 	exit
fi

# Name of the output:
T=$1/restrax_run

cat > $T << \EOD
#!/bin/sh
##################################################################
#  *****  script for starting RESTRAX  *****
#
# RESTRAX can be started by calling the executable directly, but this 
# script helps to set properly the environment and command-line options.
# Look also in the initialization file "restrax.ini", which should be
# found in the <cfgfiles> directory.
# 
# Syntax: restrax_run <cfg> [<options>]
# where 
# <cfgfiles> ... absolute path to the directory with instrument configurations 
# <options>  ... additional command line options   
###################################################################

#------------------------------------------------------------------
# Edit following to match your local environment
#------------------------------------------------------------------
# path to RESTRAX installation

EOD
echo "INSTDIR=$1" >> $T
echo "RESTRAX=$1/bin/$2" >> $T
if [ -d $3 ] ; then
	echo "PG=$3" >> $T
else
	echo "PG=''" >> $T 
fi

cat >> $T << \EOH

# Absolute path to the RESTRAX configurations and/or lookup tables:
CFGFILES=$1

# path to your EXCILIB library (fitting model)
# This path will be searched if EXCILIB is not found 
# in the current directory
EXCI=.

# start with this EXCI library
EXCILIB=$2

# string with command line options
STDOPT=$3

#------------------------------------------------------------------
# search for PGPLOT
#-------------------------------------------------------------------

PGPATH="$PG $PGPLOT_DIR /usr/local/pgplot /usr/local/lib "
PGPLOT=""
for F in $PGPATH
do
	if test -f $F/grfont.dat ; then
		PGPLOT=$F
		echo "PGPLOT found at $PGPLOT"
		break
	fi
done

PGPLOT_DIR=$PGPLOT
PGPLOT_FONT=$PGPLOT/grfont.dat
# your printer command
if test $PRINTER ; then 
	PGPLOT_ILL_PRINT_CMD="lp -d$PRINTER ";
else
	PGPLOT_ILL_PRINT_CMD="lpr ";
fi

# default PGPLOT device
PGPLOT_DEV=/xserv

if test -z $PGPLOT ; then
	echo "Cannot find PGPLOT!"
	echo "continue? [y/n]"
	read ANSW
	if [ $ANSW != "y" ] ; then
		exit
	else
		echo "PGPLOT output redirected to /NULL"
		PGPLOT_DEV="/NULL"	
	fi
fi

export PGPLOT_DIR PGPLOT_DEV PGPLOT_FONT PGPLOT_ILL_PRINT_CMD

#------------------------------------------------------------------
# There is nothing to modify below
#-------------------------------------------------------------------

# path to link shared libraries (for lib_res_exci.so)
# there are 2 versions for Unix and HP_UX
LD_LIBRARY_PATH=.:$EXCI:./lib:$INSTDIR/lib:$PGPLOT_DIR:$LD_LIBRARY_PATH
SHLIB_PATH=.:$EXCI:./lib:$INSTDIR/lib:$PGPLOT_DIR:$SHLIB_PATH
export LD_LIBRARY_PATH SHLIB_PATH

# start RESTRAX

$RESTRAX "-dir=$CFGFILES" "-exci=$EXCILIB" "$STDOPT"

EOH

chmod 755 $T

