#!/bin/sh

# Swirc configure script
#
# SPDX-FileCopyrightText: Copyright 2016-2025 Markus Uhlin
# SPDX-License-Identifier: BSD-3-Clause

MAKE_DEF_FILE=options.mk

. "posixshell/check_etext.sh"
. "posixshell/check_strcasestr.sh"
. "posixshell/check_timingsafe_memcmp.sh"
. "posixshell/fix_cflags.sh"
. "posixshell/link_with_gnu_libidn.sh"
. "posixshell/link_with_gnu_libintl.sh"
. "posixshell/link_with_hunspell.sh"
. "posixshell/link_with_libiconv.sh"
. "posixshell/link_with_libnotify.sh"
. "posixshell/set_common.sh"

. "posixshell/os_BSD.sh"
. "posixshell/os_GNU.sh"
. "posixshell/os_LINUX.sh"
. "posixshell/os_LINUX_suncc.sh"
. "posixshell/os_MAC.sh"
. "posixshell/os_NETBSD.sh"

_sanitize_address=0
_sanitize_thread=0

_fuzz_mode=0
_with_libnotify=0
_without_libiconv=0
_without_libidn=0
_without_libintl=0

print_help () {
	echo ""
	echo "Configuration options:"
	echo ""
	echo "  --with-libnotify   Enable support for desktop notifications"
	echo "  --without-libiconv Build without GNU libiconv"
	echo "  --without-libidn   Build without GNU libidn"
	echo "  --without-libintl  No internationalization"
	echo ""
	if [ "$(whoami)" = "maxxe" ]; then
		echo "  --sanitize-address"
		echo "  --sanitize-thread"
		echo "  --fuzz-mode"
		echo ""
		echo "ASAN_OPTIONS=\"log_path=asan.log\""
		echo "TSAN_OPTIONS=\"log_path=tsan.log\""
	fi
}

for arg in "$@"; do
	case "$arg" in
	"--sanitize-address")
		_sanitize_address=1
		;;
	"--sanitize-thread")
		_sanitize_thread=1
		;;
	"--fuzz-mode")
		_fuzz_mode=1
		;;
	"--help" | "-?" | "-h")
		print_help
		exit 0
		;;
	"--with-libnotify")
		_with_libnotify=1
		;;
	"--without-libiconv")
		_without_libiconv=1
		;;
	"--without-libidn")
		_without_libidn=1
		;;
	"--without-libintl")
		_without_libintl=1
		;;
	*)
		;;
	esac
done

cat <<EOF >$MAKE_DEF_FILE
E = @echo
Q = @
RM = @rm -f
SLASH_SYM = /
EOF

case "$(uname -s)" in
"Darwin")
	os_MAC
	;;
"FreeBSD")
	os_BSD
	;;
"GNU")
	os_GNU
	;;
"Linux")
	if [ "$1" = "suncc" ]; then
		os_LINUX_suncc
	else
		os_LINUX
	fi

	fix_cflags

	if [ "$(uname -m)" = "riscv64" ]; then
		cat <<EOF >>$MAKE_DEF_FILE
LDLIBS += -latomic
EOF
	fi
	;;
"NetBSD")
	os_NETBSD
	;;
"OpenBSD")
	os_BSD

	if [ "$(uname -m)" = "sparc64" ]; then
		cat <<EOF >>$MAKE_DEF_FILE
LDLIBS += -lstdc++fs
EOF
	fi
	;;
*)
	echo "OS not supported!"
	exit 1
	;;
esac

check_etext
check_strcasestr
check_timingsafe_memcmp

if [ ${_sanitize_address} -eq 1 ]; then
	cat <<EOF >>$MAKE_DEF_FILE
CFLAGS += -fsanitize=address
CXXFLAGS += -fsanitize=address
EOF
fi

if [ ${_sanitize_thread} -eq 1 ]; then
	cat <<EOF >>$MAKE_DEF_FILE
CFLAGS += -fsanitize=thread
CXXFLAGS += -fsanitize=thread
EOF
fi

if [ ${_fuzz_mode} -eq 1 ]; then
	cat <<EOF >>$MAKE_DEF_FILE
CPPFLAGS += -DIRCFUZZ_MODE=1
EOF
fi

if [ ${_with_libnotify} -eq 1 ]; then
	link_with_libnotify
fi

if [ ${_without_libiconv} -eq 0 ]; then
	link_with_libiconv
fi

if [ ${_without_libidn} -eq 0 ]; then
	link_with_gnu_libidn
fi

if [ ${_without_libintl} -eq 0 ]; then
	link_with_gnu_libintl
#	${MAKE:-make} -Cpo clean
	${MAKE:-make} -Cpo
fi

link_with_hunspell

if [ -f "$MAKE_DEF_FILE" ]; then
	echo "configure: $MAKE_DEF_FILE successfully created!"
else
	echo "configure: fatal error"
	exit 1
fi
