#!/bin/bash


# Script to test, package, and transfer distributable to ISSM Web site.
# Corresponds with build generated by configuration in 
# $ISSM_DIR/jenkins/ross-debian_linux-binaries.
#

## Constants
#
LIBGFORTRAN="/usr/lib/x86_64-linux-gnu/libgfortran.so.5.0.0" # Important that this is the library itself
LIBGFORTRAN_DIST="${ISSM_DIR}/lib/libgfortran.so.5" # Important the file name matches the SONAME entry in the binaries and other shared libraries which link to it
MATLAB_NROPTIONS="'exclude',[IdFromString('Dakota')]"
MATLAB_PATH="/usr/local/MATLAB/R2019b"
PACKAGE="ISSM" # Name of directory to copy distributable files to
TARBALL_NAME="issm-linux"
TARBALL="${TARBALL_NAME}.tar.gz"

## Environment
#
export PATH="${ISSM_DIR}/bin:$(getconf PATH)" # Ensure that we pick up binaries from 'bin' directory rather than 'externalpackages'

# Check if MATLAB exists
if ! [ -d ${MATLAB_PATH} ]; then
	echo "${MATLAB_PATH} does not point to a MATLAB installation! Please modify MATLAB_PATH variable in $(basename $0) and try again."
	exit 1
fi

# Clean up from previous packaging
echo "Cleaning up existing assets"
cd ${ISSM_DIR}
rm -rf ${PACKAGE}
mkdir ${PACKAGE}

# Add/modify required binaries and libraries
cd ${ISSM_DIR}/bin

echo "Modify generic"
cat generic_static.m | sed -e "s/generic_static/generic/g" > generic.m

echo "Moving MPICH binaries to bin/"
if [ -f ${ISSM_DIR}/externalpackages/petsc/install/bin/mpiexec ]; then
	cp ${ISSM_DIR}/externalpackages/petsc/install/bin/mpiexec .
	cp ${ISSM_DIR}/externalpackages/petsc/install/bin/hydra_pmi_proxy .
elif [ -f ${ISSM_DIR}/externalpackages/mpich/install/bin/mpiexec ]; then
	cp ${ISSM_DIR}/externalpackages/mpich/install/bin/mpiexec .
	cp ${ISSM_DIR}/externalpackages/mpich/install/bin/hydra_pmi_proxy .
else
	echo "MPICH not found"
	exit 1
fi

echo "Moving libgfortran to lib/"
cp ${LIBGFORTRAN} ${LIBGFORTRAN_DIST} 2> /dev/null

# Run tests
echo "Running tests"
cd ${ISSM_DIR}/test/NightlyRun

# Check that MATLAB tests run
echo "Running MATLAB tests"

rm matlab.log 2> /dev/null

# Run MATLAB tests redirecting output to logfile and suppressing output to console
${MATLAB_PATH}/bin/matlab -nojvm -nosplash -r "try, addpath ${ISSM_DIR}/bin ${ISSM_DIR}/lib; runme(${MATLAB_NROPTIONS}); exit; catch me,fprintf('%s',getReport(me)); exit; end" -logfile matlab.log &> /dev/null

# Check that MATLAB did not exit in error
matlabExitCode=`echo $?`
matlabExitedInError=`grep -E "Activation cannot proceed|license|Error" matlab.log | wc -l`

if [[ ${matlabExitCode} -ne 0 || ${matlabExitedInError} -ne 0 ]]; then
	echo "----------MATLAB exited in error!----------"
	cat matlab.log
	echo "-----------End of matlab.log-----------"

	# Clean up execution directory
	rm -rf ${ISSM_DIR}/execution/*

	exit 1
fi

# Check that all MATLAB tests passed
numMatlabTestsFailed=`cat matlab.log | grep -c -e "FAILED|ERROR"`

if [[ ${numMatlabTestsFailed} -ne 0 ]]; then
	echo "One or more MATLAB tests FAILED"
	exit 1
else
	echo "All MATLAB tests PASSED"
fi

# Create tarball
cd ${ISSM_DIR}
rm -f ${TARBALL}
svn cleanup --remove-ignored --remove-unversioned test # Clean up test directory (before copying to package)
echo "Copying assets to package: ${PACKAGE}"
cp -rf bin examples lib scripts test ${PACKAGE}/
echo "Cleaning up unneeded/unwanted files"
rm -f ${PACKAGE}/bin/generic_static.* # Remove static versions of generic cluster classes
rm -f ${PACKAGE}/lib/*.a # Remove static libraries from package
rm -f ${PACKAGE}/lib/*.la # Remove libtool libraries from package
rm -rf ${PACKAGE}/test/SandBox # Remove testing sandbox from package
echo "Creating tarball: ${TARBALL_NAME}"
tar -czf ${TARBALL} ${PACKAGE}
ls -lah ${ISSM_DIR}/${TARBALL}

echo "Transferring binaries to ISSM Web site"
scp -i ~/.ssh/debian_linux-vm_to_ross ${TARBALL} jenkins@ross.ics.uci.edu:/var/www/html/${TARBALL}

if [ $? -ne 0 ]; then
	echo "FAILED: Manually check connection"
	exit 1
fi
