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


# Used in BASH to build core components for pal-vm

function usage ()
{
    echo "Usage: pal-dev-build-core [options]"
    echo "  options:"
    echo "       -h        display this help"
    echo "      --nodev"
    echo "       -e        use exported compilers to build tools"
    echo "       -t        build only test programs"
    echo "       -v        verbose output"
    echo "       -c        clean core directories"
}

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

DSTDIR="developer/dev-lib"
TSTDIR="developer/runtime-environment/engine/pal-vm/test"
BASEDIR="developer/runtime-environment/engine/pal-base"
PROCESSDIR="developer/runtime-environment/core/process"
RESOURCEDIR="developer/runtime-environment/core/resource"
HGRAPHDIR="developer/runtime-environment/core/hgraph"
MALDIR="developer/runtime-environment/core/communication/mal-layer"
ROUTINGDIR="developer/runtime-environment/core/communication/routing-layer"
COMMDIR="developer/runtime-environment/core/communication/communication-layer"
FUNCTIONDIR="developer/runtime-environment/core/communication/function-layer"
RTEUTILDIR="developer/runtime-environment/rte-util"
RESOURCEMANAGERDIR="developer/middleware-managers/resource-manager"
DEVICEDIR="developer/frameworks/device"
SERVICEDIR="developer/frameworks/service"
ASSEMBLYDIR="developer/frameworks/assembly"
XMLDIR="developer/runtime-environment/core/xml"
JLIBS="developer/runtime-environment/engine/j-libs"
COLLECTIONS="developer/runtime-environment/core/collections"
PALJDIR="developer/tools/compilers/pal-j"

STORAGEDIR="developer/util/storage" 
# TODO Move storage to pal-dev-build-util script


DEV=-dev

SOURCES="${ROOT}developer/frameworks/assembly/src${SEP}\
${ROOT}developer/frameworks/device/src${SEP}\
${ROOT}developer/frameworks/service/src${SEP}\
${ROOT}developer/middleware-managers/assembly-manager/src${SEP}\
${ROOT}developer/middleware-managers/resource-manager/src${SEP}\
${ROOT}developer/middleware-managers/service-manager/src${SEP}\
${ROOT}developer/runtime-environment/core/collections/src${SEP}\
${ROOT}developer/runtime-environment/core/communication/communication-layer/src${SEP}\
${ROOT}developer/runtime-environment/core/communication/function-layer/src${SEP}\
${ROOT}developer/runtime-environment/core/communication/mal-layer/src${SEP}\
${ROOT}developer/runtime-environment/core/communication/routing-layer/src${SEP}\
${ROOT}developer/runtime-environment/core/contingency/src${SEP}\
${ROOT}developer/runtime-environment/core/hgraph/src${SEP}\
${ROOT}developer/runtime-environment/core/process/src${SEP}\
${ROOT}developer/runtime-environment/core/xml/src${SEP}\
${ROOT}developer/runtime-environment/engine/j-libs/src${SEP}\
${ROOT}developer/runtime-environment/engine/pal-base/src${SEP}\
${ROOT}developer/runtime-environment/rte-util/communication/mal-layer-util/src${SEP}\
${ROOT}developer/runtime-environment/rte-util/communication/routing-layer-util/src${SEP}\
${ROOT}developer/frameworks/assembly/src-ast/gen${SEP}\
${ROOT}developer/runtime-environment/core/communication/communication-layer/jastadd-src/gen${SEP}\
${ROOT}developer/runtime-environment/core/communication/function-layer/jastadd-src/gen${SEP}\
${ROOT}developer/runtime-environment/core/communication/mal-layer/jastadd-src/gen${SEP}\
${ROOT}developer/runtime-environment/core/resource/jastadd-src/gen${SEP}\
${ROOT}developer/util/storage/src"


function clean_prca()
{
    name="$1"
    echo "  $name"
    cd "${ROOT}"
    if [ "$TEST_ONLY" != "1" ]; then
	rm -f ${DSTDIR}/${name}.pr?
    fi 
    rm -f ${TSTDIR}/${name}.test.pr?
}


function clean_ant()
{
    if [ "$TEST_ONLY" != "1" ]; then
	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
    fi
}

