#! /bin/sh
## File:        babel
## Package:     Babel binary
## Copyright:   (c) 2000-2001 The Regents of the University of California
## Revision:    $Revision: 4434 $
## Modified:    $Date: 2005-03-17 09:05:29 -0800 (Thu, 17 Mar 2005) $
## 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="0.10.2"
PACKAGE="babel"
prefix="/usr"
exec_prefix="${prefix}"
datadir="${prefix}/share"
bindir="${exec_prefix}/bin"
JAVA="java"
WHICH_JAVA="/usr/bin/java"
TEST_EF="/usr/bin/test"
abs_top_builddir="/home/hazelsct/babel-0.10.2/bin/../."
python="python2.4"

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 - "$1" "$2" <<EOF
import os.path
import sys
print not os.path.samefile( sys.argv[1], sys.argv[2] )
EOF`
      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; a=;;
    --noquiet) quiet=false; a=;;
    --no-default-repository | -!) default_repo=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="$WHICH_JAVA"
THIS_JDB="jdb"
my_java=`which ${JAVA}`
java_found=$?
if test "x${my_java}" != "x${WHICH_JAVA}"; then
  if samefile ${my_java} ${WHICH_JAVA}; then 
    :
  else
    if test "$override_installpath" = "false"; then
      echo "warning: Babel configured for ${WHICH_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
SYSJAR=/usr/share/java
is_installed="false"
if samefile ${bindir}/${myname} $path2me; then 
  is_installed="true"
  BABELJAR="${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
  BABELJAR="${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 ${BABELJAR}/${PACKAGE}-${VERSION}.jar; then
  if $verbose; then
    echo "Looking for jar files in \"${BABELJAR}\"...";
  fi
else 
  echo "ERROR: $0 " >&2
  echo "   Expected to find ${BABELJAR}/${PACKAGE}-${VERSION}.jar" >&2
  exit 1
fi

BABELCP=${BABELJAR}/${PACKAGE}-${VERSION}.jar:${SYSJAR}/gnu-getopt.jar:${SYSJAR}/xercesImpl.jar:${SYSJAR}/xmlParserAPIs.jar:${SYSJAR}/xml-apis.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 test -d ${REPOSITORY}; then
    if $verbose; then 
      echo "Looking for SIDL XMLs in \"${REPOSITORY}\"...";
    fi 
    if $cygwin; then
      REPOSITORY=`cygpath --path --windows "${REPOSITORY}"`
      REPOSITORY_PATH="--repository-path=\"${REPOSITORY}\""
    else 
      REPOSITORY_PATH="--repository-path=${REPOSITORY}"
    fi
  else
    echo "ERROR: $0 " >&2
    echo "    The default XML repository of SIDL types was expected in" >&2
    echo "       ${REPOSITORY}" >&2
    exit -1
  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
