#
# atlocal_win gnucobol/tests
#
# This script is a special variant of the atlocal script for testing
# a not POSIX build (OrangeC or Visual Studio build for example)
# in a POSIX environment (like MinGW or Cygwin, possibly WSL).
#
# Copyright (C) 2003-2012, 2014-2023 Free Software Foundation, Inc.
# Written by Keisuke Nishida, Roger While, Simon Sobisch, Edward Hart
#
# This file is part of GnuCOBOL.
#
# The GnuCOBOL compiler is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# GnuCOBOL is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GnuCOBOL.  If not, see <https://www.gnu.org/licenses/>.


TEMPLATE="${abs_srcdir}/testsuite.src"

COB_SRC_PATH="$(cd ${abs_srcdir}/.. ; pwd)"
COB_WIN_BUILDPATH="$COB_SRC_PATH/build_windows/x64/Debug"

COBC="cobc.exe"
COBCRUN="cobcrun.exe"
COBCRUN_DIRECT="" # used for running created executables through tools
LOCAL_ENV=""

PATH="${COB_WIN_BUILDPATH}:${PATH}"
export PATH

AWK=awk
GREP=grep
SED=sed
export AWK GREP SED

# be sure to use the English messages
LC_ALL=C
export LC_ALL
unset LANG

# define for performance checks (running code several thousand times)
# comment manually if not needed
COBOL_FLAGS="-DCHECK-PERF ${COBOL_FLAGS}"

FLAGS="-debug -Wall ${COBOL_FLAGS} -fdiagnostics-plain-output"
# workaround to adjust the testsuite later:
FLAGS="${FLAGS} -fno-diagnostics-show-option"

COMPILE="${COBC} -x ${FLAGS}"
COMPILE_ONLY="${COBC} -fsyntax-only ${FLAGS} -Wno-unsupported"
COMPILE_MODULE="${COBC} -m ${FLAGS}"
LISTING_FLAGS="-fttitle=GnuCOBOL_V.R.P -fno-ttimestamp"
COMPILE_LISTING="${COMPILE_ONLY} ${LISTING_FLAGS}"
COMPILE_LISTING0="${COMPILE_LISTING} -tlines=0"

# Helper script to unify listings (replace version, date, time)
UNIFY_LISTING="${abs_srcdir}/listings-sed.sh"

# test runner for manual tests, content may be adjusted by the user
RUN_PROG_MANUAL="${abs_builddir}/run_prog_manual.sh"

# unset option if not internally set in this script - or external
_unset_option () {
	if test "$1" != "COB_CONFIG_DIR" \
	     -a "$1" != "COB_COPY_DIR" \
	     -a "$1" != "COB_RUNTIME_CONFIG" \
	     -a "$1" != "COB_LIBRARY_PATH" \
	     -a "$1" != "COB_CFLAGS" \
	     -a "$1" != "COB_LIBS" \
	     -a "$1" != "COB_UNIX_LF" \
	     -a "$1" != "OS"; then
		unset $1
	fi
}

# possible path conversion for running the testsuite in an environment
# that doesn't match the one where the tested binaries were built
# Note: not needed for running the testsuite with MSYS as this translates the path
_return_path () {
	# For later reference only
	#echo "$1" | sed 's/\//\\/g' | sed 's/^\\\([a-z]\)\\/\1:\\/g')
	if test "$OSTYPE" = "cygwin"; then
		cygpath -pw "$1"
	else
		# check for WSL / Bash on Windows; ignore error messages (file not available)
		if $(grep -q "Microsoft" /proc/sys/kernel/osrelease 2>/dev/null); then
			echo "$1" | sed -E 's+^/mnt/(.{1})+\1:+' | sed 's+:$+:/+1'
		else
			echo "$1"
		fi
	fi
}

# Note: we explicit do not set COB_ON_CYGWIN here,
# as this is file is about running non-cygwin binaries

# Fix for testcases where cobc uses win32 paths internally
PATHSEP=";"
export PATHSEP

COB_CONFIG_DIR="$(_return_path "${abs_top_srcdir}/config")"
COB_COPY_DIR="$(_return_path "${abs_top_srcdir}/copy")"
COB_LIBRARY_PATH="$(_return_path "${abs_top_builddir}/extras")${PATHSEP}$(_return_path "${COB_WIN_BUILDPATH}")"

export COB_CFLAGS COB_LIBS
export COB_CONFIG_DIR COB_COPY_DIR
#export LD_LIBRARY_PATH DYLD_LIBRARY_PATH SHLIB_PATH LIBPATH
export COB_LIBRARY_PATH
COB_UNIX_LF=1
export COB_UNIX_LF

