#! /bin/bash
## File:        babel
## Package:     Babel binary
## Copyright:   (c) 2000-2007 Lawrence Livermore National Security, LLC
## Revision:    $Revision: 6212 $
## Modified:    $Date: 2007-10-31 17:07:17 -0700 (Wed, 31 Oct 2007) $
## Description: main babel run script
##

# 
# This whole script exists to hide the fact that the babel
# compiler is written in java.  It launches the babel-x.x.x.jar
# file with a java virtual machine, sets the classpaths, and 
# usually also adds the default repository (unless specifically excluded
# by a commandline option).
# 

#
# Settings determined at configure time.
#
VERSION="1.4.0"
PACKAGE="babel"
prefix="/usr"
exec_prefix="${prefix}"
datarootdir="${prefix}/share"
datadir="${datarootdir}"
bindir="${exec_prefix}/bin"
JAVA="/usr/bin/java"
TEST_EF="/usr/bin/test"
abs_top_builddir="/build/buildd/babel-1.4.0.dfsg"
python="python"

samefile() 
{
  # returns 0 if first two arguments are files with the same inode
  # returns 1 if less than two arguments, or either are not found, 
  #           or they are not the same inode
  if test "$TEST_EF" = "None"; then
    if $verbose; then 
      echo "samefile() using $python"
    fi
    if test -e "$1"; then 
      if test -e "$2"; then 
        return `$python -c "import os.path; print int( not os.path.samefile(\"$1\",\"$2\"))"`
      fi
    fi
    return 1;
  else
    if $verbose; then 
      echo "samefile() using $TEST_EF";
    fi
    $TEST_EF "$1" -ef "$2"
    return $?
  fi;
}

#
# Remove ./path and path1/./path2 from non-arguments for AIX JVM.  
# Also detect if we are debugging or need extra verbiage.
#
args=""
debug=false;
verbose=false;
quiet=true;
default_repo=true;
override_installpath=false;
for a in $@; do
  case "$a" in
    --debug) debug=true; a=;;
    --verbose) verbose=true; quiet=false;;
    --noquiet) quiet=false; a=;;
    --override-installpath) override_installpath=true; a=;;
    -*) ;;
    *) a=`echo $a | sed -e 's,^\./,,g' -e 's,/\./,/,g'`;;
  esac
  args="$args $a"
done

#
# make sure user's environment isn't incompatible with installation
#
THIS_JAVA="${JAVA}"
THIS_JDB="jdb"
base_java=`basename "${JAVA}"`
my_java=`which "$base_java"`
java_found=$?
if test "x${my_java}" != "x${THIS_JAVA}"; then
  if samefile ${my_java} ${THIS_JAVA}; then 
    :
  else
    if test "$override_installpath" = "false"; then
      echo "warning: Babel configured for ${JAVA},"
      echo "         your environment set up for ${my_java}."
      echo "         Babel will use what it was configured for,"
      echo "         You can override with the --override-installpath option"
    else
      THIS_JAVA="$JAVA"
    fi;
  fi;
fi; 

#
# Test if we are running under cywin.
#
case "`uname`" in
  CYGWIN*) cygwin=true;;
  *) cygwin=false;;
esac


#
# Now we need to determine where the jar-files are.
#

#test if $0 is the same inode as where babel script is installed.
path2me=$0
myname=babel
SYSJARDIR=/usr/share/java
is_installed="false"
if samefile ${bindir}/${myname} $path2me; then 
  is_installed="true"
  BABELJARDIR="${datadir}/java"
  if $verbose; then 
     echo "Just use paths determined at configure time to find jar files..."
  fi
