#!/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 tst.prc and benchmarks.prc for pal[-dev]-vm

#set -x 

function usage ()
{
    echo "Usage: pal-dev-build-tst [options]"
    echo "  options:"
    echo "       -h  display this help"
    echo "       -c  delete generated files"
    echo "       -e  use exported pal-st to build"
    echo "       -b  do not build base too (implied by -e)"
}

ROOT=`pal-dev-root`

SRCDIR="developer/runtime-environment/engine/pal-vm"
OUTDIR="developer/runtime-environment/engine/pal-vm/test"
EXTDIR="developer/runtime-environment/engine/pal-vm/src/VM/test"

BUILDBASE=1
DEV=-dev

function cleanup()
{
    echo "Removing files generated by pal-dev-build-tst"
    cd "${ROOT}"
    rm -f "$OUTDIR/tst.prc"
    rm -f "$OUTDIR/benchmarks.prc"
    rm -f "$OUTDIR/libmyextlib.so"
    rm -f "$OUTDIR/myextlib.o"
    rm -f "$OUTDIR/libmyextlib.dylib"
}

# Check arguments
for a 
do
  if [ "$a" == "-h" ]; then 
      usage
      exit
  fi
  if [ "$a" == "-c" ]; then 
      cleanup
      exit
  fi
  if [ "$a" == "-b" ]; then 
      BUILD_BASE=0
      continue
  fi
  if [ "$a" == "-e" ]; then 
      USE_EXPORTED=1
      BUILD_BASE=0
      DEV=
      continue
  fi
  echo Illegal option: $a
  usage
  exit 1
done

cd "${ROOT}"

mkdir -p "${OUTDIR}"

if [ "$BUILD_BASE" == "1" ]; then
    echo Building base
    pal-dev-build-base
fi

echo ""
echo Building tst using pal${DEV}-st
pal${DEV}-st -serviceMainClass Tst \
  -requires ist.palcom.base.io:ist.palcom.base.io.prc \
  -c "${SRCDIR}/tst/Component.pcs" -op "${OUTDIR}"

echo ""
echo Building benchmarks using pal${DEV}-st
pal${DEV}-st -serviceMainClass All `find "$SRCDIR/benchmarks/" -name "*.st"` \
       -o "$OUTDIR/benchmarks.prc"
       

echo ""
echo Building shared library:
if [ "$PALGCC" != "" ]
then
   echo libmyextlib.so with PALGCC=$PALGCC
   $PALGCC -x c -g -c -o "$OUTDIR/myextlib.o" -fPIC -Wall "$EXTDIR/myextlib.def"
   $PALGCC -shared -Wl,-soname,libmyextlib.so -o "$OUTDIR/libmyextlib.so" "$OUTDIR/myextlib.o" -lc
else
case $OSTYPE in
cygwin*)
   echo libmyextlib.so for Cygwin with gcc
   gcc -x c -g -c -o "$OUTDIR/myextlib.o" -Wall "$EXTDIR/myextlib.def"  # -fPIC not needed on windows
   gcc -shared -Wl,-soname,libmyextlib.so -o "$OUTDIR/libmyextlib.so" "$OUTDIR/myextlib.o" -lc
;;
linux*)
   echo libmyextlib.so for Linux with gcc
   gcc -x c -g -c -o "$OUTDIR/myextlib.o" -fPIC -Wall "$EXTDIR/myextlib.def"
   gcc -shared -Wl,-soname,libmyextlib.so -o "$OUTDIR/libmyextlib.so" "$OUTDIR/myextlib.o" -lc
;;
darwin*)
   echo libmyextlib.dylib for MacOSX with gcc
   gcc -x c -g -c -o "$OUTDIR/myextlib.o" -fPIC -Wall "$EXTDIR/myextlib.def"
   gcc -dynamiclib -o "$OUTDIR/libmyextlib.dylib" "$OUTDIR/myextlib.o" -lc
;;
*)
   echo Unknown platform
;;
esac
fi

echo Done. 
echo Remember you may have to set LD_LIBRARY_PATH to include 
echo "${ROOT}$OUTDIR"
echo to be able to load myextlib

exit 0
