#!/bin/sh -ex

# We need to be using compiler specific versions of Boost.
# Set the path to the compiler specific version of Boost.
# export ROSE_TEST_BOOST_PATH=${ROSE_TEST_BOOST_PATH}-gnu_${ROSE_TEST_GCC_VERSION}/lib

export LD_LIBRARY_PATH="${ROSE_TEST_BOOST_PATH}/lib:$LD_LIBRARY_PATH"

echo "*******************************************************************"
echo "         Output variables required by Hudson runTest script."
echo "*******************************************************************"
echo "LD_LIBRARY_PATH                   = $LD_LIBRARY_PATH"
echo "ROSE_TEST_BUILD_SKIP_BUILD_SCRIPT = ${ROSE_TEST_BUILD_SKIP_BUILD_SCRIPT}"
echo "ROSE_TEST_BUILD_STYLE             = ${ROSE_TEST_BUILD_STYLE}"
echo "ROSE_TEST_BOOST_PATH              = ${ROSE_TEST_BOOST_PATH}"
echo "ROSE_TEST_JAVA_PATH               = ${ROSE_TEST_JAVA_PATH}"

echo "Environment variables required for ROSE_TEST_BUILD_STYLE = full"
echo "ROSE_TEST_QT_PATH                 = ${ROSE_TEST_QT_PATH}"
echo "ROSE_TEST_SQLITE_PATH             = ${ROSE_TEST_SQLITE_PATH}"
echo "ROSE_TEST_RTED_PATH               = ${ROSE_TEST_RTED_PATH}"
echo "*******************************************************************"

# Output the name of the machine where the build is being run (helpful for debugging).
machine_name=`uname -n`
start_time=`date`
start_time_seconds="$(date +%s)"
echo ""
echo "*****************************************************************************"
echo "Executing runTest for ROSE Project on: $machine_name start_time = $start_time"
# echo "Build Disk Usage Report:"
# df . || pwd
echo "*****************************************************************************"

# Provide a disk usage report to support debugging when Hudson has a problem.
# echo "Disk usage report #1 (to support debugging when Hudson has a problem):"
# df /export

if test "x$ROSE_TEST_GCC_VERSION" != "x4.1.2" ; then
	GCCROOT=/usr/apps/gcc/$ROSE_TEST_GCC_VERSION
	if test -e $GCCROOT/setup.sh ; then
		set +ex
		. $GCCROOT/setup.sh
		set -ex
	else
		export PATH="$GCCROOT/bin:$PATH"
		export LD_LIBRARY_PATH="$GCCROOT/lib:$LD_LIBRARY_PATH"
	fi
fi


# if test false; then
ROSE_EXIT_TO_TEST_HUDSON=no
if test "x$ROSE_EXIT_TO_TEST_HUDSON" = "xyes" ; then
# ************************************************************************
# Force specific failures as part of testing a restricted subset of tests.
# ************************************************************************
if test $ROSE_TEST_BUILD_STYLE = edg ; then
 # DQ (1/14/2010): I am debugging the Hudson tests of failures on tux269.
   echo "This test is forced to exit with an error as part of debugging Hudson tests."
   exit 1
fi

if test \( "x$ROSE_TEST_GCC_VERSION" = "x4.3.2" \) ; then
 # DQ (1/14/2010): I am debugging the Hudson tests of failures on tux269.
   echo "This test is forced to exit with an error as part of debugging Hudson tests."
   exit 1
fi

if test "x$ROSE_TEST_BUILD_STYLE" = "xcmake" ; then

 # DQ (1/14/2010): I am debugging the Hudson tests of failures on tux269.
   echo "This test is forced to exit with an error as part of debugging Hudson tests."
   exit 1
fi
# ************************************************************************
fi



if test "x$ROSE_TEST_BUILD_STYLE" = "xcmake" ; then
	rm -rf ROSE-build-cmake
	mkdir ROSE-build-cmake
	cd ROSE-build-cmake
	cmake .. -DBOOST_ROOT=${ROSE_TEST_BOOST_PATH} && \
	make -j8

   if [ $? -ne 0 ]  ; then
      echo "fatal error in cmake build , aborting..."
      exit 3
   fi

 # Provide a disk usage report to support debugging when Hudson has a problem.
 # echo "Disk usage report #2 (to support debugging when Hudson has a problem):"
 # df /export

	exit 0
fi