function cleanup()
{
    echo "Removing core prc and pra files from"
    if [ "$TEST_ONLY" != "1" ]; then
	echo "  ${ROOT}${DSTDIR} and"
    fi 
    echo "  ${ROOT}${TSTDIR}:"
    clean_prca ist.palcom.assemblies
    clean_prca ist.palcom.collections
    clean_prca ist.palcom.communication.ast
    clean_prca ist.palcom.communication.ast.assemblies
    clean_prca ist.palcom.connections
    clean_prca ist.palcom.debugging
    clean_prca ist.palcom.discovery
    clean_prca ist.palcom.hgraph
    clean_prca ist.palcom.mal
    clean_prca ist.palcom.messaging
    clean_prca ist.palcom.palcomthreads
    clean_prca ist.palcom.pubsub
    clean_prca ist.palcom.routing
    clean_prca ist.palcom.routing.util.messages
    clean_prca ist.palcom.xml
    clean_prca ist.palcom.common
    clean_prca ist.palcom.services
    clean_prca org.kxml2.io
    clean_prca org.xmlpull.v1
    clean_prca ist.palcom.storage.persistent
    clean_prca ist.palcom.storage.pfs
    clean_prca ist.palcom.storage.pio
    clean_prca ist.palcom.storage.tags

    echo "Removing JastAdd generated files"
    clean_ant developer/frameworks/assembly
    clean_ant developer/runtime-environment/engine/thirdparty
    clean_ant developer/runtime-environment/core/communication/function-layer
    clean_ant developer/runtime-environment/core/resource
    clean_ant developer/runtime-environment/core/hgraph
    clean_ant developer/middleware-managers/contingency-manager/lib
    clean_ant developer/middleware-managers/contingency-manager
}

function build()
{
    build_with_prc_name "$1" "$2" "$2"
}

function build_with_prc_name()
{
    srcdir="$1"
    name="$2"
    prcname="$3"
    slashname=`echo $name | sed -e 's%\.%/%g'`
    echo Compiling $name with pal${DEV}-j
    echo -n "  "
    # Notice: ${BASEDIR}/src is included in classpath/sourcepath in order
    # for the pal-j compiler to read the java stubs corresponding to the
    # smalltalk files in base

    cd "${ROOT}"
    CPATH="${BASEDIR}/src${SEP}${JLIBS}/src${SEP}${DSTDIR}"
    if [ "$VERBOSE" == "1" ]; then 
	echo "pal${DEV}-j $jverbose -classpath "$CPATH" -c $srcdir/$slashname/Component"
    fi
    pal${DEV}-j $jverbose -classpath "$CPATH" -c $srcdir/$slashname/Component
    if [ "$name" != "$prcname" ]; then
       mv "$name.pra" "$prcname.pra"
    fi
    if [ "$VERBOSE" == "1" ]; then 
	echo "pal${DEV}-asm $prcname.pra"
    fi
    pal${DEV}-asm "$prcname.pra"
    mv $prcname.pr? "$DSTDIR"
    echo "  --> ${ROOT}$DSTDIR"
}

function build_test()
{
    srcdir="$1"
    name="$2"
    slashname=`echo $name | sed -e 's%\.%/%g'`
    echo Compiling $name with pal${DEV}-j
    echo -n "  "
    # Notice: ${BASEDIR}/src is included in classpath/sourcepath in order
    # for the pal-j compiler to read the java stubs corresponding to the
    # smalltalk files in base
    cd "${ROOT}"
    if [ "$DEV" == "-dev" ]; then
	# use prc files in $DSTDIR
	CPATH=${BASEDIR}/src${SEP}${DSTDIR}${SEP}${TSTDIR}
    else
	# use prc files in exported lib
	CPATH=${BASEDIR}/src${SEP}${ROOT}lib${SEP}${TSTDIR}
    fi
    if [ "$VERBOSE" == "1" ]; then 
	echo "pal${DEV}-j $jverbose -classpath $CPATH -c $srcdir/$slashname/Component"
    fi
    pal${DEV}-j $jverbose -classpath $CPATH -c $srcdir/$slashname/Component
    if [ "$VERBOSE" == "1" ]; then 
	echo "pal${DEV}-asm $name.pra"
    fi
    pal${DEV}-asm $name.pra
    mv $name.pr? $TSTDIR
    echo "  --> ${ROOT}$TSTDIR"
}

# The following is used when circular dependencies exists between sources
function build_with_sourcepath()
{
        cd "${ROOT}"
	build_with_sourcepath_and_prc_name "$1" "$2" "$3" "$2"
}

