#!/bin/sh
#$Id: simsys_run_test_examples,v 1.11 2023/03/14 01:58:22 marcum Exp $

# ******************************************************************************
# Make example case test runs.
# ******************************************************************************

# ==============================================================================
# Required definitions
# ==============================================================================

script_path="`readlink $0`"

if [ "${script_path}" = "" ]; then

	script_path="$0"
fi

valid_program_names="aflr2 aflr2c aflr3 aflr4 aflr4_caps aflr43 bloom3 uvmap"

# ==============================================================================
# Include required functions
# ==============================================================================

. "`dirname ${script_path}`/script_functions/set_current_arch_type"
. "`dirname ${script_path}`/script_functions/set_sys_dir"

# -----------------------------------------
# Set system directories
# -----------------------------------------

# ==============================================================================
# Function to list usage documentation
# ==============================================================================

script_usage ()
{
	echo "
`basename $0` [option flags] program_name

Required Program Name:

program_name	: ${valid_program_names}

Option Flags:

-h, -help	: list summary of documentation
-64		: run with 64-bit integer executable
-gs		: run sanitize debug executable
-valgrind	: run debug executable with valgrind
-valgrind-full	: run debug executable with valgrind --leak-check=full
-exe_dir name	: set executable directory for program executable
		  default is ${exe_dir}
-data_dir name	: set main data directory for input data
		  default is ${data_dir}
-doc_dir name	: set main documentation directory for input data
		  default is ${doc_dir}
-test_dir name	: set main testing directory for output data
		  default is ${test_dir}
-opt opt1,..	: run program with command line options opt1,..
		  (options must be comma separated)

Notes:

1. The executable directory must exist and contain program_name.exe,
program_name_l.exe (if running with 64-bit integers), program_name_g.exe (if
running with valgrind), or program_name_gs.exe (if running with sanitize debug executable).

2. The main data directory must exist, contain sub-directory program_name/,
and sub-directory program_name/ must contain all run data. If this directory
or its sub-directory do not exist then the directory, sub-directory, and data
will be extracted from the example files package provided in the documentation
directory. In this case, the documentation directory must exist, contain
sub-directories program_name/ and program_name/examples/, and contain the
package file program_name/examples/program_name-examples.tar.gz.

3. If the main testing directory does not exist, then the testing directory and
a sub-directory program_name/ will be created. At completion of the test runs
the testing sub-directory program_name/ will contain all output data.
"
}

#===============================================================================
# Main script
#===============================================================================

set_sys_dir
set_current_arch_type

date_suffix=".`date +%m%d%y_%H%M`"

doc_dir=${sys_dir}/doc
data_dir=${sys_dir}/examples
exe_dir=${sys_dir}/${arch_type}/bin
test_dir=${sys_dir}/testing

valgrind_suppressions_file_MacOSX="$HOME/bin/tools/valgrind_suppressions.MacOSX"

if [ -r "${valgrind_suppressions_file_MacOSX}" ] && [ "`uname -s`" = "Darwin" ]; then
	valgrind_suppressions="--suppressions=${valgrind_suppressions_file_MacOSX}"
else
	valgrind_suppressions=
fi

debug=
debugger=
debugger_options=
options=
program_name=
sanitizer=

while [ X"$1" != X-- ]; do

	case "$1"
	in

		"")		set -- -- $@
				;;

		-h,help)	script_usage
				exit
				;;

		-64)		int64="yes"
				shift 1
				;;

		-gs)		debug="sanitizer"
				shift 1
				;;

		-valgrind)	debug="valgrind"
				debugger="valgrind ${valgrind_suppressions}"
				shift 1
				;;

		-valgrind-full)	debug="valgrind"
				debugger="valgrind ${valgrind_suppressions} --leak-check=full"
				shift 1
				;;

		-data_dir)	data_dir=$2
				shift 2
				;;

		-doc_dir)	doc_dir=$2
				shift 2
				;;

		-exe_dir)	exe_dir=$2
				shift 2
				;;

		-test_dir)	test_dir=$2
				shift 2
				;;

		-opt)		options=$2
				shift 2
				;;

		-*)		echo "*** $1 is not a valid option flag ***"
				script_usage
				exit
				;;

		*)		program_name=$1
				shift 1
				;;
	esac
