#! /bin/sh

# 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.

#   This file is derived from the ant-1.5.3 distribution

#   +++ Changes for dedicated httpd documentation build +++
#
#   - ANT_HOME is the current directory (instead of parent)
#   - ignore external ANT_OPTS
#   - ignore external ANT_ARGS
#   - ignore external CLASSPATH
#   - set java stack size to 128 MB
#   - lower down verbosity (because the foreach task would be _very_ verbose
#     otherwise)
#

# explicit name our build file (may be replaced by a variable some time)
ANT_ARGS="-buildfile build.xml"

# lower down logger verbosity
ANT_ARGS="-logger org.apache.tools.ant.NoBannerLogger $ANT_ARGS"

# raise heap/stack size...
ANT_OPTS="-Xss1024k -Xmx256m -mx256m"

# set classpath
CLASSPATH=./lib

# set ant home directory (cwd)
ANT_HOME=.

# OS specific support.  $var _must_ be set to either true or false.
cygwin=false;
darwin=false;
case "`uname -a`" in
  CYGWIN*) cygwin=true ;;
  Darwin*) darwin=true
           if [ -z "$JAVA_HOME" ] ; then
             JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Home   
           fi
           ;;
  Linux*ppc*) ANT_OPTS="$ANT_OPTS -Xss1m -ss1m" ;;
esac

# 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

# set ANT_LIB location
ANT_LIB="${ANT_HOME}/lib"

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=`which java 2> /dev/null `
    if [ -z "$JAVACMD" ] ; then 
        JAVACMD=java
    fi
  fi
fi
 
if [ ! -x "$JAVACMD" ] ; then
  echo "Error: JAVA_HOME is not defined correctly."
  echo "  We cannot execute $JAVACMD"
  exit 1
fi

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

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

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

  if [ -f "$JAVA_HOME/lib/classes.zip" ] ; then
    LOCALCLASSPATH="$LOCALCLASSPATH:$JAVA_HOME/lib/classes.zip"
  fi
else
  echo "Warning: JAVA_HOME environment variable is not set (or not exported)."
  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

# For Cygwin, switch paths to Windows format before running java
if $cygwin; then
  ANT_HOME=`cygpath --windows "$ANT_HOME"`
  JAVA_HOME=`cygpath --windows "$JAVA_HOME"`
  CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
  LOCALCLASSPATH=`cygpath --path --windows "$LOCALCLASSPATH"`
  CYGHOME=`cygpath --windows "$HOME"`
fi

if ! java -fullversion 2>&1 | egrep -e '\"(1\.8|1[1-9][0-9]*|[2-9][0-9]+)' > /dev/null; then
     echo "Use a 1.8 or 11+ JRE to build the manual $(which java)"
     exit 1
fi

# --- Ensure vnu.jar is present and up-to-date ---
VNU_JAR="${ANT_HOME}/vnu.jar"
VNU_URL="https://github.com/validator/validator/releases/download/latest/vnu.jar"
VNU_MD5_URL="https://github.com/validator/validator/releases/download/latest/vnu.jar.md5"

# Get the expected MD5 from the published checksum file
VNU_REMOTE_MD5=$(curl -sfL "$VNU_MD5_URL" | awk '{print $1}')

if [ -z "$VNU_REMOTE_MD5" ]; then
    echo "Warning: could not fetch latest vnu.jar checksum from GitHub."
    if [ ! -f "$VNU_JAR" ]; then
        echo "  vnu.jar is missing and cannot be fetched. HTML validation will be skipped."
    else
        echo "  Continuing with existing vnu.jar."
    fi
else
    # Compute local MD5 (if file exists)
    VNU_LOCAL_MD5=""
    if [ -f "$VNU_JAR" ]; then
        VNU_LOCAL_MD5=$(md5sum "$VNU_JAR" 2>/dev/null || md5 -q "$VNU_JAR" 2>/dev/null)
        VNU_LOCAL_MD5=$(echo "$VNU_LOCAL_MD5" | awk '{print $1}')
    fi

    if [ "$VNU_LOCAL_MD5" = "$VNU_REMOTE_MD5" ]; then
        echo "vnu.jar is up-to-date."
    else
        echo "Fetching latest vnu.jar ..."
        curl -sfL -o "$VNU_JAR" "$VNU_URL"
        if [ $? -eq 0 ] && [ -f "$VNU_JAR" ]; then
            echo "vnu.jar updated successfully."
        else
            echo "Warning: failed to download vnu.jar. HTML validation may not work."
        fi
    fi
fi

if [ -n "$CYGHOME" ]; then
    exec "$JAVACMD" $ANT_OPTS -classpath "$LOCALCLASSPATH" -Dant.home="${ANT_HOME}" -Dcygwin.user.home="$CYGHOME" org.apache.tools.ant.Main $ANT_ARGS "$@"
else
    exec "$JAVACMD" $ANT_OPTS -classpath "$LOCALCLASSPATH" -Dant.home="${ANT_HOME}" org.apache.tools.ant.Main $ANT_ARGS "$@"
fi