# Note that Boost 1.39 and greater require using "--with-boost-libdir"
# CONFIGURE_FLAGS="--with-boost=${ROSE_TEST_BOOST_PATH} --with-boost-libdir=${ROSE_TEST_BOOST_PATH}/lib --with-java=${ROSE_TEST_JAVA_PATH} --with-ROSE_LONG_MAKE_CHECK_RULE=yes --enable-dq-developer-tests"
# CONFIGURE_FLAGS="--with-boost=${ROSE_TEST_BOOST_PATH} --with-boost-libdir=${ROSE_TEST_BOOST_PATH}/lib --with-java=${ROSE_TEST_JAVA_PATH} --with-ROSE_LONG_MAKE_CHECK_RULE=yes"
CONFIGURE_FLAGS="--with-boost=${ROSE_TEST_BOOST_PATH} --with-boost-libdir=${ROSE_TEST_BOOST_PATH}/lib --with-java=${ROSE_TEST_JAVA_PATH} --with-ROSE_LONG_MAKE_CHECK_RULE=yes --with-CXX_DEBUG=-g --with-C_DEBUG=-g --with-CXX_WARNINGS=-Wall"

# Setup the prefix directory so that an install tree can be generated 
# for use with secondary testing of other projects using ROSE.
# We want the install tree for all the different versions of ROSE in the 
# Hudson home directory so that any secondary tests will have it avaiable
# for testing any platform.
# Note that running make install is very quick so not a problem for a 
# remotely mounted directory.
# CONFIGURE_PREFIX_DIR="${PWD}/ROSE-install"
MACHINE=`uname -m`
if test "$MACHINE" = "i686" ; then
  OS_MACHINE_LABEL=i686-linux
else
  OS_MACHINE_LABEL=amd64-linux
fi

# Define the platform specific install directory for ROSE (using same names as in rest of Hudson tests).
# This directory will be built into the Hudson home directory.
CONFIGURE_PREFIX_DIR="${HOME}/MasterInstallTree/ROSE-matrix/ROSE_TEST_BUILD_STYLE/$ROSE_TEST_BUILD_STYLE/ROSE_TEST_GCC_VERSION/$ROSE_TEST_GCC_VERSION/label/$OS_MACHINE_LABEL/ROSE-install"
echo "ROSE install tree will be: $CONFIGURE_PREFIX_DIR"

if test "x$ROSE_TEST_BUILD_STYLE" = "xfull" ; then

 # DQ (1/14/2010): I am debugging the Hudson tests of failures on tux269.
 # echo "This test is forced to exit with an error as part of debugging a subset of the Hudson tests."
 # exit 1

	CONFIGURE_FLAGS="${CONFIGURE_FLAGS} --with-qt=${ROSE_TEST_QT_PATH} --with-roseQt --with-sqlite3=${ROSE_TEST_SQLITE_PATH} --with-rted=${ROSE_TEST_RTED_PATH}"
	if test "x$ROSE_TEST_GCC_VERSION" = "x4.1.2" ; then
		CONFIGURE_FLAGS="${CONFIGURE_FLAGS} --with-pch"
	fi
fi

echo "CONFIGURE_FLAGS = ${CONFIGURE_FLAGS}"

# DQ (1/14/2010): Support for dumping the enviroment so that it 
# can be easily sourced to permit Hudson tests to be reproduced.
ROSE_DUMP_HUDSON_ENV=yes
if test "x$ROSE_DUMP_HUDSON_ENV" = "xyes" ; then
   echo "Dumping the Hudson environment...."
#   rm -f HudsonEnvironment
   echo "# Required environment variables for reproducing Hudson tests" > HudsonEnvironment
   echo "export PATH=$PATH" >> HudsonEnvironment
   echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH" >> HudsonEnvironment
   echo "export ROSE_TEST_JAVA_PATH=$ROSE_TEST_JAVA_PATH" >> HudsonEnvironment
   echo "export ROSE_TEST_BOOST_PATH=$ROSE_TEST_BOOST_PATH" >> HudsonEnvironment
   echo "export ROSE_CONFIGURE_FLAGS=\"$CONFIGURE_FLAGS\"" >> HudsonEnvironment
   echo "export ROSE_TEST_SQLITE_PATH=$ROSE_TEST_SQLITE_PATH" >> HudsonEnvironment
   echo "export ROSE_TEST_RTED_PATH=$ROSE_TEST_RTED_PATH" >> HudsonEnvironment
   echo "export ROSE_TEST_QT_PATH=$ROSE_TEST_QT_PATH" >> HudsonEnvironment

   echo "Finished dumping the Hudson environment into: $PWD/HudsonEnvironment"
   cat $PWD/HudsonEnvironment

 # echo "Exiting after dumping the environment..."
 # exit 1
