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

# Builds the basic developer tools and libraries without needing to
# start Eclipse.

QUIET=0
BUILD_ARGS=
ROOT=`pal-dev-root`

function usage ()
{
    echo "Usage: pal-dev-build-for-palvm [options]"
    echo "       Build the basic developer tools and libraries for pal-vm."
    echo "  options:"
    echo "       -h  display this help"
    echo "       -r  build pal-vm with resource counters enabled"
    echo "       -d  do NOT build VM-developer versions of pal-vm"
    echo "       -q  be quiet - display output only when error"
}

# Check arguments
for a 
do
  if [ "$a" == "-h" ]; then 
      usage
      exit
  fi
  if [ "$a" == "-q" ]; then 
      QUIET=1
      continue
  fi
  if [ "$a" == "-d" ]; then 
      NODEV=1
      continue
  fi
  if [ "$a" == "-r" ]; then 
      RESCOUNTING=1
      continue
  fi
  echo Illegal option: $a
  usage
  exit 1
done

function error ()
{
    echo ""
    echo "pal-dev-build-for-palvm: Stopped because of build error(s)"
    echo "               during pal-dev-$1"
    if [ $QUIET -eq 1 ]; then
	echo "Output from build is:"
	cat $1.txt
    fi
    exit 1
}

function build ()
{
    suite=build-$1
    desc=$2
    
    echo ''; echo "###### Building $desc ######"
    trap "error $suite" EXIT  # execute "error $suite" on exit
    if [ $QUIET -eq 1 ]; then
	echo "Output in $suite.txt"
	pal-dev-$suite $BUILD_ARGS > $suite.txt 2>&1 
    else
	pal-dev-$suite $BUILD_ARGS 
    fi
}

set -e  # exit on error

echo ''; echo "###### Building tools and libraries for Pal-VM ######" 

build tools  "developer tools"
build base   "basic smalltalk libraries"
build j-libs "basic java libraries"
build core   "core components"

if [ "$RESCOUNTING" == "1" ]; then
    BUILD_ARGS="-r"
fi 
if [ "$NODEV" == "1" ]; then
    BUILD_ARGS="${BUILD_ARGS} -d"
fi 
build vm     "virtual machine"
BUILD_ARGS=

echo ''; echo '###### Done ######'

if [ $QUIET -eq 1 ]; then
    echo ''; echo -n 'Remove build output files now? (y/n) '
    read answer
    if [ "$answer" != "n" ]; then
	rm -f build-tools.txt
	rm -f build-base.txt
	rm -f build-j-libs.txt
	rm -f build-core.txt
    fi
fi

trap - EXIT  # execute nothing on exit
exit 0
