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

# Run pal-vm directly from Eclipse build
# Bash script to use on e.g. linux, macosx, cygwin.

# Build configuration is selected as such (decreasing precedence):
#  1. Command line options, specifying target (see usage)
#  2. Script will try to guess, based on host system (Release)

function usage {
    echo ""
    echo "Script usage:";
    echo ""
    echo "  pal-vm [script-options] [vm-arguments] <component.prc>";
    echo ""
    echo "  Build target is selected by using a command line option."
    echo ""
    echo "  Script-options are:";
    echo "    -v   verbose";
    echo "    -h   help - print this text (use -help to see vm-arguments)";
    echo "    -d   Select debug build of default platform";
    echo "    -c   Select checked build of default platform";
    echo "    -p   Select profiling build of default platform";
    exit;
}

VERBOSE=0
DEBUG=
GPROF=0
EXE=

ROOT=`pal-dev-root`
case $OSTYPE in
cygwin*)
   DEFAULT="Cygwin"
   EXE=".exe"
   ;;
darwin*)
   DEFAULT="MacOSX"
   ;;
linux*)
   DEFAULT="Linux"
   ;;
*)
   echo $0: Cannot determine host platform
   exit 1
   ;;
esac

VM="$DEFAULT"

# 1. First priority: script options
# Collect arguments, testing for script-handled options
OPTS=""
for a 
do
if [ "$a" == "-v" ]; then VERBOSE=1; echo Verbose on;                 fi
if [ "$a" == "-h" ]; then usage;                                      fi
if [ "$a" == "-d" ]; then VM="$VM-Debug"; TYPE="-debug";  continue;   fi
if [ "$a" == "-p" ]; then VM="$VM-Profiling"; GPROF=1; TYPE="-profiling"; continue; fi
if [ "$a" == "-c" ]; then VM="$VM-Checked"; TYPE="-checked"; continue; fi
OPTS="$OPTS $a"
done

# 2. Second priority: use default release build
if [ "$VM" == "$DEFAULT" ];           then VM="$VM-Release"; fi;

# For convenience, set LD_LIBRARY_PATH on linux
# to include current directory (for loading shared libs)
if [ "$OSTYPE" = "linux" ]; then
   export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:.
   if [ $VERBOSE -eq 1 ]; then
      echo Appended current directory to LD_LIBRARY_PATH
   fi
fi

BASE=${ROOT}developer/runtime-environment/engine/pal-vm
DEVLIB=${ROOT}developer/dev-lib

# Call the executable
EXEC="${BASE}/$VM/pal-vm$TYPE$EXE"
# Removed use of variable OPTS: Hard to quote paths with spaces enough
if [ "$VERBOSE" == "1" ]; then 
   echo Executing "$EXEC" -cp "${DEVLIB}" -base "${DEVLIB}/ist.palcom.base.prc" $OPTS
   echo "in" `pwd`:
fi
if [ ! -x "$EXEC" ]; then echo "$EXEC does not exist"; usage; fi

"$EXEC" -cp "${DEVLIB}" -base "${DEVLIB}/ist.palcom.base.prc" $OPTS

if [ $GPROF -eq 1 ]; then
    echo ""
    echo To get profiling results:
    echo "gprof [-l] $EXEC gmon.out"
fi
