#!/bin/sh
#
# Modifications:
#   Mark C. Miller, Thu Jul 29 23:35:19 PDT 2010
#   Added option to ignore third party lib problems to cmake invokation.
#
#   Eric Brugger, Thu Mar 31 09:38:38 PDT 2011
#   Modified the script to work with the 2.2RC instead of the 2.0RC.
#
#   Eric Brugger, Tue Apr 12 10:53:02 PDT 2011
#   Removed the specular lighting tests from the skip list.
#
#   Jeremy Meredith, Tue Aug  9 17:19:55 EDT 2011
#   Updated the subversion host/path.

#
# Determine the users name and e-mail address.
#
user=`whoami`

#
# Set the user e-mail address.
#
userNersc=unknown
userEmail=unknown
case $user in
    bonnell)
        userNersc=bonnell
        userEmail=bonnell2@llnl.gov
        ;;
    kbonnell)
        userNersc=bonnell
        userEmail=bonnell2@llnl.gov
        ;;
    brugger)
        userNersc=brugger
        userEmail=brugger1@llnl.gov
        ;;
    hrchilds)
        userNersc=hrchilds
        userEmail=hank@uoregon.edu
        ;;
    mcmiller)
        userNersc=miller86
        userEmail=miller86@llnl.gov
        ;;
    miller)
        userNersc=miller86
        userEmail=miller86@llnl.gov
        ;;
    miller86)
        userNersc=miller86
        userEmail=miller86@llnl.gov
        ;;
esac

if test "$userEmail" = "unknown" ; then
    echo "Unknown user name. Exiting."
    exit 1
fi

#
# Parse the command line to determine if we should run the test suite
# on the trunk or RC.
#
debug="false"
branch="trunk"
revision="latest"
serial="false"

for abc
do
    case $1 in
        -trunk)
            branch="trunk"
            shift
            ;;
        -2.2RC)
            branch="2.2RC"
            shift
            ;;
        -serial)
            serial="true"
            shift
            ;;
        -parallel)
            serial="false"
            shift
            ;;
        -d)
            debug="true"
            shift
            ;;
        -r)
            revision=$2
            shift 2
            ;;
    esac
done

if test "$branch" = "trunk" ; then
    workingDir="test_trunk"
else
    workingDir="test_rc"
fi

#
# Run the test suite on alastor.
#
rm -f alastor
cat <<EOF > alastor
#!/bin/sh

