#!/bin/bash

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# -----------------------------------------------------------------------------
# Script for Jasper compiler
#   - mvyskocil reworked for SUSE tomcat6 package - see
#   https://bugzilla.novell.com/show_bug.cgi?id=653586
#
# Environment Variable Prequisites
#
#   JASPER_HOME   May point at your Catalina "build" directory.
#
#   JASPER_OPTS   (Optional) Java runtime options used when the "start",
#                 "stop", or "run" command is executed.
#
#   JAVA_HOME     Must point at your Java Development Kit installation.
#
#   JAVA_OPTS     (Optional) Java runtime options used when the "start",
#                 "stop", or "run" command is executed.

# Get the tomcat config (use this for environment specific settings)

if [ -z "${TOMCAT_CFG}" ]; then
    TOMCAT_CFG="/etc/tomcat6/tomcat6.conf"
fi

if [ -r "$TOMCAT_CFG" ]; then
    . $TOMCAT_CFG
fi

CLASSPATH="${CLASSPATH}:$(build-classpath tomcat6 ant)"
CLASSPATH="${CLASSPATH}:$(build-classpath tomcat6-servlet-2.5-api tomcat6-jsp-2.1-api tomcat6-el-1.0-api)"
# CLASSPATH munging
if [ -n "$JSSE_HOME" ]; then
  CLASSPATH="${CLASSPATH}:$(build-classpath jcert jnet jsse 2>/dev/null)"
fi
CLASSPATH="${CLASSPATH}:${CATALINA_HOME}/bin/bootstrap.jar"
CLASSPATH="${CLASSPATH}:${CATALINA_HOME}/bin/tomcat-juli.jar"
CLASSPATH="${CLASSPATH}:$(build-classpath commons-daemon 2>/dev/null)"

#sanitize the classpath - remove double and trailing colons
CLASSPATH="`echo ${CLASSPATH} | sed 's/::+/:/g; s/^://; s/:$//g'`"

# ----- Execute The Requested Command -----------------------------------------

if [ "$1" = "jspc" ] ; then

  shift
  exec "${JAVA_HOME}/bin/java" $JAVA_OPTS $JASPER_OPTS \
    -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \
    -Djasper.home="$JASPER_HOME" \
    -Dcatalina.home="$JASPER_HOME" \
    org.apache.jasper.JspC "$@"

elif [ "$1" = "debug" ] ; then

  shift
  exec "${JAVA_HOME}/bin/jdb" $JAVA_OPTS $JASPER_OPTS \
    -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \
    -Djasper.home="$JASPER_HOME" \
    -Dcatalina.home="$JASPER_HOME" \
    org.apache.jasper.JspC "$@"

else

  echo "Usage: jasper.sh ( jspc )"
  echo "Commands:"
  echo "  jspc - Run the offline JSP compiler"
  exit 1

fi
