#!/bin/bash

cygwin=false;
case "`uname`" in
  CYGWIN*) cygwin=true;
esac

# Figure out DTDANALYZER_HOME, which is the directory in which this script
# resides.  Note that this resolves symlinks (HT Gerrit Imsieke). First check
# if greadlink exists (Mac OS X) and if so, use that.
if hash greadlink 2>/dev/null; then
    READLINK=greadlink
else
    READLINK=readlink
fi
DTDANALYZER_HOME="$( cd -P "$(dirname $( $READLINK -f "${BASH_SOURCE[0]}" ))" && pwd )"

# Add the build directory to the classpath.  This script is used both by
# developers and users.  For developers, the .class files in the build
# directory will be found first.  Users don't have a build directory.
CP="$DTDANALYZER_HOME/build"

# Add all the .jar files in the lib subdirectory to the classpath
for jar in "$DTDANALYZER_HOME"/lib/*.jar "$DTDANALYZER_HOME"/lib/*/*.jar
do
  CP="$CP:$jar"
done

# If we're on cygwin, convert the format of the classpath and home directory
if $cygwin; then
  CP="$(cygpath -map "$CP")"
  DTDANALYZER_HOME="$(cygpath -ma "$DTDANALYZER_HOME")"
fi

# We're not using log4j yet, but we might in the future.  Until then, this
# shouldn't do any harm.
LOGCONFIG=${LOGCONFIG:-file:"$DTDANALYZER_HOME"/etc/log4j.properties}

# And, execute!  Setting the DTDANALYZER_HOME system property so that
# the utility can find supplementary files like XSLT, CSS and JS.
exec java -cp "$CP" -Xmx256M "-Dlog4j.configuration=${LOGCONFIG}" \
           "-DDTDANALYZER_HOME=$DTDANALYZER_HOME" \
           gov.ncbi.pmc.dtdanalyzer.DtdDocumentor "$@"