fi

# Skip this for now but uncomment before checkin!
if test "x$ROSE_TEST_BUILD_SKIP_BUILD_SCRIPT" = "xyes" ; then
   echo "Skipping build at part of testing from makefile rule."
else
   echo "Hudson tests on a new branch must run the ROSE build script."
   ./build
fi

if test -e ROSE-build ; then chmod -R u+wx ROSE-build ; fi
rm -rf ROSE-build
mkdir ROSE-build
cd ROSE-build
echo "Starting configure step"
# ../configure ${CONFIGURE_FLAGS}
../configure --prefix=${CONFIGURE_PREFIX_DIR} ${CONFIGURE_FLAGS}
echo "Done with configure step"

if test $ROSE_TEST_BUILD_STYLE = edg ; then
	make -j8 upload_edg_binary && \
	make -Csrc/frontend/CxxFrontend deploy_tarballs
      if [ $? -ne 0 ]  ; then
        echo "fatal error when build edg binaries , aborting..."
        exit 3
      fi

	exit 0
fi

# Provide a disk usage report to support debugging when Hudson has a problem.
# echo "Disk usage report #3 (to support debugging when Hudson has a problem):"
# df /export

# Andreas suggests that the use of "tee" is not passing error through to Hudson.
# DQ has confirmed that Andreas's point is true for bash but false for tcsh 
# (where it was originally tested).
echo "Starting compile step"

start_time_make_seconds="$(date +%s)"

# if test false; then
ROSE_SPEW_ANALYSIS=no
if test "x$ROSE_SPEW_ANALYSIS" = "xyes" ; then

 # DQ (1/14/2010): I am debugging the Hudson tests of failures on tux269.
   echo "It should be impossible to reach this branch (exiting in false branch)."
   exit 1

   make -j8 2>&1 | tee make_output.txt

# Filter out the legitimate lines specific to compilation and linking (filter compile, links, and Qt specific tools).
   echo "Building the filtered_make_output.txt file"
   grep -v "Entering directory" make_output.txt | grep -v "Leaving directory" | grep -v "COMPILE" | grep -v "LINK" | grep -v "Qt-MOC" | grep -v "Qt-RCC" | grep -v "Qt-UIC" > filtered_make_output.txt
   echo "Built filtered_make_output.txt"

# Now process to count the number of lines of spew.
   echo "Computing compile time spew"
   grep -n "Making all in " filtered_make_output.txt | ../scripts/checkMakeSpew.pl | tee makeSpewLineCount.txt
   echo "Done with compile time spew"
else
   echo "Test compile step"

   make -j8
 # sleep 5

   if [ $? -ne 0 ]  ; then
        echo "fatal error during 'make'  , aborting..."
        exit 3
   fi
fi
echo "Done with compile step"

end_time_make_seconds="$(date +%s)"
elapsed_make_time_seconds="$(expr $end_time_make_seconds - $start_time_make_seconds)"

echo "***********************************************************************************************************"
echo "Elapsed time for make test: $elapsed_make_time_seconds sec"
echo "***********************************************************************************************************"