else
  if $verbose; then 
     echo "Make a best effort to chase down symbolic links to find the jar files..."
  fi
  # 
  PWD_SAVE=`pwd`
  done="false"
  while test $done = "false"; do
    if $verbose; then 
       echo "loop: path2me = "$path2me
    fi
    # first chase down symbolic links in directories
    cd `dirname $path2me`                      # cd to the directory where I exist
    mydir=`pwd`
    myname=`basename $path2me`
    newpath2me="$mydir/$myname"                # get new path2me from current working directory
    # next chase down symbolic links in files
    for i in `ls -l $newpath2me`; do
      :
    done
    if test "$i" = "$path2me"; then
      done="true"
    fi
    path2me=$i
  done
  mypath=`dirname $path2me`
  cd "$mypath/.."
  exec_prefix=`pwd`
  if test -e ${exec_prefix}/lib/${PACKAGE}-${VERSION}.jar; then
    prefix=${exec_prefix}
  elif test -e ${exec_prefix}/../lib/${PACKAGE}-${VERSION}.jar; then
    prefix=`(cd ${exec_prefix} && pwd)`
  else
    echo "ERROR: $0 " >&2
    echo "    This script to launch Babel is not located in installation directory" >&2 
    echo "    (as set by configure).  It tried to find the required JAR files based" >&2
    echo "    its the location (chasing down links, if necessary), but failed." >&2
    echo "    Has this script been separated from the rest of the Babel package?" >&2
    exit -1 
  fi 
  cd $PWD_SAVE
  BABELJARDIR="${prefix}/lib"
fi
if $verbose; then 
  echo "  prefix=\"${prefix}\""
  echo "  exec_prefix=\"${exec_prefix}\""
  echo "  is_installed=\"${is_installed}\""
fi

#
# Set the CLASSPATH based on the BABEL home directory
#
if test -e ${BABELJARDIR}/${PACKAGE}-${VERSION}.jar; then
  if $verbose; then
    echo "Looking for jar files in \"${BABELJARDIR}\"...";
  fi
else 
  echo "ERROR: $0 " >&2
  echo "   Expected to find ${BABELJARDIR}/${PACKAGE}-${VERSION}.jar" >&2
  exit 1
fi

BABELCP=${BABELJARDIR}/${PACKAGE}-${VERSION}.jar:${SYSJARDIR}/gnu-getopt.jar:${SYSJARDIR}/xercesImpl.jar:${SYSJARDIR}/xmlParserAPIs.jar:${SYSJARDIR}/xml-apis.jar:${SYSJARDIR}/xalan2.jar
if test "X$CLASSPATH" = "X"; then
  CLASSPATH=${BABELCP}
else
  CLASSPATH=${BABELCP}:${CLASSPATH}
fi
unset BABELCP

#
# If the SIDL repository exists, then add it to the default repository path.
#

REPOSITORY=
REPOSITORY_PATH=
if $default_repo; then
  if test $is_installed = "true"; then 
    REPOSITORY="${datadir}/${PACKAGE}-${VERSION}/repository"
  else
    REPOSITORY="${abs_top_builddir}/share/repository"
  fi
  if $verbose; then 
    echo "Looking for SIDL XMLs in \"${REPOSITORY}\"...";
  fi 
  if $cygwin; then
    REPOSITORY=`cygpath --path --windows "${REPOSITORY}"`
    REPOSITORY_PATH="--default-repository-path=\"${REPOSITORY}\""
  else 
    REPOSITORY_PATH="--default-repository-path=${REPOSITORY}"
  fi
fi

#
# Modify other paths for Cygwin if necessary.
#
if $cygwin; then
  exec_prefix=`cygpath --path --windows "${exec_prefix}"`
  prefix=`cygpath --path --windows "${prefix}"`
  CLASSPATH=`cygpath --path --windows "${CLASSPATH}"`
fi

#
# Run the babel command line driver
#
if $debug; then
  THIS_JAVA=$THIS_JDB
fi

if test $quiet = false; then 
  echo "CLASSPATH = ${CLASSPATH}"
  echo ${THIS_JAVA} gov.llnl.babel.CommandLineDriver ${REPOSITORY_PATH} ${args}
fi;
export CLASSPATH
${THIS_JAVA} gov.llnl.babel.CommandLineDriver ${REPOSITORY_PATH} ${args}

exitstat=$?
if test $quiet = false; then
  echo exit status = $exitstat
fi;

exit $exitstat
