#! /bin/sh
#
# Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

#set appserv specific env variables
. %CONFIG_HOME%/asenv.conf
# Set ANT_HOME
ANT_HOME=${AS_ANT}
# Set JAVA_HOME
JAVA_HOME=$AS_JAVA

if [ -f $HOME/.antrc ] ; then 
  . $HOME/.antrc
fi

# OS specific support.  $var _must_ be set to either true or false.
cygwin=false;
darwin=false;
case "`uname`" in
  CYGWIN*) cygwin=true ;;
  Darwin*) darwin=true ;;
esac

if [ -z "$ANT_HOME" ] ; then
  # try to find ANT
  if [ -d /opt/ant ] ; then 
    ANT_HOME=/opt/ant
  fi

  if [ -d ${HOME}/opt/ant ] ; then 
    ANT_HOME=${HOME}/opt/ant
  fi

  ## resolve links - $0 may be a link to ant's home
  PRG=$0
  progname=`basename $0`
  
  while [ -h "$PRG" ] ; do
    ls=`ls -ld "$PRG"`
    link=`expr "$ls" : '.*-> \(.*\)$'`
    if expr "$link" : '.*/.*' > /dev/null; then
	PRG="$link"
    else
	PRG="`dirname $PRG`/$link"
    fi
  done
  
  ANT_HOME=`dirname "$PRG"`/..

fi

# For Cygwin, ensure paths are in UNIX format before anything is touched
if $cygwin ; then
  [ -n "$ANT_HOME" ] &&
    ANT_HOME=`cygpath --unix "$ANT_HOME"`
  [ -n "$JAVA_HOME" ] &&
    JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
  [ -n "$CLASSPATH" ] &&
    CLASSPATH=`cygpath --path --unix "$CLASSPATH"`
fi

if [ -z "$JAVACMD" ] ; then 
  if [ -n "$JAVA_HOME"  ] ; then
    if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 
      # IBM's JDK on AIX uses strange locations for the executables
      JAVACMD=$JAVA_HOME/jre/sh/java
    else
      JAVACMD=$JAVA_HOME/bin/java
    fi
  else
    JAVACMD=java
  fi
fi
 
if [ ! -x "$JAVACMD" ] ; then
  echo "Error: JAVA_HOME is not defined correctly."
  echo "  We cannot execute $JAVACMD"
  exit
fi

if [ -n "$CLASSPATH" ] ; then
  LOCALCLASSPATH=$CLASSPATH
fi

# add in the dependency .jar files
DIRLIBS=$AS_ANT_LIB/*.jar
for i in ${DIRLIBS}
do
    # if the directory is empty, then it will return the input string
    # this is stupid, so case for it
    if [ "$i" != "${DIRLIBS}" ] ; then
      if [ -z "$LOCALCLASSPATH" ] ; then
        LOCALCLASSPATH=$i
      else
        LOCALCLASSPATH="$i":$LOCALCLASSPATH
      fi
    fi
done

# add in the dependency Sun Appserv .jar files
if [ -f ${AS_INSTALL}/lib/sun-appserv-ant.jar ] ; then
  LOCALCLASSPATH=${AS_INSTALL}/lib/sun-appserv-ant.jar:$LOCALCLASSPATH
fi

if [ -n "$JAVA_HOME" ] ; then
  if [ -f "$JAVA_HOME/lib/tools.jar" ] ; then
    LOCALCLASSPATH=$JAVA_HOME/lib/tools.jar:$LOCALCLASSPATH
  fi

  if [ -f "$JAVA_HOME/lib/classes.zip" ] ; then
    LOCALCLASSPATH=$LOCALCLASSPATH:$JAVA_HOME/lib/classes.zip
  fi

  # OSX hack to make Ant work with jikes
  if $darwin ; then
    OSXHACK="/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Classes"
    if [ -d ${OSXHACK} ] ; then
      for i in ${OSXHACK}/*.jar
      do
        JIKESPATH=$JIKESPATH:$i
      done
    fi
  fi

else
  echo "Warning: JAVA_HOME environment variable is not set."
  echo "  If build fails because sun.* classes could not be found"
  echo "  you will need to set the JAVA_HOME environment variable"
  echo "  to the installation directory of java."
fi

# supply JIKESPATH to Ant as jikes.class.path
if [ -n "$JIKESPATH" ] ; then
  if [ -n "$ANT_OPTS" ] ; then
    ANT_OPTS="$ANT_OPTS -Djikes.class.path=$JIKESPATH"
  else
    ANT_OPTS=-Djikes.class.path=$JIKESPATH
  fi
fi

# For Cygwin, switch paths to Windows format before running java
if $cygwin; then
  ANT_HOME=`cygpath --path --windows "$ANT_HOME"`
  JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"`
  LOCALCLASSPATH=`cygpath --path --windows "$LOCALCLASSPATH"`
fi
# $JAVACMD -Djava.library.path=$AS_INSTALL/lib:$AS_ICU_LIB -classpath "$LOCALCLASSPATH" -Dant.home="${ANT_HOME}" $ANT_OPTS org.apache.tools.ant.Main "$@"

$JAVACMD -Djava.library.path=$AS_INSTALL/lib:$AS_ICU_LIB -Dcom.sun.aas.defaultLocale=$AS_LOCALE -Dcom.sun.aas.icuLib=$AS_ICU_LIB -Dcom.sun.aas.webServicesLib=$AS_WEBSERVICES_LIB -Dcom.sun.aas.javaRoot=$AS_JAVA -Dcom.sun.aas.imqLib=$AS_IMQ_LIB -Dcom.sun.aas.imqBin=$AS_IMQ_BIN -Dcom.sun.aas.perlRoot=$AS_PERL -Dcom.sun.aas.configRoot=$AS_CONFIG -Dcom.sun.aas.installRoot=$AS_INSTALL -Dcom.sun.aas.domainsRoot=$AS_DEF_DOMAINS_PATH -classpath $LOCALCLASSPATH:$AS_INSTALL/lib/appserv-admin.jar:$AS_INSTALL/lib/appserv-ext.jar:$AS_INSTALL/lib/appserv-rt.jar -Dant.home="${ANT_HOME}" $ANT_OPTS org.apache.tools.ant.Main "$@"
