#!/bin/bash

# Copyright (c) 2004-2007, The PalCom Project consortium.
# See http://www.ist-palcom.org/consortium.
# 
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
# 
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
# 
#     * Redistributions in binary form must reproduce the above
#       copyright notice, this list of conditions and the following
#       disclaimer in the documentation and/or other materials provided
#       with the distribution.
# 
#     * Neither the name of the PalCom Project nor the names of its
#       contributors may be used to endorse or promote products derived
#       from this software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


# set -e # exit on error

function usage ()
{
    echo "Usage: pal-dev-build-javadoc [options]"
    echo "  options:"
    echo "       -h  display this help"
    echo "       -f  fast build: do not check svn status and fix dates"
    echo "       -b  do not rebuild java interfaces from Smalltalk base"
}

# Check arguments
for a 
do
  if [ "$a" == "-h" ]; then 
      usage
      exit
  fi
  if [ "$a" == "-f" ]; then 
      FASTBUILD=1
      continue
  fi
  if [ "$a" == "-b" ]; then 
      NOBASE=1
      continue
  fi
  echo Illegal option: $a
  usage
  exit 1
done

echo ''; echo "###### Building PalCom javadoc ######" 

if [ ! "$JAVA_HOME" ]
then
  echo "The JAVA_HOME environment variable is not set. It must be set to "
  echo "point to the JRE directory (The javac executable should be at "
  echo "\$JAVA_HOME/bin/javac)."
  exit
fi

if [ "$OSTYPE" = "cygwin" ]
then
  SEP=";"
else
  SEP=":"
fi

ROOT=`pal-dev-root`
DEVDIR="${ROOT}developer"
JAVADOCDIR="${ROOT}doc/javadoc"
BASEDIR="${DEVDIR}/runtime-environment/engine/pal-base"
ANTDIR="${DEVDIR}/tools/compilers/pal-j/lib"
DEVLIB="${DEVDIR}/dev-lib"

# Locations of java files to include.
# Add source directories here:

DIRS="\
${DEVDIR}/frameworks/assembly/src \
${DEVDIR}/frameworks/assembly/src-ast/gen \
${DEVDIR}/frameworks/assembly/src-ast/gen/ist/palcom/resource \
${DEVDIR}/frameworks/assembly/src-ast/gen/ist/palcom/resource/descriptor \
${DEVDIR}/frameworks/device/src \
${DEVDIR}/frameworks/service/src \
${DEVDIR}/tools/launcher/src \
${DEVDIR}/tools/cli-assembler/src \
${DEVDIR}/tools/reflect/pal-dep/src \
${DEVDIR}/tools/simulation/simulated-devices/src \
${DEVDIR}/tools/browsers/EclipseBrowser/src \
${DEVDIR}/tools/browsers/pal-overview-browser/src \
${DEVDIR}/tools/browsers/RCPBrowser/src \
${DEVDIR}/tools/compilers/pal-st/src \
${DEVDIR}/tools/compilers/pal-ast/src \
${DEVDIR}/tools/bytecode/pal-dis/src \
${DEVDIR}/tools/bytecode/pal-asm/src \
${DEVDIR}/tools/bytecode/pal-bytecodes/src \
${DEVDIR}/runtime-environment/engine/jtst/src \
${DEVDIR}/runtime-environment/engine/pal-jbase/src \
${DEVDIR}/runtime-environment/rte-util/communication/routing-layer-util/src \
${DEVDIR}/runtime-environment/rte-util/communication/mal-layer-util/src \
${DEVDIR}/runtime-environment/core/xml/src \
${DEVDIR}/runtime-environment/core/communication/communication-layer/src \
${DEVDIR}/runtime-environment/core/communication/function-layer/src \
${DEVDIR}/runtime-environment/core/communication/routing-layer/src \
${DEVDIR}/runtime-environment/core/communication/mal-layer/src \
${DEVDIR}/runtime-environment/core/hgraph/src \
${DEVDIR}/runtime-environment/core/resource/src-ast-hla
${DEVDIR}/runtime-environment/core/collections/src \
${DEVDIR}/runtime-environment/core/process/src \
${DEVDIR}/middleware-managers/resource-manager/src \
${DEVDIR}/middleware-managers/assembly-manager/src \
${DEVDIR}/middleware-managers/contingency-manager/src \
${DEVDIR}/application-layer/services/hgraph-manager-service/src \
${DEVDIR}/application-layer/services/BioBelt/src \
${DEVDIR}/application-layer/services/AlarmHRHR/src \
${DEVDIR}/application-layer/services/tilessimulator/src \
${DEVDIR}/application-layer/services/timer/src \
${DEVDIR}/application-layer/services/poetry/src \
${DEVDIR}/application-layer/services/proximityService/src \
${DEVDIR}/application-layer/services/CompassService/src \
${DEVDIR}/application-layer/services/simulated-slideshow/src \
${DEVDIR}/application-layer/services/incubator-display/src \
${DEVDIR}/application-layer/services/simulated-geotagger/src \
${DEVDIR}/application-layer/services/axis-camera/src \
${DEVDIR}/application-layer/services/Respirator/src \
${DEVDIR}/application-layer/services/simulated-mstone/src \
${DEVDIR}/application-layer/services/id/rfid/src \
${DEVDIR}/application-layer/services/simulated-handheld/src \
${DEVDIR}/application-layer/services/pal-visualization-applications/src
${DEVDIR}/application-layer/services/GoogleEarth/src \
${DEVDIR}/application-layer/services/GeoConverter/src \
${DEVDIR}/application-layer/services/simulated-axiscamera/src \
${DEVDIR}/application-layer/services/incubatoralarm/src \
${DEVDIR}/application-layer/services/Saturimeter/src \
${DEVDIR}/application-layer/services/communication/rascal/src \
${DEVDIR}/application-layer/services/GPSParser/src \
${DEVDIR}/application-layer/services/factory/src \
${DEVDIR}/application-layer/services/GPSService/src \
${DEVDIR}/application-layer/services/ais/src \
${DEVDIR}/application-layer/services/ais/test \
${DEVDIR}/application-layer/services/echo/src \
${DEVDIR}/application-layer/services/mattress/src \
${DEVDIR}/application-layer/services/PalBox/src \
${DEVDIR}/util/display/nanoX/src \
${DEVDIR}/util/storage/src \
${DEVDIR}/util/pal-visualization/src
"
# removed from DIRS to fix warnings and errors: 
# ${DEVDIR}/runtime-environment/engine/j-libs/src \