# The following is used when circular dependencies exists between sources
# and another name than the default should be used for the PRC file
function build_with_sourcepath_and_prc_name()
{
    srcdir="$1"
    name="$2"
    sourcepath="$3"
    prcname="$4"
    
    slashname=`echo "$name" | sed -e 's%\.%/%g'`
    echo Compiling $name
    echo -n "  "
    if [ "$VERBOSE" == "1" ]; then 
	echo pal${DEV}-j $jverbose -sourcepath "$sourcepath" -c "$srcdir/$slashname/Component"
    fi
    pal${DEV}-j $jverbose -sourcepath "$sourcepath" -c "$srcdir/$slashname/Component" 
    if [ "$name" != "$prcname" ]; then
       mv "$name.pra" "$prcname.pra"
    fi
    if [ "$VERBOSE" == "1" ]; then 
	echo pal${DEV}-asm "$prcname.pra"
    fi
    pal${DEV}-asm "$prcname.pra"
    mv $prcname.pr? "${DSTDIR}"
    echo "  --> ${ROOT}$DSTDIR"
}

function build_jastadd()
{
    projdir="$1"

    # Set classpath for running Ant
    export CLASSPATH="${ROOT}${PALJDIR}/lib/ant.jar${SEP}${ROOT}${PALJDIR}/lib/ant-launcher.jar"

    cd $projdir
    echo generating JastAdd classes
    echo "  --> $projdir"
    java org.apache.tools.ant.Main jastadd-gen   
}

# Check arguments
for a 
do
  if [ "$a" == "-h" ]; then 
      usage
      exit
  fi
  if [ "$a" == "-c" ]; then 
      cleanup
      exit
  fi
  if [ "$a" == "-t" ]; then 
      TEST_ONLY=1
      shift 1
      continue
  fi
  if [ "$a" == "-e" ]; then 
      DEV=
      shift 1
      continue
  fi
  if [ "$a" == "-v" ]; then 
      VERBOSE=1
      jverbose="-verbose"
      shift 1
      continue
  fi
  if [ "$a" == "--nodev" ]; then # repeat: cannot make -o work
      DEV=-dev
      shift 1
      continue
  fi
  echo Illegal option: $a
  usage
display this help  exit 1
done

if [ "$TEST_ONLY" != "1" ]; then
    echo Cleaning old files
    cleanup >/dev/null
fi

set -e  # exit on error

echo ""
mkdir -p "${ROOT}${DSTDIR}"




if [ "$TEST_ONLY" != "1" ]; then

    build_jastadd "${ROOT}$RESOURCEDIR"
    build_jastadd "${ROOT}$FUNCTIONDIR"
    build_jastadd "${ROOT}$ASSEMBLYDIR"

    # Collections
    build "$COLLECTIONS/src" ist.palcom.collections

    # Process
    build "$PROCESSDIR/src" ist.palcom.palcomthreads

    #hgraph
    build "$HGRAPHDIR/src" ist.palcom.hgraph
    
    # XML
    build "$XMLDIR/src"     org.xmlpull.v1
    build_with_sourcepath \
          "$XMLDIR/src"   \
          org.kxml2.io    \
          "${JLIBS}/src${SEP}${XMLDIR}/src${SEP}${BASEDIR}/src"
    build "$XMLDIR/src"     ist.palcom.xml
    
    # AST classes
    build_with_sourcepath_and_prc_name "$RESOURCEDIR/jastadd-src/gen" \
          ist.palcom.resource.descriptor \
          "${JLIBS}/src${SEP}${COLLECTIONS}/src${SEP}${PROCESSDIR}/src${SEP}${XMLDIR}/src${SEP}${BASEDIR}/src${SEP}${RESOURCEDIR}/src${SEP}${HGRAPHDIR}/src" \
          ist.palcom.resource.descriptor-pure
    build_with_sourcepath_and_prc_name "$FUNCTIONDIR/jastadd-src/gen" \
          ist.palcom.resource.descriptor \
          "${JLIBS}/src${SEP}${COLLECTIONS}/src${SEP}${PROCESSDIR}/src${SEP}${XMLDIR}/src${SEP}${BASEDIR}/src${SEP}${RESOURCEDIR}/src${SEP}${MALDIR}/src${SEP}${COMMDIR}/src${SEP}${HGRAPHDIR}/src${SEP}${FUNCTIONDIR}/src" \
          ist.palcom.resource.descriptor-function

    # MAL layer
    build "$MALDIR/src" ist.palcom.mal
    build "$RTEUTILDIR/communication/mal-layer-util/src" ist.palcom.mal.udp

    # Routing layer
    build "$ROUTINGDIR/src" ist.palcom.routing
    build_with_sourcepath \
      "$RTEUTILDIR/communication/routing-layer-util/src" \
      ist.palcom.routing.util.messages \
      "${JLIBS}/src${SEP}${ROUTINGDIR}/src${SEP}${BASEDIR}/src${SEP}${PROCESSDIR}/src${SEP}${MALDIR}/src${SEP}${COLLECTIONS}/src${SEP}${XMLDIR}/src${SEP}${FUNCTIONDIR}/src${SEP}${FUNCTIONDIR}/jastadd-src/gen${SEP}$RTEUTILDIR/communication/routing-layer-util/src${SEP}${HGRAPHDIR}/src"
    build "$RTEUTILDIR/communication/routing-layer-util/src" ist.palcom.routing.util

    # Communication layer
    build "$COMMDIR/src" ist.palcom.resource.communication
    build "$COMMDIR/src" ist.palcom.communication
    build "$COMMDIR/src" ist.palcom.messaging
    build "$COMMDIR/src" ist.palcom.pubsub
    build "$COMMDIR/src" ist.palcom.connections

    # Function layer
    build "$FUNCTIONDIR/src" ist.palcom.resource.function
    build "$FUNCTIONDIR/src" ist.palcom.discovery

    # Resource manager
    build "$RESOURCEMANAGERDIR/src" ist.palcom.resource.manager

    build_with_sourcepath "${DEVICEDIR}/src" ist.palcom.device "${SOURCES}"

    # Service framework
    build "$SERVICEDIR/src" ist.palcom.debugging
    build_with_sourcepath     \
          "$SERVICEDIR/src"   \
          ist.palcom.services \
          "${JLIBS}/src${SEP}${COLLECTIONS}/src${SEP}${PROCESSDIR}/src${SEP}${XMLDIR}/src${SEP}${MALDIR}/src${SEP}${COMMDIR}/src${SEP}${ROUTINGDIR}/src${SEP}${FUNCTIONDIR}/src${SEP}${FUNCTIONDIR}/jastadd-src/gen${SEP}${HGRAPHDIR}/src${SEP}${BASEDIR}/src${SEP}${RESOURCEMANAGERDIR}/src${SEP}${DEVICEDIR}/src${SEP}${SERVICEDIR}/src"

