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

# enable MPI
#MYVER=mpich2/1.1.1p1
#MYBIN=/nfs/apps/${MYVER}/bin
#MYLIB=/nfs/apps/${MYVER}/lib
#PATH=${MYBIN}:${PATH}
#LD_LIBRARY_PATH=${MYLIB}:${LD_LIBRARY_PATH}
#export PATH
#export LD_LIBRARY_PATH

set +e
source /nfs/apps/mpich2/1.1.1p1/setup.sh
set -e
echo "Checking if MPI is installed!"
mpicc --version

export OMP_NUM_THREADS=1

export mpdvar=$(ps ax | grep mpd | wc -l )
#echo $mpdvar
if (($mpdvar >= 2)); then
:
#   echo "MPD RUNNING"
 elif (($mpdvar < 2)); then
#   echo "Starting MPD"
    /nfs/apps/mpich2/1.1.1p1/bin/mpd &
#   echo "MPD started."
fi

#tps : 11/16/2010 : Lets add support for UPC
#tps : 11/22/2010 : The setup.sh has been updated by admin
UPC_TEST_MACHINE=`uname -m`
if test "$UPC_TEST_MACHINE" = "i686" ; then
  echo "UPC not tested on 32bit machines for now."
else
# Turn off bash error checking to run source with file that uses "grep" (will return error code if target is not found).
  set +e
  # This causes "symbol lookup error: /usr/libexec/gcc/x86_64-redhat-linux/4.1.2/f951: undefined symbol: __gmpn_sqr_n"
  # on all 4.1.2 compilers (only effects 64 bit tests since we only use this script on 64 bit machines).
  # This UPC support must be thought out more carefully.
  if test "x$ROSE_TEST_GCC_VERSION" != "x4.1.2" ; then
    source /nfs/apps/upc/4.5.1.2/setup.sh
  else
    echo "UPC not tested 64-bit machines using gnu 4.1.2 for now."
  fi
  set -e
fi


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
    # This is the case for g++ 3.4.6 (since there is no uniform existence of a setup.sh file).
      echo "Handling case of no $GCCROOT/setup.sh file."
		export PATH="$GCCROOT/bin:$PATH"

    # DQ (2/15/2010): For 64-bit this has to be setup to use lib64 explicitly
    # export LD_LIBRARY_PATH="$GCCROOT/lib:$LD_LIBRARY_PATH"
      if test "$MACHINE" = "i686" ; then
         export LD_LIBRARY_PATH="$GCCROOT/lib:$LD_LIBRARY_PATH"
      else
         export LD_LIBRARY_PATH="$GCCROOT/lib64:$GCCROOT/lib:$LD_LIBRARY_PATH"
      fi
	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-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-parallel_ast_traversal_mpi  CC=mpicc CXX=mpicxx"
#--with-parallel_ast_traversal_omp
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

# tps (02/04/2010) : Checking the EDG submodule version
git submodule status

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

# Note that if "$elapsed_make_time_seconds < 60" then it is an error to evaluate the expression "$elapsed_make_time_seconds / 60".
# elapsed_make_time_minutes="$(expr $elapsed_make_time_seconds / 60)"
# elapsed_make_time_modulo_seconds="$(expr $elapsed_make_time_seconds % 60)"
# if test $elapsed_make_time_seconds > 60; then
#   elapsed_make_time_modulo_seconds="$(expr ($elapsed_make_time_seconds) / 60)"
# else
#   elapsed_make_time_modulo_seconds="$elapsed_make_time_seconds"
# fi
# elapsed_make_time_modulo_seconds="0"
echo "***********************************************************************************************************"
# echo "Elapsed time for make test: $elapsed_make_time_minutes min $elapsed_make_time_modulo_seconds sec"
echo "Elapsed time for make test: $elapsed_make_time_seconds sec"
echo "***********************************************************************************************************"

# DQ (1/14/2010): I am debugging the Hudson tests of failures on tux269.
# echo "Exiting as a test to simplify elapsed time tests."
# exit 1

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

# 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
 # sleep 5

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

 # FIXME
   make -j8 distcheck
 # sleep 5

   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)"
 # if test $elapsed_make_distcheck_time_seconds > 60; then
 #    elapsed_make_distcheck_time_minutes="$(expr ($elapsed_make_distcheck_time_seconds) / 60)"
 # else
 #    elapsed_make_distcheck_time_minutes="$elapsed_make_distcheck_time_seconds"
 # fi
 # elapsed_make_distcheck_time_minutes="0"
   echo "***********************************************************************************************************************"
 # echo "Elapsed time for make distcheck test: $elapsed_make_distcheck_time_minutes min $elapsed_make_distcheck_time_seconds sec"
   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)"
 # Exit normally even if it fails so that we can debug the docs rule.
 # make -j8 docs || exit 0
 # make docs || exit 0

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

 # FIXME
   make -j8 docs
 # sleep 5

   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)"
 # elapsed_make_docs_time_minutes="$(expr $elapsed_make_docs_time_seconds / 60)"
 # elapsed_make_docs_time_minutes="0"
   echo "********************************************************************************************************"
 # echo "Elapsed time for make docs test: $elapsed_make_docs_time_minutes min $elapsed_make_docs_time_seconds sec"
   echo "Elapsed time for make docs test: $elapsed_make_docs_time_seconds sec"
   echo "********************************************************************************************************"
fi

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

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

# DQ (1/17/2010): This is evaluated as an error if when the result is 0 (when $elapsed_hudson_time_seconds is an even multiple of 60).
#elapsed_hudson_time_modulo_seconds="$(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 "Elapsed time for Hudson test: $elapsed_hudson_time_minutes min $elapsed_hudson_time_modulo_seconds sec"
echo "***********************************************************************************************"

# DQ (1/14/2010): I am debugging the Hudson tests of failures on tux269.
# echo "Exiting as a test to simplify elapsed time tests."
# exit 1

#end_time=`date`
#echo "**************************************************************************************************"
#echo "Executing runTest for ROSE Project on: $machine_name start_time = $start_time end_time = $end_time"
#echo "**************************************************************************************************"

# DQ (2/12/2010): Added the ROSE version number to the base of the output
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 "********************************************************************"
