#!/bin/bash
#	g4bl - run g4beamline using the distributed libraries
#
#	This script must be run via an absolute pathname, using the 
#	bin directory of the distribution. That implies that the
#	g4beamline-$VERSION/bin directory should be put into your PATH.
#
#	If G4BL_DIR is set in the environment, then it is used instead of 
#	the above.
#
#	if G4BEAMLINE is set in the environment, it is the user-built
#	executable to run.
#
#	G4BL_DIR is used to find the runtime libraries, the physics data 
#	directories, and viewer.def.

# find G4BL_DIR
if test "$G4BL_DIR" = ""
then
	G4BL_DIR=`dirname "$0"`
	G4BL_DIR=`\cd "$G4BL_DIR" >&- 2>&- && pwd`
	G4BL_DIR=`dirname "$G4BL_DIR"`
	if test -x /bin/cygpath
	then
		G4BL_DIR=`/bin/cygpath -m "$G4BL_DIR"`
	fi
fi
export G4BL_DIR
echo G4BL_DIR=$G4BL_DIR

# check for G4BEAMLINE set
if test -z "$G4BEAMLINE"
then
	G4BEAMLINE="$G4BL_DIR/bin/g4beamline"
else
	echo G4BEAMLINE=$G4BEAMLINE
fi

# set ROOTSYS if not already set
if test -z "$ROOTSYS"
then
	export ROOTSYS=''
fi

# get physics datasets into the environment
if ! source $G4BL_DIR/bin/g4bldata --setenv
then
	echo "Running 'g4bldata --install' to install the data."
	$G4BL_DIR/bin/g4bldata --install
	source $G4BL_DIR/bin/g4bldata --setenv
fi

# get library search paths
case `"$G4BL_DIR/bin/g4bl-config" --sys` in
Linux)
	export LD_LIBRARY_PATH="$G4BL_DIR/lib:$LD_LIBRARY_PATH"
	echo LD_LIBRARY_PATH="$LD_LIBRARY_PATH"
	;;
Darwin)
	export DYLD_LIBRARY_PATH="$G4BL_DIR/lib:$DYLD_LIBRARY_PATH"
	echo DYLD_LIBRARY_PATH="$DYLD_LIBRARY_PATH"
	;;
WIN32)
	# need in PATH for .dll-s
	export PATH="$PATH:$G4BL_DIR/bin"
	;;
esac

# execute g4beamline
exec "$G4BEAMLINE" "$@"