dateTag=\`date +%y%b%d-%H:%M\`

if test "$serial" = "true" ; then
    modes="alastor,serial"
else
    modes="alastor,serial alastor,parallel alastor,scalable,parallel,icet"
fi

skipList_for_Alastor="\
    alastor,serial:tests/plots/streamline_geom.py:streamline_geom_07,streamline_geom_08,streamline_geom_09 \
    alastor,parallel:tests/plots/streamline_geom.py:streamline_geom_07,streamline_geom_08,streamline_geom_09 \
    alastor,scalable,parallel,icet:tests/plots/streamline_geom.py:streamline_geom_07,streamline_geom_08,streamline_geom_09 \
    alastor,serial:tests/operators/persistent_particles.py \
    alastor,parallel:tests/operators/persistent_particles.py \
    alastor,scalable,parallel,icet:tests/operators/persistent_particles.py \
    alastor,serial:tests/databases/itaps.py:itaps_14,itaps_15 \
    alastor,parallel:tests/databases/itaps.py:itaps_14,itaps_15 \
    alastor,scalable,parallel,icet:tests/databases/itaps.py:itaps_14,itaps_15 \
    alastor,serial:tests/rendering/tuvok.py:tuvok_1,tuvok_2,tuvok_3,tuvok_4,tuvok_6 \
    alastor,parallel:tests/rendering/tuvok.py:tuvok_1,tuvok_2,tuvok_3,tuvok_4,tuvok_6 \
    alastor,scalable,parallel,icet:tests/rendering/tuvok.py:tuvok_1,tuvok_2,tuvok_3,tuvok_4,tuvok_6 \
    alastor,scalable,parallel,icet:tests/operators/transform.py:ops_transform05 \
    alastor,parallel:tests/rendering/compositing.py \
    alastor,scalable,parallel,icet:tests/rendering/compositing.py \
    alastor,parallel:tests/session/legendproperties.py:legendproperties00,legendproperties01 \
    alastor,scalable,parallel,icet:tests/session/legendproperties.py:legendproperties00,legendproperties01"

skipList="\$skipList_for_Alastor"

# Create the test directory.
if test ! -e /nfs/tmp2/$user; then
    mkdir /nfs/tmp2/$user
fi
rm -rf /nfs/tmp2/$user/$workingDir
mkdir /nfs/tmp2/$user/$workingDir
cp visit-notify-test-failure /nfs/tmp2/$user/$workingDir
cd /nfs/tmp2/$user/$workingDir

# Check out the source
if test "$debug" = "true" ; then
   echo "Checkout of $branch started at: \`date\`" | mail -s "Checkout started" $userEmail
fi
if test "$branch" = "trunk" ; then
    if test "$revision" = "latest" ; then
        svn co svn+ssh://$userNersc@cori.nersc.gov/project/projectdirs/visit/svn/visit/trunk/src > buildlog 2>&1
        svn co svn+ssh://$userNersc@cori.nersc.gov/project/projectdirs/visit/svn/visit/trunk/data >> buildlog 2>&1
        svn co svn+ssh://$userNersc@cori.nersc.gov/project/projectdirs/visit/svn/visit/trunk/test >> buildlog 2>&1
    else
        svn co svn+ssh://$userNersc@cori.nersc.gov/project/projectdirs/visit/svn/visit/trunk/src -r $revision > buildlog 2>&1
        svn co svn+ssh://$userNersc@cori.nersc.gov/project/projectdirs/visit/svn/visit/trunk/data -r $revision >> buildlog 2>&1
        svn co svn+ssh://$userNersc@cori.nersc.gov/project/projectdirs/visit/svn/visit/trunk/test -r $revision >> buildlog 2>&1
    fi
else
    if test "$revision" = "latest" ; then
        svn co svn+ssh://$userNersc@cori.nersc.gov/project/projectdirs/visit/svn/visit/branches/2.2RC/src > buildlog 2>&1
        svn co svn+ssh://$userNersc@cori.nersc.gov/project/projectdirs/visit/svn/visit/branches/2.2RC/data >> buildlog 2>&1
        svn co svn+ssh://$userNersc@cori.nersc.gov/project/projectdirs/visit/svn/visit/branches/2.2RC/test >> buildlog 2>&1
    else
        svn co svn+ssh://$userNersc@cori.nersc.gov/project/projectdirs/visit/svn/visit/branches/2.2RC/src -r $revision > buildlog 2>&1
        svn co svn+ssh://$userNersc@cori.nersc.gov/project/projectdirs/visit/svn/visit/branches/2.2RC/data -r $revision >> buildlog 2>&1
        svn co svn+ssh://$userNersc@cori.nersc.gov/project/projectdirs/visit/svn/visit/branches/2.2RC/test -r $revision >> buildlog 2>&1
    fi
fi
if test "$debug" = "true" ; then
    echo "Checkout of $branch completed at: \`date\`" | mail -s "Checkout completed" $userEmail
fi

# Build the code
cd src
export PATH=\$PATH:/usr/bin/X11
ln -s aztec1.cmake config-site/\`hostname\`.cmake
if test "$serial" = "true" ; then
    sed -i "s/VISIT_PARALLEL ON/VISIT_PARALLEL OFF/" config-site/aztec1.cmake
fi
/usr/gapps/visit/cmake/2.8.0/linux-x86_64_gcc-4.1/bin/cmake . -DCMAKE_BUILD_TYPE=Release -DVISIT_BUILD_ALL_PLUGINS=1 -DIGNORE_THIRD_PARTY_LIB_PROBLEMS:BOOL=ON >> ../buildlog 2>&1
make -j 12 >> ../buildlog 2>&1
if test "$debug" = "true" ; then
    echo "Source build completed at: \`date\`" | mail -s "Source build completed" $userEmail
fi

# Build the test data
cd ../data
make -j 8 test >> ../buildlog 2>&1
if test "$debug" = "true" ; then
    echo "Data build completed at: \`date\`" | mail -s "Data build completed" $userEmail
fi

# Run the tests, consolidating the results in one directory
cd ../test
rm -rf \$dateTag
mkdir \$dateTag
for m in \$modes; do
    # set pixel diff-tolerances slightly above zero for mode
    # containing 'scalable'
    diffTols=""
    if test -n "\$(echo \$m | grep scalable)"; then
        diffTols="-pixdiff 2 -avgdiff 1"
    fi

    # Run the test
    ./runtest -q -s "\$skipList_for_Alastor" -m "\$m" -l 600 \$diffTols -notrackmem >> ../buildlog 2>&1

    # Move the results to the consolidation directory
    theMode=\`echo \$m | tr ',' '_'\`
    mv html \$dateTag/\$theMode
    if test "$debug" = "true" ; then
        echo "Test \$m completed at: \`date\`" | mail -s "Tests completed" $userEmail
    fi
done

# Determine if the test suite passed or failed.
hasFailed=""
for m in \$modes; do
    theMode=\`echo \$m | tr ',' '_'\`
    if test "\$hasFailed" = ""; then
        hasFailed=\`cat "\$dateTag/\$theMode/index.html" | grep -m 1 Failed 2>/dev/null\`
    fi
    if test "\$hasFailed" = ""; then
        hasFailed=\`cat "\$dateTag/\$theMode/index.html" | grep -m 1 Unacceptable 2>/dev/null\`
    fi
    if test "\$hasFailed" = ""; then
        hasFailed=\`cat "\$dateTag/\$theMode/index.html" | grep -m 1 OS-Killed 2>/dev/null\`
    fi
done

# Send out an e-mail notifying the users of the test suite status.
cd ..
if test "\$hasFailed" = ""; then
   ./visit-notify-test-failure -pass -host alastor
else
   ./visit-notify-test-failure -host alastor
fi

# If the test suite passed then update the revision number indicating the
# last pass.
if test "\$hasFailed" = ""; then
    svn update src/svn_bin/lastpass_alastor
    svnRevision=\`svn info src | grep "Revision:" | cut -d' ' -f2\`
    rm -f src/svn_bin/lastpass_alastor
    echo "\$svnRevision" > src/svn_bin/lastpass_alastor
    svn commit -m "Update the revision number of the last test suite pass." src/svn_bin/lastpass_alastor
fi

# Tar up the results
cd test
tar cf - \$dateTag | gzip > html.tar.gz

# Set the permissions so that others may access the test directory.
cd ../..
chgrp -R visit $workingDir
chmod -R g+rwX,o+rX $workingDir

EOF

chmod 750 alastor

scp alastor alastor:alastor_testit
scp visit-notify-test-failure alastor:

# Run the test suite.
ssh alastor "srun -n 1 -p pdebug -t 12:00:00 --exclusive ./alastor_testit"

# Copy the results to euclid
ssh alastor "scp /nfs/tmp2/$user/$workingDir/test/html.tar.gz $userNersc@euclid.nersc.gov:"
if test "$debug" = "true" ; then
    echo "Results copied to euclid at: `date`" | mail -s "Results copied" $userEmail
fi

# Post the results.
scp visit-update-test-website $userNersc@euclid.nersc.gov:
ssh $userNersc@euclid.nersc.gov "./visit-update-test-website"
if test "$debug" = "true" ; then
    echo "Results posted at: `date`" | mail -s "Results posted" $userEmail
fi
