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


# Builds the Java tools without using Eclipse

if [ "$OSTYPE" = "cygwin" ]
then
  SEP=";"
else
  SEP=":"
fi
ROOT=`pal-dev-root`
cd "$ROOT"

function usage ()
{
    echo "Usage: pal-dev-build-tools [options]"
    echo "  options:"
    echo "       -h  display this help"
    echo "       -c  clean tool directories"
}


function clean_classes()
{
    dir="$1"
    echo "cleaning $dir"
    find "$dir" -name '*.class' -exec rm -f '{}' \;
    rm -rf "$dir/bin"
}

function clean_java()
{
    dir=$1
    echo "  from $dir"
    rm -f $dir/*.java
}

function clean_ant()
{
    dir="$1"
    echo ""
    echo -n "Ant cleaning ${ROOT}$dir: "
    cd "${ROOT}$dir"
    if [ -f "build.xml" ]; then
	grep -q 'name="clean"' build.xml
	if [ "$?" == "0" ]; then
	    ant -quiet clean
	else
	    echo ""
	    echo '*** No "clean" target in build.xml. Please fix!'
	fi
    else
	echo ""
	echo No build.xml found.
    fi
}


function rmdir()
{
    dir="$1"
    echo "  $dir"
    rm -rf "$dir"
}

function cleanup()
{
    clean_classes "${ROOT}developer/tools/bytecode/pal-bytecodes"
    clean_classes "${ROOT}developer/tools/bytecode/pal-asm"
    clean_classes "${ROOT}developer/tools/bytecode/pal-dis"
    clean_classes "${ROOT}developer/tools/compilers/pal-st"
    clean_classes "${ROOT}developer/tools/compilers/pal-j"
    clean_classes "${ROOT}developer/tools/compilers/pal-ast"
    echo Removing generated Java files and directories:
    #FIXME: the following directories contains both generated and committed files
    clean_ant developer/tools/browsers/EclipseBrowser
    clean_ant developer/tools/browsers/OverviewBrowser
    clean_ant developer/tools/browsers/jars-plugin
    clean_ant developer/tools/compilers/pal-j/Java1.4Frontend
    clean_ant developer/tools/compilers/pal-j/Backend
    /bin/rm -f tools/compilers/pal-j/Backend/componentparser/ComponentParser.java
    /bin/rm -f tools/compilers/pal-j/Backend/componentparser/ComponentScanner.java
    rmdir "${ROOT}developer/tools/compilers/pal-j/Backend/AST"
    rmdir "${ROOT}developer/tools/compilers/pal-j/Backend/beaver"
    rmdir "${ROOT}developer/tools/compilers/pal-j/Backend/bytecode"
    rm -f "${ROOT}developer/tools/compilers/pal-j/Backend/pal-j.jar"
}

# Check arguments
for a 
do
  if [ "$a" == "-h" ]; then 
      usage
      exit
  fi
  if [ "$a" == "-c" ]; then 
      cleanup
      exit
  fi
  echo Illegal option: $a
  usage
  exit 1
done

function error ()
{
    echo 'Stopped because of compilation error(s)'
    echo 'CLASSPATH is:'
    echo $CLASSPATH
    exit 1
}

set -e  # exit on error
trap error EXIT  # execute "error" on exit

DIR="${ROOT}developer/tools"
cd "$DIR"
export CLASSPATH="${DIR}/compilers/pal-st/lib/antlr.jar${SEP}${DIR}/compilers/pal-st/lib/junit-4.1.jar"

DIR=${ROOT}developer/tools/bytecode/pal-bytecodes
cd "$DIR"; echo "--- compiling ${DIR}"
mkdir -p bin
find src -name \*.java | xargs javac -source 1.5 -sourcepath src -d bin
export CLASSPATH="$CLASSPATH${SEP}${DIR}/bin"

DIR=${ROOT}developer/tools/bytecode/pal-asm
cd "$DIR"; echo "--- compiling ${DIR}"
mkdir -p bin
find src -name \*.java | xargs javac -source 1.5 -sourcepath src -d bin
export CLASSPATH="$CLASSPATH${SEP}${DIR}/bin"

DIR=${ROOT}developer/tools/bytecode/pal-dis
cd "$DIR"; echo "--- compiling ${DIR}"
mkdir -p bin
find src -name \*.java | xargs javac -source 1.5 -sourcepath src -d bin
export CLASSPATH="$CLASSPATH${SEP}${DIR}/bin"

DIR=${ROOT}developer/tools/reflect/pal-dep
cd "${DIR}"; echo "--- compiling ${DIR}"
mkdir -p bin
find src -name \*.java | xargs javac -source 1.5 -sourcepath src -d bin
export CLASSPATH="$CLASSPATH${SEP}${DIR}/bin"

DIR=${ROOT}developer/tools/compilers/pal-st
cd "$DIR"; echo "--- compiling ${DIR}"
cd "$DIR"; echo "--- compiling smalltalk parser"
pal-dev-build-parser-pal-st
mkdir -p bin
cd "$DIR"; echo "--- compiling smalltalk compiler"
find src -name \*.java | xargs javac -source 1.5 -sourcepath src -d bin
export CLASSPATH="$CLASSPATH${SEP}${ROOT}developer/tools/compilers/pal-st/bin"

DIR=${ROOT}developer/tools/compilers/pal-j
cd "$DIR"; echo "--- compiling ${DIR}"
export CLASSPATH="$CLASSPATH${SEP}${DIR}/lib/ant.jar${SEP}${DIR}/lib/ant-launcher.jar"

#N.B. the frontend need not be built separately
#DIR=${ROOT}developer/tools/compilers/pal-j/Java1.4Frontend
#cd "$DIR"; echo "--- compiling ${DIR}"
#java org.apache.tools.ant.Main

DIR=${ROOT}developer/tools/compilers/pal-j/Backend
cd "$DIR"; echo "--- compiling ${DIR}"
java -Xmx150M org.apache.tools.ant.Main -q jar

DIR=${ROOT}developer/tools/compilers/pal-ast
cd "$DIR"; echo "--- compiling ${DIR}"
mkdir -p bin
find src -name \*.java | xargs javac -source 1.5 -sourcepath src -d bin
export CLASSPATH="$CLASSPATH${SEP}${DIR}/bin"

echo '--- DONE.'           

trap - EXIT  # execute nothing on exit
exit 0