# TODO: build frameworks and managers

    # Storage
    build "$STORAGEDIR/src" ist.palcom.storage.pfs
    build "$STORAGEDIR/src" ist.palcom.storage.pio
    build "$STORAGEDIR/src" ist.palcom.storage.persistent
    build "$STORAGEDIR/src" ist.palcom.storage.tags

    
    
    # Assembly manager
    # ASSEMBLYDIR must be before COMMDIR in sourcepaths here, 
    # so assembly AST classes are chosen 

#    build_with_sourcepath       \
#          "$ASSEMBLYDIR/src"  \
#          ist.palcom.assemblies \
#          "${JLIBS}/src${SEP}${COLLECTIONS}/src${SEP}${PROCESSDIR}/src${SEP}${XMLDIR}/src${SEP}${ASSEMBLYDIR}/src${SEP}${COMMDIR}/src${SEP}${BASEDIR}/src${SEP}${SERVICEDIR}/src"
#
#    # The ist.palcom.communication.ast component is built with a 
#    # non-default PRC file name,
#    # so it does not clash with the one from communication 
#    build_with_sourcepath_and_prc_name \
#          "$ASSEMBLYDIR/src"         \
#          ist.palcom.communication.ast \
#          "${JLIBS}/src${SEP}${COLLECTIONS}/src${SEP}${PROCESSDIR}/src${SEP}${XMLDIR}/src${SEP}${ASSEMBLYDIR}/src${SEP}${COMMDIR}/src${SEP}${BASEDIR}/src${SEP}${SERVICEDIR}/src" \
#          ist.palcom.communication.ast.assemblies
    
    echo ""
fi

echo Building core test programs  


build_test "$DEVICEDIR/test" ist.palcom.device.test

build_test "$PROCESSDIR/test"   ist.palcom.palcomthreads.test

build_test "$RTEUTILDIR/communication/mal-layer-util/test" \
           ist.palcom.mal.udp.test
build_test "$RTEUTILDIR/communication/communication-layer-util/test" \
           ist.palcom.connections.test
build_test "$RTEUTILDIR/communication/communication-layer-util/test" \
           ist.palcom.messaging.test
build_test "$RTEUTILDIR/communication/communication-layer-util/test" \
           ist.palcom.pubsub.test
build_test "$RESOURCEMANAGERDIR/test" ist.palcom.discovery.test

# TODO build_test "$SERVICEDIR/test"   ist.palcom.services.test.echo 

build_test "$STORAGEDIR/test"   ist.palcom.storage.test

build_test "$HGRAPHDIR/test"      ist.palcom.hgraph.test 