# unset all environment variables that are used in libcob
# for runtime configuration ...
COB_RUNTIME_CONFIG="$(_return_path "${abs_top_srcdir}/config/runtime_empty.cfg")"
export COB_RUNTIME_CONFIG
for cobenv in $(${LOCAL_ENV} ${COBCRUN} --runtime-conf \
                 | grep " env:" | cut -d: -f2 | cut -d= -f1 \
                 | grep -v "PATH" | grep -v "TERM"); \
	do _unset_option $cobenv; \
done

# prevent multiple calls by caching the output
${LOCAL_ENV} ${COBC} --verbose --info > info.out

# ... and also unset for the compiler
if test "$GNUCOBOL_TEST_LOCAL" != "1"; then
	for cobenv in $(grep "env:" info.out | cut -d: -f2 | cut -d= -f1 \
	              | grep -v "PATH"); \
		do _unset_option $cobenv; \
	done
fi

COB_STACKTRACE=0
export COB_STACKTRACE

COB_MSG_FORMAT=GCC
export COB_MSG_FORMAT

# different flags checked in the testsuite
# note: kept intended to ease merge from atlocal.in

	COB_OBJECT_EXT="$(grep COB_OBJECT_EXT info.out | cut -d: -f2 | cut -b2-)"
	COB_EXE_EXT=".exe"
	COB_MODULE_EXT="dll"

	if test $(grep -i -c "little-endian" info.out) = 0; then
		COB_BIGENDIAN="yes"
	else
		COB_BIGENDIAN="no"
	fi
	COB_HAS_64_BIT_POINTER=$(grep "64bit-mode" info.out | cut -d: -f2 | cut -b2-)

	cob_indexed=$(grep -i "indexed file" info.out | cut -d: -f2)
	if test "x$cob_indexed" = "x"; then
		cob_indexed=$(grep ISAM info.out | cut -d: -f2)
	fi
	case "$cob_indexed" in
	" disabled")	COB_HAS_ISAM="no";;
	" BDB") 		COB_HAS_ISAM="db";;
	" VBISAM"*)	COB_HAS_ISAM="vbisam";;
	" D-ISAM")	COB_HAS_ISAM="disam";;
	" C-ISAM")	COB_HAS_ISAM="cisam";;
	" EXTFH")	COB_HAS_ISAM="index_extfh";;
	*)		echo "unknown entry for indexed handler: '"$cob_indexed"' please report" && exit 1;;
	esac

	if test $(grep -i -c "XML library.*disabled" info.out) = 0; then
		COB_HAS_XML2="yes"
	else
		COB_HAS_XML2="no"
	fi
	if test $(grep -i -c "JSON library.*disabled" info.out) = 0; then
		COB_HAS_JSON="yes"
	else
		COB_HAS_JSON="no"
	fi
	# see note below
	if test $(grep -i -c " screen .*disabled" info.out) = 0; then
		COB_HAS_CURSES="yes"
	else
		COB_HAS_CURSES="no"
	fi


if test "x$MSYSTEM" != "x" -o "$OSTYPE" = "cygwin"; then
	# running MSYS builds as not-visible child processes result in
	# "Redirection is not supported" (at least with PDCurses "wincon" port)
	# --> disabling the tests for this feature
	# ncurses is known to work as long as TERM is appropriate
	if test $(grep -i -c "ncurses" info.out) != 0; then
		if test "x$MSYSTEM" != "x"; then
			TERM=""
		else
			TERM="xterm"
		fi
		export TERM
		# no change here... COB_HAS_CURSES="yes"
	else
		# manual tests are executed in separate window
		# and are visible - so no need to handle it there
		echo "$at_help_all" | grep -q "run_manual_screen" 2>/dev/null
		if test $? -ne 0; then
		   COB_HAS_CURSES="no"
		fi
	fi
fi

rm -rf info.out

# NIST tests (tests/cobol85) are executed in a separate perl process with a new environment --> export needed
export COB_HAS_ISAM COB_HAS_XML2 COB_HAS_JSON COB_HAS_CURSES COB_HAS_64_BIT_POINTER
export COBC COBCRUN COBCRUN_DIRECT RUN_PROG_MANUAL
export COB_OBJECT_EXT COB_EXE_EXT COB_MODULE_EXT

# to ensure that no external DB_HOME is polluted: unset
DB_HOME="" && export DB_HOME

# For the very rare cases where cobc/libcob may need to know if they're running in test mode:
COB_IS_RUNNING_IN_TESTMODE=1 && export COB_IS_RUNNING_IN_TESTMODE
