#
# 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-2020 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/Win32/Release"

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

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

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

FLAGS="-debug -Wall ${COBOL_FLAGS}"
COMPILE="${COBC} -x ${FLAGS}"
COMPILE_ONLY="${COBC} -fsyntax-only ${FLAGS}"
COMPILE_MODULE="${COBC} -m ${FLAGS}"

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

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

# unset option if not internally set
_unset_option () {
	if test "$1" != "COB_CONFIG_DIR" \
	     -a "$1" != "COB_RUNTIME_CONFIG" \
	     -a "$1" != "COB_LIBRARY_PATH" \
	     -a "$1" != "COB_UNIX_LF"; 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
}

# 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_LDFLAGS 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
COB_MSG_FORMAT=GCC
export COB_UNIX_LF COB_MSG_FORMAT

# 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 `${COBCRUN} --runtime-conf | grep COB_ | cut -d: -f2`; \
	do _unset_option $cobenv; \
done


# different flags checked in the testsuite
COB_BIGENDIAN="no"

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

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

COB_HAS_64_BIT_POINTER=`${COBCRUN} --info | grep "64bit-mode" | cut -d: -f2 | cut -b2-`
if test $(grep -i -c "ISAM.*disabled" info.out) = 0; then
	COB_HAS_ISAM="yes"
else
	COB_HAS_ISAM="no"
fi
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_CJSON="yes"
else
	COB_HAS_CJSON="no"
fi
COB_HAS_CURSES="no"
#if test "x$MSYSTEM" != "x" -o "$OSTYPE" = "cygwin"; then
	# running MSYS builds as not-visible child processes result in
	# "Redirection is not supported" (old PDcurses) and exit 127 (NCurses)
	# --> disabling the tests for this feature, uncomment if fixed in the curses library
	#if test $(grep -i -c "screen .*: .*curses" info.out) = 1; then
	#	COB_HAS_CURSES="yes"
	#fi
#fi


export COB_HAS_ISAM COB_HAS_XML2 COB_HAS_CJSON COB_HAS_CURSES COB_HAS_64_BIT_POINTER
export COBC COBCRUN COBCRUN_DIRECT RUN_PROG_MANUAL
export COB_OBJECT_EXT COB_EXE_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:
case $0 in
	*/testsuite) COB_IS_RUNNING_IN_TESTMODE=1 && export COB_IS_RUNNING_IN_TESTMODE;;
	*)	unset COB_IS_RUNNING_IN_TESTMODE;;
esac
