#!/bin/bash

# this script builds different GNU APL variants and tests some of them

# figure and normalize some directory names
#
LOG_dir="/tmp"    # where to store longer build logs
START_dir=`pwd`
cd `dirname $0` 
BUILD_dir=`pwd`
ROOT_dir=`dirname $BUILD_dir`
SRC_dir="$ROOT_dir/src"

# remove old build results and start a fresh summary file
#
rm -f "$BUILD_dir"/apl-*
echo "Build Summary:" > "$BUILD_dir/summary"

# build one configuration
#
build_one()
{
   local time_FROM=`date +%s`
   cd $ROOT_dir
   echo "configuring for configuration \"$1\"..."
   {
     set -o xtrace

     ./configure $2
     set +o xtrace
   } > "/tmp/configure-$1" 2>&1
   echo "building configuration \"$1\"..."
   cd $ROOT_dir
   {
     echo "building \"$1\"..."
     make clean
     echo "
======== make clean done ======================================================
"
     make -j4 all
   } > "/tmp/build-$1"
   local make_result=$?
   local time_TO=`date +%s`
   local secs=$(( $time_TO - $time_FROM ))
   local summ="$BUILD_dir/summary"
   if [ $make_result -eq 0 ] 
   then
       echo "    make OK" >> $summ
       if [ -f "$SRC_dir/apl" ]
       then
           mv -f "$SRC_dir/apl" "$BUILD_dir/apl--$1"
       elif [ -f "$SRC_dir/libapl.so" ]
       then
           mv -f "$SRC_dir/libapl.so" "$BUILD_dir/"
       else
           echo "**** no file $SRC_dir/apl" >> $summ
       fi
       echo "    build $1: OK ($secs seconds)" >> $summ
   else
       echo "make returned: $make_result" >> $summ
       echo "*** build $1: FAILED ($secs seconds)" >> $summ
   fi
}

# test one configuration
#
test_one()
{
   echo "testing configuration \"$1\"..."
   local time_FROM=`date +%s`
   local summ="$BUILD_dir/summary"
   cd $SRC_dir
   make test > /dev/null 2>&1
   local make_result=$?
   local time_TO=`date +%s`
   local secs=$(( $time_TO - $time_FROM ))
   if [ $make_result -eq 0 ] 
      then echo "    test $1: OK ($secs seconds)"     >> $summ
      else echo "*** test $1: FAILED ($secs seconds)" >> $summ
   fi
}

cd $ROOT_dir
autoreconf

# shortcuts for various build options
STD="--enable-maintainer-mode"
LIB="--with-libapl"
DEV="DEVELOP_WANTED=yes"
RAT="RATIONAL_NUMBERS_WANTED=yes"

PER="PERFORMANCE_COUNTERS_WANTED=yes"
DYL="DYNAMIC_LOG_WANTED=yes"
CC_3="CORE_COUNT_WANTED=-3"		# ⎕SYL controls the nubber fo cores
noVC="VALUE_CHECK_WANTED=no"
noVH="VALUE_HISTORY_WANTED=no"
AL0="ASSERT_LEVEL_WANTED=0"             # no ASSERTions
FAST="noVC noVH AL0"

# the standard configuration
#
build_one "standard" "$STD"
test_one  "standard"

# the developer configuration
#
build_one "develop" "$STD $DEV"
test_one "develop"


# the developer configuration with rational number support
#
build_one "rational" "$STD $DEV $RAT"


# apl as library
#
build_one "libapl" "$STD $LIB"


# parallel benchmarking
#
build_one "parallel_bench" "$STD $FAST $PER $DYL $CC_3"


cat "$BUILD_dir/summary"

cd $START_dir	# undo all cds in this script

