#!/bin/sh -ex

#########################################################
# This Hudson script tests the new EDG interface in ROSE.
#########################################################

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

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"
# 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-new-edg-interface --enable-edg-version4 --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-ROSE_LONG_MAKE_CHECK_RULE=yes --enable-edg-version=4.0 --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-install ; then chmod -R u+wx ROSE-install ; fi
rm -rf ROSE-install


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
   echo "Building the EDG binary"
	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)"

   echo "Starting make check step"

   cd tests/CompileTests/NewEDGInterface_C_tests
   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"
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 "***********************************************************************************************"

# DQ (2/12/2010): Added the ROSE version number to the base of the output
cd ../../..
echo "Version numbers of ROSE and the different parts of ROSE."
make PrintRoseTranslatorVersion

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