#
# atlocal_win gnucobol/tests
#
# This script is a special variant of the atlocal script for testing
# a not POSIX build (VisualStudio build for example) in a POSIX environment.
#
# Copyright (C) 2003-2012, 2014-2018 Free Software Foundation, Inc.
# Written by Keisuke Nishida, Roger While, Simon Sobisch
#
# 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 <http://www.gnu.org/licenses/>.

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

COB_BIGENDIAN="no"

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

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

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

COB_OBJECT_EXT="obj"
COB_EXE_EXT=".exe"

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

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

# Test runner for manual tests, content may changed by the user
RUN_PROG_MANUAL="$COB_SRC_PATH/run_prog_manual.sh"

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

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

# 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
}

# unset all environment variables that are used in libcob
# for runtime configuration
COB_RUNTIME_CONFIG="$(_return_path "$COB_SRC_PATH/config/runtime_empty.cfg")"
for cobenv in `$COBCRUN --runtime-conf | grep COB_ | cut -d: -f2`; \
	do unset $cobenv; \
done

COB_CONFIG_DIR="$(_return_path "$COB_SRC_PATH/config")"
COB_RUNTIME_CONFIG="$(_return_path "$COB_SRC_PATH/config/runtime_empty.cfg")"
COB_COPY_DIR="$(_return_path "$COB_SRC_PATH/copy")"
COB_LIBRARY_PATH="$(_return_path "$COB_WIN_BUILDPATH"):$(_return_path ":$COB_LIBRARY_PATH")"
COB_UNIX_LF=YES
COB_MSG_FORMAT=GCC


COB_HAS_64_BIT_POINTER=`$COBCRUN --info | grep "64bit-mode" | cut -d: -f2 | cut -b2-`
if test $($COBC --info | grep -i -c "ISAM.*disabled") = 0; then
	COB_HAS_ISAM="yes"
else
	COB_HAS_ISAM="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 $($COBC --info | grep -i -c "screen .*: .*curses") = 1; then
	#	COB_HAS_CURSES="yes"
	#fi
#fi


export COBC COBCRUN COBCRUN_DIRECT RUN_PROG_MANUAL
export PATHSEP COB_OBJECT_EXT COB_EXE_EXT
export COB_CFLAGS COB_LDFLAGS COB_LIBS
export COB_CONFIG_DIR COB_RUNTIME_CONFIG COB_COPY_DIR
export LD_LIBRARY_PATH DYLD_LIBRARY_PATH SHLIB_PATH LIBPATH
export COB_LIBRARY_PATH
export COB_MSG_FORMAT COB_UNIX_LF
export COB_HAS_ISAM COB_HAS_CURSES COB_HAS_64_BIT_POINTER


# For 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
