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

# DQ (8/14/2010): This environment variable should not be reset (already set before calling this script).
# DQ (2/7/2010): Force the same version to be used as will be used with ROSE (testing).
# export ROSE_TEST_BOOST_PATH=/home/hudson-rose/tmp/opt/boost_1_37_0.gxx-4.2.4

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 "*****************************************************************************"

# 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 "x$ROSE_TEST_BUILD_STYLE" != "xRoseAnalyzeRose" ; then
   echo "This script only handles the case of ROSE_TEST_BUILD_STYLE = RoseAnalyzeRose (ROSE_TEST_BUILD_STYLE = $ROSE_TEST_BUILD_STYLE)"
   exit 1
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



# 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"
CONFIGURE_FLAGS="--with-boost=${ROSE_TEST_BOOST_PATH} --with-boost-libdir=${ROSE_TEST_BOOST_PATH}/lib --with-java=${ROSE_TEST_JAVA_PATH} --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. For testing the compilation of ROSE with
# ROSE scripts we will reuse the install tree from the ROSE-matrix builds (at least for initial testing).
# CONFIGURE_PREFIX_DIR="${HOME}/MasterInstallTree/ROSE-compile-ROSE-matrix/ROSE_TEST_BUILD_STYLE/$ROSE_TEST_BUILD_STYLE/ROSE_TEST_GCC_VERSION/$ROSE_TEST_GCC_VERSION/label/$OS_MACHINE_LABEL/ROSE-install"
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"

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
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

# Here will will build the executable (tests/testAnalysis) that we will then use to compile ROSE again.
if test -e ROSE-compile-ROSE-executable-build ; then chmod -R u+wx ROSE-compile-ROSE-executable-build ; fi
rm -rf ROSE-compile-ROSE-executable-build
mkdir ROSE-compile-ROSE-executable-build
cd ROSE-compile-ROSE-executable-build
echo "Starting configure step"
# ../configure ${CONFIGURE_FLAGS}
../configure --prefix=${CONFIGURE_PREFIX_DIR} ${CONFIGURE_FLAGS}
echo "Done with configure step"

echo "Starting compile step"

start_time_make_seconds="$(date +%s)"

echo "Test compile step"

make -j8
# sleep 5

if [ $? -ne 0 ]  ; then
     echo "fatal error during 'make'  , aborting..."
     exit 3
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 "***********************************************************************************************************"

start_time_make_check_seconds="$(date +%s)"

echo "Starting make check step"

# make -j8 check
sleep 5

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)"
echo "***********************************************************************************************************"
echo "Elapsed time for make check test: $elapsed_make_check_time_seconds sec"
echo "***********************************************************************************************************"

echo "Done with make check step"

echo "Starting make install step (so we can save a installed copy for secondary tests)"
start_time_make_distcheck_seconds="$(date +%s)"

make -j8 install
# sleep 5
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 "***********************************************************************************************************************"

echo "Now run the generated ROSE executable to compile ROSE..."

echo "Current directory:"
pwd

echo "Change directories to the parent directory."
cd ..

echo "Current directory:"
pwd

if test -e ROSE-compile-ROSE-build ; then chmod -R u+wx ROSE-compile-ROSE-build ; fi
rm -rf ROSE-compile-ROSE-build
mkdir ROSE-compile-ROSE-build
cd ROSE-compile-ROSE-build

echo "Current directory:"
pwd

echo "Starting configure step to use ROSE to compile ROSE: CONFIGURE_PREFIX_DIR = ${CONFIGURE_PREFIX_DIR}"
# ../configure ${CONFIGURE_FLAGS}
# export path=${CONFIGURE_PREFIX_DIR}

echo "Make sure that testAnalysis is in place."
ls -l ${CONFIGURE_PREFIX_DIR}/bin/testAnalysis

echo "Put testAnalysis is in the default path."
export PATH="${CONFIGURE_PREFIX_DIR}/bin:$PATH"

echo "Put boost libs into LD_LIBRARY_PATH."
# export LD_LIBRARY_PATH="/home/hudson-rose/tmp/opt/boost_1_37_0.gxx-4.2.4/lib:$LD_LIBRARY_PATH"
export LD_LIBRARY_PATH="${ROSE_TEST_BOOST_PATH}/../../boost_1_37_0.gxx-4.2.4/lib:$LD_LIBRARY_PATH"

echo "Make sure that testAnalysis is in the default path."
which testAnalysis

echo "Calling testAnalysis to make sure it works."
testAnalysis --help

echo "Calling configure to build compile tree to build ROSE using ROSE."

# DQ (8/14/2010): The environment variable ROSE_TEST_BOOST_PATH should be used (but we need to use a modified version of BOOST to test with ROSE).
# DQ (2/6/2010): Separate tests confirm that we don't need to exclude the java and Fortran support from analysis via ROSE.
# DQ (1/28/2010): Simplify the testing initially by skipping the Java support and Fortran support.
# ../configure CXX=testAnalysis ${CONFIGURE_FLAGS}
# ../configure CXX=testAnalysis --with-boost=/home/hudson-rose/tmp/opt/boost_1_37_0.gxx-4.2.4 --without-java
# ../configure CXX=testAnalysis --with-boost=/home/hudson-rose/tmp/opt/boost_1_37_0.gxx-4.2.4
# ../configure CXX=testAnalysis --with-boost=${ROSE_TEST_BOOST_PATH} --with-boost-libdir=${ROSE_TEST_BOOST_PATH}/lib --without-java
../configure CXX=testAnalysis --with-boost=${ROSE_TEST_BOOST_PATH}/../../boost_1_37_0.gxx-4.2.4 --without-java

echo "Done with configure step to use ROSE to compile ROSE"

# DQ (2/10/2010): As of 2/9/2010 ROSE can now analysis all of ROSE, not just the src directory.
# However, large parts of the RTED work had to be commented out for the case of ROSE analysis only.
# echo "Change to src directory."
# cd src

echo "Current directory where we will compile ROSE using ROSE:"
pwd

echo "Test compile step of ROSE compiling ROSE"

make -j8
# sleep 5

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

echo "Done with compile step of ROSE compiling ROSE"

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 a list of current problem areas in ROSE where we have skipped some code.
make ProblemAreas

# 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 "********************************************************************"

# echo "Ending in an error since this script is not finished yet..."
# exit 1