# GCC 4.3 and 4.4 do not currently pass make check
# Known unresolved issues:
#  - RTED test failures due to obsolete STL headers used in test suite
#  - Unparser generates (C++ and Fortran) code that is incompatible with these compilers
# if test \! \( "x$ROSE_TEST_GCC_VERSION" = "x4.3.2" -o "x$ROSE_TEST_GCC_VERSION" = "x4.4.1" \) ; then

   start_time_make_check_seconds="$(date +%s)"

 # DQ (12/9/2009): Save output from "make check" rule and measure the test spew.
 # make -j8 check
   echo "Starting make check step"
 # Andreas suggests that the use of "tee" is not passing error through to Hudson.
 # make -j8 check 2>&1 | tee make_check_output.txt;

   make -j8 check

 # Or perhaps: ( false || echo "testing" ) | tee make_check_output.txt
 # ( make -j8 check || echo "error detected in make check rule"; exit 1 ) 2>&1 | tee make_check_output.txt
   if [ $? -ne 0 ]  ; then
        echo "fatal error during 'make check'  , aborting..."
        exit 3
   fi

   end_time_make_check_seconds="$(date +%s)"
   elapsed_make_check_time_seconds="$(expr $end_time_make_check_seconds - $start_time_make_check_seconds)"
 # elapsed_make_check_time_minutes="$(expr $elapsed_make_check_time_seconds / 60)"
 # if test $elapsed_make_check_time_seconds > 60; then
 #    elapsed_make_check_time_minutes="$(expr ($elapsed_make_check_time_seconds) / 60)"
 # else
 #    elapsed_make_check_time_minutes="$elapsed_make_check_time_seconds"
 # fi
 # elapsed_make_check_time_minutes="0"
   echo "***********************************************************************************************************"
 # echo "Elapsed time for make check test: $elapsed_make_check_time_minutes min $elapsed_make_check_time_seconds sec"
   echo "Elapsed time for make check test: $elapsed_make_check_time_seconds sec"
   echo "***********************************************************************************************************"

   echo "Done with make check step"
 # grep -n "Making check in " make_check_output.txt | ../scripts/checkMakeSpew.pl | tee makeCheckSpewLineCount.txt

 # The distcheck rule builds a distribution and reruns all the tests; including the rules: install, installcheck
   echo "Starting make distcheck step"

   start_time_make_distcheck_seconds="$(date +%s)"

   make -j8 distcheck

   if [ $? -ne 0 ]  ; then
        echo "fatal error during 'make distcheck'  , aborting..."
        exit 3
   fi

   echo "Done with make distcheck step"

 # DQ (1/17/2010): Added install so we can have a frequently updated install tree upon which to base secondary 
 # tests (e.g. internal software developed but not distributed with ROSE because it is not mature enough).
 # This also test the install rull, but it is alos run and tested using "make installcheck" as part of the 
 # make distcheck" rule just run (before this step).  This "make install" rule runs in a few seconds and is
 # not a bottleneck to the Hudson tests.
   echo "Starting make install step (so we can save a installed copy for secondary tests)"
   make -j8 install
   echo "Done with make install step (so we can save a installed copy for secondary tests)"

   end_time_make_distcheck_seconds="$(date +%s)"
   elapsed_make_distcheck_time_seconds="$(expr $end_time_make_distcheck_seconds - $start_time_make_distcheck_seconds)"
   echo "***********************************************************************************************************************"
   echo "Elapsed time for make distcheck test: $elapsed_make_distcheck_time_seconds sec"
   echo "***********************************************************************************************************************"
# fi

# Provide a disk usage report to support debugging when Hudson has a problem.
# echo "Disk usage report #5 (to support debugging when Hudson has a problem):"
# df /export

# DQ (12/16/2009): This appears to work everywhere byt on the 4.3.2 systems, verify this.
# Finally run the "make docs" test (on just the 4.2.4 version of gcc).
# if test \! \( "x$ROSE_TEST_GCC_VERSION" = "x4.3.2" \); then

   echo "Starting make docs step (exit normally even if it fails so that we can debug the docs rule)"
 # Running with parallelism here does not tend to be very helpful since 
 # doxygen is what has to run and it does not run in parallel.

   start_time_make_docs_seconds="$(date +%s)"

   make -j8 docs

   if [ $? -ne 0 ]  ; then
        echo "fatal error during 'make docs'  , aborting..."
        exit 3
   fi
   echo "Done with make docs step"

   end_time_make_docs_seconds="$(date +%s)"
   elapsed_make_docs_time_seconds="$(expr $end_time_make_docs_seconds - $start_time_make_docs_seconds)"
   echo "********************************************************************************************************"
   echo "Elapsed time for make docs test: $elapsed_make_docs_time_seconds sec"
   echo "********************************************************************************************************"
# fi

end_time_seconds="$(date +%s)"
elapsed_hudson_time_seconds="$(expr $end_time_seconds - $start_time_seconds)"

# Note that this is always over 60 seconds so I think we should keep this.
elapsed_hudson_time_minutes="$(expr $elapsed_hudson_time_seconds / 60)"

echo "***********************************************************************************************"
echo "Elapsed time for Hudson test (total minutes): $elapsed_hudson_time_minutes"
echo "Elapsed time for Hudson test (total seconds): $elapsed_hudson_time_seconds"
echo "***********************************************************************************************"

# Output the date and time of day at the end of the test (helpful for debugging).
end_date=`date`
echo ""
echo "********************************************************************"
echo "Executed runTest for ROSE Project on: $machine_name date = $end_date"
echo "********************************************************************"