done

	if [ ! -d ${exe_dir} ]; then

		echo "*** executable directory ${exe_dir} not found ***"
		exit
	fi

if [ "${program_name}" = "" ]; then

	echo "*** no program name specified ***"
	script_usage
	exit
fi

found=

for valid_program_name in ${valid_program_names}; do

	if [ "${valid_program_name}" = "${program_name}" ]; then

		found="yes"
		break
	fi
done

if [ "${found}" = "" ]; then

	echo "*** ${program_name} is not a valid program name ***"
	script_usage
	exit
fi

if [ ! -d ${exe_dir} ]; then

	echo "*** executable directory ${exe_dir} not found ***"
	exit
fi

if [ "${debug}" = "valgrind" ]; then

	file_exe_suffix=".valgrind"
	run_suffix="_g.exe"

elif [ "${debug}" = "sanitizer" ]; then

	file_exe_suffix=".sanitize"
	run_suffix="_gs.exe"

elif [ "${int64}" = "yes" ]; then

	file_exe_suffix=".int64"
	run_suffix="_l.exe"

else
	file_exe_suffix=
	run_suffix=".exe"
fi

run_prefix="${exe_dir}/"

if [ ! -d "${data_dir}" ]; then

	mkdir ${data_dir}
fi

data_dir="${data_dir}/${program_name}"

file_suffix="${file_exe_suffix}${date_suffix}.${arch_type}"

if [ ! -d ${data_dir} ]; then

	if [ "${program_name}" = "aflr4_caps" ]; then

		doc_dir="${doc_dir}/aflr4/examples"
	else
		doc_dir="${doc_dir}/${program_name}/examples"
	fi

	if [ ! -d "${doc_dir}" ]; then

		echo "*** data directory ${data_dir} not found ***"
		echo "*** documentation directory ${doc_dir} not found ***"
		exit
	fi

	mkdir ${data_dir}

	cd ${data_dir}

	data_dir="`pwd`"

	tar zxpvf ${doc_dir}/${program_name}-examples.tar.gz
else
	cd ${data_dir}

	data_dir="`pwd`"
fi

if [ ! -d "${test_dir}" ]; then

	mkdir ${test_dir}
fi

test_dir="${test_dir}/${program_name}"

if [ ! -d "${test_dir}" ]; then

	mkdir ${test_dir}
fi

rsync -azuv --exclude=*.bak --exclude=.* --progress ${data_dir}/ ${test_dir}/

cd ${test_dir}

test_dir="`pwd`"

output_file="run_test_examples${file_suffix}.log"

test_script_file="run_test_examples${file_suffix}.sh"

if [ -e "${test_script_file}" ]; then

	/bin/rm -f ${test_script_file}
fi

if [ "${program_name}" = "aflr4_caps" ]; then
	program_name=aflr4
fi

cat run_all_examples.sh | sed \
-e "2a\\
${debugger} ${exe_dir}/${program_name}${run_suffix} -version" \
-e "2a\\
debug=\"${debug}\"" \
-e "/ -qchk/ s,${program_name},QCHK_EXE," \
-e "s,${program_name} ,${debugger} ${exe_dir}/${program_name}${run_suffix} ," \
-e "s,QCHK_EXE,${exe_dir}/${program_name}${run_suffix}," \
> ${test_script_file}

chmod u+x ${test_script_file}

option_list="`echo ${options} | sed -e \"s/,/ /g\"`"

echo "
`pwd`
./${test_script_file} ${option_list} >| ${output_file}
"

./${test_script_file} ${option_list} >| ${output_file} 2>&1 &

