Index: /issm/trunk-jpl/jenkins/jenkins.sh
===================================================================
--- /issm/trunk-jpl/jenkins/jenkins.sh	(revision 25915)
+++ /issm/trunk-jpl/jenkins/jenkins.sh	(revision 25916)
@@ -1,5 +1,5 @@
 #!/bin/bash
 ################################################################################
-# This bash script manages installation of ISSM on a given Jenkins node using a
+# This script manages installation of ISSM on a given Jenkins node using a
 # configuration file passed as the only argument. This file also contains
 # details about which nightly run tests should be executed after the build has
@@ -23,12 +23,10 @@
 #Get configuration
 #Source config file{{{
-if [ $# -ne 1 ];
-then
+if [ $# -ne 1 ]; then
 	#no config file specified: exit
 	echo "no config file specified. Exiting..." >&2 # Error message to stderr.
 	exit 1
 fi
-if [ ! -f "$1" ]
-then
+if [ ! -f "$1" ]; then
 	echo "File $1 not found!" >&2 # Error message to stderr.
 	exit 1
@@ -46,8 +44,13 @@
 NUMCPUS_RUN=1
 
-
 #source configuration script
 source $1;
 #}}}
+
+if [[ $EXAMPLES_TEST -eq 1 && $MATLAB_TEST+$PYTHON_TEST+$JAVASCRIPT_TEST -ne 0 ]]; then
+	echo "When running examples tests, set *only* EXAMPLES_TEST=1"
+	exit 1
+fi
+
 #Get Operating system (OS) name{{{
 OS=$(uname -s)
@@ -62,6 +65,5 @@
 echo "             Determining Installation type            "
 echo "======================================================";
-if [ -a $ISSM_DIR/svn_revision_old ]
-then
+if [ -a $ISSM_DIR/svn_revision_old ]; then
 	SVN_PREVIOUS=$(cat $ISSM_DIR/svn_revision_old)
 	SVN_CURRENT=$SVN_REVISION_1
@@ -97,9 +99,9 @@
 	# If the contents of the externalpackages directory were modified in any
 	# way, check for changed external packages
-	if [ ! -z "$(cat $ISSM_DIR/TEMP | grep externalpackages)" ] ; then
-		echo "  -- checking for changed externalpackages... yes";
+	if [ ! -z "$(cat $ISSM_DIR/TEMP | grep externalpackages)" ]; then
+		echo "  -- checking for changed externalpackages... yes"
 		ISSM_EXTERNALPACKAGES="yes"
 	else
-		echo "  -- checking for changed externalpackages... no";
+		echo "  -- checking for changed externalpackages... no"
 		ISSM_EXTERNALPACKAGES="no"
 	fi
@@ -109,10 +111,9 @@
 	if [ ! -z "$(cat $ISSM_DIR/TEMP | grep -e "Makefile.am" -e "m4" )" ] ||
 		[ ! -f "$ISSM_DIR/bin/issm.exe" ] && [ ! -f "$ISSM_DIR/bin/issm-bin.js" ] ||
-		[ "$ISSM_EXTERNALPACKAGES" == "yes" ] ;
-	then
-		echo "  -- checking for reconfiguration... yes";
+		[ "$ISSM_EXTERNALPACKAGES" == "yes" ]; then
+		echo "  -- checking for reconfiguration... yes"
 		ISSM_RECONFIGURE="yes"
 	else
-		echo "  -- checking for reconfiguration... no";
+		echo "  -- checking for reconfiguration... no"
 		ISSM_RECONFIGURE="no"
 	fi
@@ -120,10 +121,9 @@
 	# If source files were changed in any way, recompile
 	if [ ! -z "$(cat $ISSM_DIR/TEMP | grep -e "\.cpp" -e "\.h" )" ] ||
-		[ "$ISSM_RECONFIGURE" == "yes" ] ;
-	then
-		echo "  -- checking for recompilation... yes";
+		[ "$ISSM_RECONFIGURE" == "yes" ]; then
+		echo "  -- checking for recompilation... yes"
 		ISSM_COMPILATION="yes"
 	else
-		echo "  -- checking for recompilation... no";
+		echo "  -- checking for recompilation... no"
 		ISSM_COMPILATION="no"
 	fi
@@ -134,7 +134,7 @@
 else
 	echo "Previous revision not found, this must be a fresh install"
-	echo "  -- checking for changed externalpackages... yes";
-	echo "  -- checking for reconfiguration... yes";
-	echo "  -- checking for recompilation... yes";
+	echo "  -- checking for changed externalpackages... yes"
+	echo "  -- checking for reconfiguration... yes"
+	echo "  -- checking for recompilation... yes"
 	ISSM_EXTERNALPACKAGES="yes"
 	ISSM_RECONFIGURE="yes"
@@ -168,6 +168,5 @@
 EXTERNALPACKAGES_FAILED=0;
 
-for ((i=1;i<=$NUMPACKAGES;i++))
-do
+for ((i=1;i<=$NUMPACKAGES;i++)); do
 	NUM1=$((2*$i-1))
 	NUM2=$((2*$i))
@@ -176,5 +175,5 @@
 
 	#install if requested or if previous install has not been successful
-	if [ "$ISSM_EXTERNALPACKAGES" == "yes" ] || [[ ! -d ./install && ! -d ./install-javascript ]]; then
+	if [ "$ISSM_EXTERNALPACKAGES" == "yes" ]; then # NOTE: Removed check on if 'install' directory exist
 		cd $ISSM_DIR/externalpackages/$PACKAGENAME
 
@@ -198,5 +197,5 @@
 			echo "<testcase classname=\"externalpackages\" name=\"$PACKAGENAME\"/>" >> $EXTERNAL_TEST_FILE
 		fi
-		source $ISSM_DIR/etc/environment.sh
+		source $ISSM_DIR/etc/environment-shared.sh
 
 		#If external package is rebuilt, we also need to recompile
@@ -210,4 +209,5 @@
 	fi
 done
+
 echo '</testsuite>' >> $EXTERNAL_TEST_FILE
 
@@ -220,5 +220,5 @@
 
 # Source here to include any newly installed externalpackages on the path.
-source $ISSM_DIR/etc/environment.sh
+source $ISSM_DIR/etc/environment-shared.sh
 
 if [ "$OS" == "win" ]; then
@@ -249,9 +249,7 @@
 #}}}
 #ISSM compilation yes/no                (ISSM_COMPILATION) {{{
-if [ "$ISSM_COMPILATION" == "yes" ]
-then
+if [ "$ISSM_COMPILATION" == "yes" ]; then
 	cd $ISSM_DIR
-	if [ "$ISSM_RECONFIGURE" == "yes" ]
-	then
+	if [ "$ISSM_RECONFIGURE" == "yes" ]; then
 		echo "======================================================";
 		echo "             Cleaning up and reconfiguring            "
@@ -274,7 +272,7 @@
 
 	#4: compile and install ISSM
-	echo "======================================================";
+	echo "======================================================"
 	echo "                    Compiling ISSM                    "
-	echo "======================================================";
+	echo "======================================================"
 	if [ $NUMCPUS_INSTALL -gt 1 ]; then
 		echo "Making with " $NUMCPUS_INSTALL " cpus"
@@ -310,80 +308,74 @@
 # {{{
 if [ $MATLAB_TEST -eq 1 ]; then
-#Launch all tests on different cpus
-for (( i=1;i<=$NUMCPUS_RUN;i++ ))
-do
-	#Launch matlab and the nightly run script
-	cat > $ISSM_DIR/nightlylog/matlab_run$i.m << EOF
-	warning off %necessary to avoid a log of several Go for parallel runs
-	try,
-	$(if [ "$MATLAB_NROPTIONS" = "" ]; then
-		echo "runme('output','nightly','rank',$i,'numprocs',$NUMCPUS_RUN);"
+	#Launch all tests on different cpus
+	for (( i=1;i<=$NUMCPUS_RUN;i++ )); do
+		#Launch matlab and the nightly run script
+		cat > $ISSM_DIR/nightlylog/matlab_run$i.m << EOF
+		warning off %necessary to avoid a log of several Go for parallel runs
+		try,
+			$(if [ "$MATLAB_NROPTIONS" = "" ]; then
+				echo "runme('output','nightly','rank',$i,'numprocs',$NUMCPUS_RUN);"
+			else
+				echo "runme($MATLAB_NROPTIONS,'output','nightly','rank',$i,'numprocs',$NUMCPUS_RUN);"
+			fi)
+		catch me,
+			%An error occured, get report and exit
+			message=getReport(me)
+			directory=strsplit(pwd,'/');
+			fid=fopen([issmdir '/nightlylog/matlaberror.log'], 'at');
+			fprintf(fid,'\nMatlab error occured in: %s\n\n',directory{end});
+			fprintf(fid,'%s',message);
+			fclose(fid);
+		end
+		disp('MATLABEXITEDCORRECTLY');
+		exit
+EOF
+		cd $ISSM_DIR/test/NightlyRun
+		if [ "$OS" = "win" ]; then
+			$MATLAB_PATH/bin/matlab -nodisplay -nosplash -r "addpath $ISSM_DIR_WIN/src/m/dev; devpath; addpath $ISSM_DIR_WIN/nightlylog/; matlab_run$i" -logfile $ISSM_DIR_WIN/nightlylog/matlab_log$i.log &
+		else
+			$MATLAB_PATH/bin/matlab -nodisplay -nosplash -r "addpath $ISSM_DIR/src/m/dev; devpath; addpath $ISSM_DIR/nightlylog/; matlab_run$i" -logfile $ISSM_DIR/nightlylog/matlab_log$i.log &
+		fi
+	done
+
+	#wait until matlab closes
+	if [ "$OS" = "win" ]; then
+		sleep 5;
+		echo "Waiting for matlab on windows"
+		pid=$(ps aux -W | grep MATLAB | awk '{printf("%s\n","MATLAB");}')
+		echo '-----------------------------'
+		echo "pid: $pid"
+		echo '-----------------------------'
+		while [ -n "$pid" ]; do
+			pid=$(ps aux -W | grep MATLAB | awk '{printf("%s\n","MATLAB");}')
+			sleep 1;
+		done
+		echo "DONE!"
 	else
-		echo "runme($MATLAB_NROPTIONS,'output','nightly','rank',$i,'numprocs',$NUMCPUS_RUN);"
-	fi
-	)
-	catch me,
-		%An error occured, get report and exit
-		message=getReport(me)
-		directory=strsplit(pwd,'/');
-		fid=fopen([issmdir '/nightlylog/matlaberror.log'], 'at');
-		fprintf(fid,'\nMatlab error occured in: %s\n\n',directory{end});
-		fprintf(fid,'%s',message);
-		fclose(fid);
-	end
-	disp('MATLABEXITEDCORRECTLY');
-
-	exit
-EOF
-	cd $ISSM_DIR/test/NightlyRun
-	if [ "$OS" = "win" ]; then
-		$MATLAB_PATH/bin/matlab -nodisplay -nosplash -r "addpath $ISSM_DIR_WIN/src/m/dev; devpath; addpath $ISSM_DIR_WIN/nightlylog/; matlab_run$i" -logfile $ISSM_DIR_WIN/nightlylog/matlab_log$i.log &
-	else
-		$MATLAB_PATH/bin/matlab -nodisplay -nosplash -r "addpath $ISSM_DIR/src/m/dev; devpath; addpath $ISSM_DIR/nightlylog/; matlab_run$i" -logfile $ISSM_DIR/nightlylog/matlab_log$i.log &
-	fi
-done
-
-#wait until matlab closes
-if [ "$OS" = "win" ]; then
-	sleep 5;
-	echo "Waiting for matlab on windows"
-	pid=$(ps aux -W | grep MATLAB | awk '{printf("%s\n","MATLAB");}')
-	echo '-----------------------------'
-	echo "pid: $pid"
-	echo '-----------------------------'
-	while [ -n "$pid" ]
-	do
-		pid=$(ps aux -W | grep MATLAB | awk '{printf("%s\n","MATLAB");}')
-		sleep 1;
-	done
-	echo "DONE!"
-else
-	wait
-fi
-
-#concatenate reports
-cd $ISSM_DIR/nightlylog/
-#echo 'CHECKING NIGHTLYLOG DIRECTORY'
-#echo '-----------------------------'
-#ls -la
-#echo '-----------------------------'
-
-if [ -f matlab_log.log ]; then
-	rm matlab_log.log
-fi
-
-for job in `jobs -p`
-do
-	echo "Waiting on: $job"
-	wait $job
-done
-
-for (( i=1;i<=$NUMCPUS_RUN;i++ ))
-do
-	cat matlab_log$i.log >> matlab_log.log
-done
-
-#filter out windows characters:
-cat matlab_log.log | tr -cd '\11\12\40-\176' > matlab_log.log2 && mv matlab_log.log2 matlab_log.log
+		wait
+	fi
+
+	#concatenate reports
+	cd $ISSM_DIR/nightlylog/
+	#echo 'CHECKING NIGHTLYLOG DIRECTORY'
+	#echo '-----------------------------'
+	#ls -la
+	#echo '-----------------------------'
+
+	if [ -f matlab_log.log ]; then
+		rm matlab_log.log
+	fi
+
+	for job in `jobs -p`; do
+		echo "Waiting on: $job"
+		wait $job
+	done
+
+	for (( i=1;i<=$NUMCPUS_RUN;i++ )); do
+		cat matlab_log$i.log >> matlab_log.log
+	done
+
+	#filter out windows characters:
+	cat matlab_log.log | tr -cd '\11\12\40-\176' > matlab_log.log2 && mv matlab_log.log2 matlab_log.log
 fi
 # }}}
@@ -429,102 +421,23 @@
 rm -f $ISSM_DIR/execution/*/ADOLC-*
 
-# Examples Test
+# Examples Tests
 # {{{
-# This test will allow us to check on the status of the examples.
-if [ $EXAMPLES_TEST -eq 1 ];
-then
-	FILE='runme.m'
-	cd $ISSM_DIR/examples
-
-	for dir in ./* ;
-	do
-		if [ -d "${dir}" ];
-		then
-		# Some of the examples are incomplete (on purpose). As such, we will
-		# have to populate the missing steps in order to make sure that
-		# everything is working.
-			echo "Testing directory example: $(basename $dir)"
-
-			if [ -z "$SED" ];
-			then
-				SED='sed'
-			fi
-
-			cd ${dir}
-
-			if [ "${dir}" == "./Greenland" ];
-			then
-				# Greenland is missing step 8
-				STEP_EIGHT="\n	disp('   Step 8: Plotting exercise');\n	md = loadmodel('.\/Models\/Greenland.HistoricTransient');\n	figure\n	surfmb=[]; for i=2:201; surfmb=[surfmb ...\n		md.results.TransientSolution(i).SmbMassBalance]; end\n	subplot(3,1,1); plot([1:200],mean(surfmb));\n	title('Mean Surface mass balance');\n	vel=[]; for i=2:201; vel=[vel md.results.TransientSolution(i).Vel]; end\n	subplot(3,1,2); plot([1:200],mean(vel));\n	title('Mean Velocity');\n	volume=[]; for i=2:201; volume=[volume md.results.TransientSolution(i).IceVolume]; end\n	subplot(3,1,3); plot([1:200],volume);\n	title('Ice Volume'); xlabel('years');"
-
-				$SED -i .bak 's/steps=\[1\];/steps=\[1:8\];\n\ntry\n/' $FILE
-				$SED -i .bak "s/if any(steps==8)/&${STEP_EIGHT}/" $FILE
-			elif [ "${dir}" == "./IceBridge" ];
-			then
-				$SED -i .bak 's/steps=\[1\];/steps=\[1:5\];\n\ntry\n/' $FILE
-			elif [ "${dir}" == "./IceflowModels" ];
-			then
-				# Almost nothing to this example
-				$SED -i .bak '1 s/^.*$/try\n\n&/' $FILE
-			elif [ "${dir}" == "./ISMIP" ];
-			then
-				# Eight steps... none of which are implmented in the script...
-				$SED -i .bak '1 s/^.*$/try\n\n&/' $FILE
-			elif [ "${dir}" == "./Inversion" ];
-			then
-				$SED -i .bak 's/steps=\[1\];/steps=\[1:4\];\n\ntry\n/' $FILE
-			elif [ "${dir}" == "./Jakobshavn" ];
-			then
-				$SED -i .bak 's/steps=\[1\];/steps=\[1:4\];\n\ntry\n/' $FILE
-			elif [ "${dir}" == "./Jakobshavn" ];
-			then
-				$SED -i .bak 's/steps=\[1\];/steps=\[1:4\];\n\ntry\n/' $FILE
-			elif [ "${dir}" == "./Pig" ];
-			then
-				# Step 6 is needed
-				STEP_SIX="\n disp('Needs work!')"
-				$SED -i .bak 's/steps=\[1\];/steps=\[1:7\];\n!mv DomainOutline.bkp DomainOutline.exp;\n\ntry\n/' $FILE
-				$SED -i .bak "s/if any(steps==6)/&${STEP_SIX}/" $FILE
-			elif [ "${dir}" == "./PigSensitivity" ];
-			then
-				# Step 4 is needed
-				STEP_FOUR="\n disp('Needs work!')"
-				$SED -i .bak 's/steps=\[1\];/steps=\[1:4\];\n\ntry\n/' $FILE
-				$SED -i .bak "s/if any(steps==6)/&${STEP_FOUR}/" $FILE
-			elif [ "${dir}" == "./SquareIceShelf" ];
-			then
-				# Almost nothing to this example
-				$SED -i .bak '1 s/^.*$/try\n\n&/' $FILE
-			elif [ "${dir}" == "./UncertaintyQuantification" ];
-			then
-				$SED -i .bak 's/steps=\[1\];/steps=\[1:7\];\n\ntry\n/' $FILE
-			elif [ "${dir}" == "./Data" ];
-			then
-				echo "Data directory is used by examples. No modifications required."
-			else
-				echo "Not implemented yet!"
-				$SED -i .bak '1 s/^.*$/try\n\n&/' $FILE
-			fi
-
-			if [ "${dir}" == "./Data" ];
-			then
-				./Download.sh
-			else
-				LOG_FILE="matlab_log_$(basename $dir)_examples.log"
-				echo "disp('SUCCESS');" >> $FILE
-				echo 'catch me' >> $FILE
-				echo 'message=getReport(me);' >> $FILE
-				echo "fprintf('%s',message);" >> $FILE
-				echo "disp('FAILURE');" >> $FILE
-				echo 'end' >> $FILE
-
-				$MATLAB_PATH/bin/matlab -nodisplay -nosplash -r "addpath $ISSM_DIR/src/m/dev; devpath; addpath $ISSM_DIR/nightlylog/; runme" -logfile $ISSM_DIR/nightlylog/$LOG_FILE
-				echo "starting: $(basename $dir)" >> $ISSM_DIR/nightlylog/matlab_log_examples.log
-				cat $ISSM_DIR/nightlylog/$LOG_FILE >> $ISSM_DIR/nightlylog/matlab_log_examples.log
-				echo "finished: $(basename $dir)" >> $ISSM_DIR/nightlylog/matlab_log_examples.log
-			fi
-			cd ..
-		fi
-	done
+# The following tests check that the examples (i.e. contents of examples 
+# directory, which are implementations of the tutorials found at 
+# https://issm.jpl.nasa.gov/documentation/tutorials/) can be run.
+#
+# runme files are modifed as needed to fill in statements that would otherwise 
+# be added by user.
+#
+# NOTE: 
+# - Indentation of replacement string literals (e.g. 'STEP_EIGHT') is set to 
+# 	nest cleanly in this file, but will result in unclean nesting runme file 
+# 	(which should not be an issue)
+# - Single-line string replacements in runme.m can effectively be performed 
+#	using sed. When performing multi-line replacements, perl is a better 
+#	option.
+#
+if [ $EXAMPLES_TEST -eq 1 ]; then
+	$ISSM_DIR/jenkins/examples_tests.sh
 fi
 # }}}
@@ -542,6 +455,5 @@
 
 	#look through numtests:
-	for i in `echo $testlist`
-	do
+	for i in `echo $testlist`; do
 		juLog -test=MATLAB-$i -name=Error -error=ERROR awk "/starting:$i/{flag=1;next}/finished/{flag=0} flag{print}" matlab_log.log
 		juLog -test=MATLAB-$i -name=Failure -error=FAILURE awk "/starting:$i/{flag=1;next}/finished/{flag=0} flag{print}" matlab_log.log
@@ -551,6 +463,5 @@
 	matlabExitedInError=`grep -E "Activation cannot proceed|Error in matlab_run" matlab_log.log | wc -l`
 
-	if [ $matlabExitedInError -ne 0 ]
-	then
+	if [ $matlabExitedInError -ne 0 ]; then
 		echo "----------MATLAB exited in error!----------"
 		cat matlab_log.log
@@ -570,6 +481,5 @@
 
 	#look through numtests:
-	for i in `echo $testlist`
-	do
+	for i in `echo $testlist`; do
 		juLog -test=PYTHON-$i -name=Error -error=ERROR awk "/starting:$i/{flag=1;next}/finished/{flag=0} flag{print}" python_log.log
 		juLog -test=PYTHON-$i -name=Failure -error=FAILURE awk "/starting:$i/{flag=1;next}/finished/{flag=0} flag{print}" python_log.log
@@ -579,6 +489,5 @@
 	pythonExitedInError=`grep -E "Error|Standard exception|Traceback|bad interpreter" python_log.log | wc -l`
 
-	if [ $pythonExitedInError -ne 0 ]
-	then
+	if [ $pythonExitedInError -ne 0 ]; then
 		echo "----------Python exited in error!----------"
 		cat python_log.log
@@ -595,8 +504,7 @@
 fi
 
-if [ $EXAMPLES_TEST -eq 1 ];
-then
+if [ $EXAMPLES_TEST -eq 1 ]; then
 	# Inexplicably, there are backspace chars in the error output that are causing issues
-	$SED -i .bak 's///g' matlab_log_examples.log
+	sed -i '.bak' 's///g' matlab_log_examples.log
 
 	numtests=`cat matlab_log_examples.log | grep "starting: " | wc -l`
@@ -604,6 +512,5 @@
 
 	echo "Processing: $numtests"
-	for i in `echo $testlist`
-	do
+	for i in `echo $testlist`; do
 		juLog -test=Example-$i -name=Error -error=FAILURE awk "/starting: $i/{flag=1;next}/finished: $i/{flag=0} flag{print}" matlab_log_examples.log
 	done