# Cut out due to errors:
# Smalltalk: ${DEVDIR}/runtime-environment/engine/pal-base/src \
#${DEVDIR}/runtime-environment/engine/jbenchmarks/src \
#${DEVDIR}/runtime-environment/engine/thirdparty/src \
# C++: ${DEVDIR}/runtime-environment/engine/pal-vm/src \


# Packages to exclude
EXCLUDE=ist.palcom.st.test

# Additional classpath
CLASSPATH="${DEVDIR}/dev-lib/jars/thirdparty/bt-skeleton.jar"
CLASSPATH="${CLASSPATH}${SEP}${DEVDIR}/dev-lib/jars/thirdparty/comm-skeleton.jar"
CLASSPATH="${CLASSPATH}${SEP}${DEVDIR}/dev-lib/jars/thirdparty/rxtx/RXTXcomm.jar"
CLASSPATH="${CLASSPATH}${SEP}${DEVDIR}/tools/compilers/pal-ast/lib/antlr.jar"

# SUBPACKAGES specifies what top-level packages to document
# All subpackages will be included automatically
SUBPACKAGES=ist.palcom:java:org

DOCTITLE="PalCom"

##### End configuration ######

nondir=0
for d in $DIRS
do
  # check existence
  if [ ! -d "$d" ]
  then
      echo '***' " $d: no such directory"
      nondir=1
  fi
done
if [ $nondir != 0 ]
then
    echo Non-existing directories specified. Exiting.
    exit 1
fi

DIRS="${ROOT}JAVADOC $DIRS"

SRCPATH=`echo $DIRS | sed -e "s% %$SEP%g"`

if [ "$NOBASE" != "1" ]
then
    echo ""
    echo Building Java interfaces corresponding to base Smalltalk sources
    pushd "$BASEDIR" >/dev/null 2>&1
    pal-dev-build-base -j | grep -v "process class" | grep -v "process meta info"
    popd >/dev/null 2>&1
    cp $BASEDIR/src/ist/palcom/base/package.html ${ROOT}JAVADOC/ist/palcom/base
    cp $BASEDIR/src/ist/palcom/base/io/package.html ${ROOT}JAVADOC/ist/palcom/base/io
    cp $BASEDIR/src/ist/palcom/base/networking/package.html ${ROOT}JAVADOC/ist/palcom/base/networking
fi

# Build Javadoc HTML
echo ""
echo Building javadoc from Java source files

mkdir -p "${JAVADOCDIR}/html"
javadoc -quiet \
        -source 1.5 \
        -classpath ${CLASSPATH} \
        -sourcepath "${SRCPATH}" \
        -d "${JAVADOCDIR}/html" \
        -doctitle "${DOCTITLE}" \
        -windowtitle "${DOCTITLE}" \
        -subpackages "${SUBPACKAGES}" \
        -exclude "${EXCLUDE}" \
        -overview "${JAVADOCDIR}/overview/overview.html"
if [ "$?" == "0" ]; then
    echo Javadoc HTML generated to "${JAVADOCDIR}/html"
else
    echo "*** javadoc generation FAILED - please check ***"
fi

if [ "$FASTBUILD" == 1 ]; then
    echo ""
    echo Doing a FAST BUILD.
    echo This means that it will not be checked if new files should
    echo be added to svn, and the date comments in the generated files 
    echo are not removed.
    echo Please do not commit without doing a full build first.
    echo ""
else
    echo ""
    echo Adding new files/dirs to svn
    cd "${JAVADOCDIR}/html"
    svn stat | perl -ne "if (/^\?..... /){system \"svn add $'\";}"
    
    echo ""
    echo Fixing svn attributes and deleting date comments from all html files
    cd "${JAVADOCDIR}/html"
    find . -name '*.html' -exec "${JAVADOCDIR}/fix-html" '{}' \;
fi

echo ""
echo Cleaning up
/bin/rm -rf "$ROOT/JAVADOC"

echo ""
echo done.
