#!/bin/bash
# THIS FILE IS A COPIED TEMPLATE! - Only edit in /pkg/src/
# Configure script helper functions
# M.Tyler 2013-2-9


APP_NAME=
BIN_NAME=
LIB_NAME=
LIB_SONUM="1"
DATA_NAME=
MAKE_DIRS="src"

ENABLE_TK=
ENABLE_MAN_PAGE=
ENABLE_HANDBOOK=
ENABLE_DESKTOP=

QT6_LIBS="Qt6Widgets Qt6OpenGL Qt6OpenGLWidgets Qt6Gui Qt6Core"


UNDERLINE_TEXT()
{
	LINES=$(echo "$1" | sed 's/./-/g' )

	echo
	echo $LINES
	echo "$1"
	echo $LINES
	echo
}


SET_INSTALL_DIRS()
{
	BIN_INSTALL="$1/bin"
	HEAD_INSTALL="$1/include"
	LIB_INSTALL="$1/lib"
	DATA_INSTALL="$1/share"
	MAN_INSTALL="$DATA_INSTALL/man/man1"
}


MTCF_INIT()
{
	CWD=$(pwd)

	VERNUM=$(basename "$CWD" | awk -F "-" '{print $NF}')

	if [ "$(basename "$CWD")" = "$VERNUM" ]
	then
		VERNUM=$(cat $CWD/../VERSION)
	fi

	MT_VERSION="$APP_NAME $VERNUM"

	CONF_FILE=_conf.txt
	DEBUG=NO
	unset BIN_SUFFIX
	unset HELP
	unset PKGCONFIG

	# GNU/Linux
	CC=${CC:-gcc}
	CXX=${CXX:-g++}
	BIN_FLAGS=${BIN_FLAGS:--fPIC}

	SET_INSTALL_DIRS "/usr"
}


MTCF_PARSE_ARG1()
{
	case "$1" in

	"flush" )	echo Clearing out directory to original state
			echo
			make clean
			echo
			rm $CONF_FILE
			exit 0
			;;

	"debug" )	DEBUG=YES
			;;

	"debugold" )	DEBUG=OLD
			;;

	"pack" )	./configure flush
			PKNAME=$(basename $(pwd))
			ARNAME=$PKNAME.tar.xz
			if [ -e ~/$ARNAME ]
			then
				echo
				echo "File already exists: ~/$ARNAME"
				echo
				exit 1
			fi
			cd ..
			tar cJvf ~/$ARNAME $PKNAME
			exit
			;;

	"wc" )		wc $(find ./src -name \*.c -o -name \*.h -o -name \*.yy -o -name \*.cpp)
			exit 0
			;;

	"--help" )	HELP=1
			;;

	"--prefix="* )	MT_PREFIX=$(echo "$A" | sed "s/--prefix=//")
			SET_INSTALL_DIRS "$MT_PREFIX"
			;;

	"--bindir="* )	BIN_INSTALL=$(echo "$A" | sed "s/--bindir=//")
			;;

	"--datadir="* )	DATA_INSTALL=$(echo "$A" | sed "s/--datadir=//")
			;;

	"--includedir="* )
			HEAD_INSTALL=$(echo "$A" | sed "s/--includedir=//")
			;;

	"--libdir="* )	LIB_INSTALL=$(echo "$A" | sed "s/--libdir=//")
			;;

	"--mandir="* )	MAN_INSTALL=$(echo "$A" | sed "s/--mandir=//")
			;;

	"--disable-man" )
			ENABLE_MAN_PAGE=
			;;

	"--disable-handbook" )
			ENABLE_HANDBOOK=
			;;

	"--disable-desktop" )
			ENABLE_DESKTOP=
			;;

	"--use-qt5" )	ENABLE_TK="qt5"
			BIN_SUFFIX="-qt5"
			;;

	"--use-qt6" )	ENABLE_TK="qt6"
			BIN_SUFFIX="-qt6"
			;;

	"" )		;;

	* )		echo ERROR - Unknown command \"$A\"
			exit 1
			;;
	esac
}



MTCF_PARSE_ARGS()
{
for A in "$@"
do
	case "$A" in
	* )		MTCF_PARSE_ARG1 "$A"
			;;
	esac
done
}


MTCF_PRINT_HELP()
{
if [ "$HELP" = 1 ]
then

# Global Help always available.
cat << EOF
Usage: ./configure [OPTION] ...

Options:

flush ........... Flush directories to initial state
debug ........... Include debugging info in binary c11 c++17
debugold ........ Include debugging info in binary c11 c++11
wc .............. Count size of source files

--prefix=DIR .......... DIR = prefix location of all installs (default=/usr)
--bindir=DIR .......... DIR = location of binary installs (default=/usr/bin)
--libdir=DIR .......... DIR = location of library installs (default=/usr/lib)
--includedir=DIR ...... DIR = location of header installs (default=/usr/include)
--datadir=DIR ......... DIR = location of data installs (default=/usr/share)
EOF


# Conditional Help. Depends on the configure script allowing an option.

	if [ "$ENABLE_MAN_PAGE" != "" ]
	then
cat << EOF
--mandir=DIR .......... DIR = location of man page installs (default=/usr/share/man/man1)
--disable-man ......... Disable man page build and install
EOF
	fi

	if [ "$ENABLE_TK" != "" ]
	then
cat << EOF
--use-qt5 ............. Compile Qt5 version (default)
--use-qt6 ............. Compile Qt6 version
EOF
	fi

	if [ "$ENABLE_HANDBOOK" != "" ]
	then
		echo "--disable-handbook .... Don't build handbook"
	fi

	if [ "$ENABLE_DESKTOP" != "" ]
	then
		echo "--disable-desktop ..... Don't build desktop files"
	fi


	if [ "$1" != "" ]
	then
		$1
	fi

	exit 0
fi
}


MTCF_PRE_CONF()
{
	UNDERLINE_TEXT "$MT_VERSION"
	DEBUG_I_EX="-pedantic -Wall -Wextra -D_FORTIFY_SOURCE=2 -O1 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Wformat-security -ffunction-sections -fdata-sections -Wdouble-promotion -Wsign-conversion -Wconversion -Wstrict-overflow=5 -Wcast-align -Wundef -Wcast-qual -Wlogical-op -Wmissing-declarations -Wshadow -Winit-self -Wtrampolines -ggdb -DDEBUG"

	case "$DEBUG" in
	"NO" )
		I_EX=""
		L_EX="-lm -s"

		CFLAGS+=" -std=gnu11"

		case "$ENABLE_TK" in
			qt6 )
				CXXFLAGS+=" -std=gnu++17"
			;;

			* )
				CXXFLAGS+=" -std=gnu++11"
			;;
		esac
		;;
	"YES" )
		I_EX="-fsanitize=undefined $DEBUG_I_EX"
		L_EX="-fsanitize=undefined -lm -O1 -Wl,--as-needed,-z,relro,-z,now"
		CFLAGS+=" -std=gnu17"
		CFLAGS+=" -Wbad-function-cast -Wstrict-prototypes -Wnested-externs"
		CXXFLAGS+=" -std=gnu++17"
		;;
	"OLD" )
		I_EX="$DEBUG_I_EX"
		L_EX="-lm -O1 -Wl,--as-needed,-z,relro,-z,now"
		CFLAGS+=" -std=gnu11"
		CFLAGS+=" -Wbad-function-cast -Wstrict-prototypes -Wnested-externs"
		CXXFLAGS+=" -std=gnu++11"
		;;
	esac

	if [ "$BIN_NAME" != "" ]
	then
		BIN_NAME=$BIN_NAME$BIN_SUFFIX
	fi
}


MTCF_CHECK_LIBS()
{
	while [ "$1" != "" ]
	do
		L_EX+=" -l$1"

		shift
	done
}


MTCF_MAN_PAGE()
{
	ENABLE_MAN_PAGE="man"
}


MTCF_ENABLE_QT()
{
	ENABLE_TK="qt5"
	BIN_SUFFIX="-qt5"
}


MTCF_ENABLE_QT_QEX ()
{
	case "$ENABLE_TK" in
	qt5 )
		MTCF_CHECK_LIBS "mtqex5"
		;;
	qt6 )
		MTCF_CHECK_LIBS "mtqex6"
		;;
	esac
}


MTCF_DESKTOP()
{
	ENABLE_DESKTOP="desktop"
}


MTCF_HANDBOOK()
{
	ENABLE_HANDBOOK="handbook"
}


MTCF_MAKE_DIRS_ADD()
{
	MAKE_DIRS+=" $@"
}


MTCF_VERSION_BIN_NAME()
{
	I_EX+=" -DVERSION=\"\\\"$MT_VERSION\"\\\""

	if [ "$BIN_NAME" != "" ]
	then
		I_EX+=" -DBIN_NAME=\"\\\"$BIN_NAME\"\\\" -DBIN_SUFFIX=\"\\\"$BIN_SUFFIX\"\\\""
	fi

	if [ "$LIB_NAME" != "" ]
	then
		I_EX+=" -DVERNUM=\"\\\"$VERNUM\"\\\""
	fi
}


MTCF_ADD_BIN_FLAGS()
{
	BIN_FLAGS+=" $@"
}


MTCF_ADD_APP_NAME()
{
	I_EX+=" -DAPP_NAME=\"\\\"$APP_NAME\"\\\""
}


MTCF_ADD_APPIMAGE_DATA()
{
	I_EX+=" -DDATA_INSTALL=\"\\\"$APPIMAGE_PREFIX$DATA_INSTALL\"\\\""
}


MTCF_ADD_DATA_NAME()
{
	I_EX+=" -DDATA_NAME=\"\\\"$DATA_NAME\"\\\""
}


MTCF_PKGCONFIG()
{
	PKGCONFIG+=" $@"
}


MTCF_POST_CONF()
{
	> $CONF_FILE

	case "$ENABLE_TK" in
	qt5 )
		MTCF_PKGCONFIG "Qt5Widgets"
		I_EX+=" -DU_TK_QT5"
		MT_GET_MOC_5
		;;
	qt6 )
		MTCF_PKGCONFIG "$QT6_LIBS"
		I_EX+=" -DU_TK_QT6"
		MT_GET_MOC_6
		;;
	esac

	if [ "$PKGCONFIG" != "" ]
	then
		I_EX+=" $(pkg-config $PKGCONFIG --cflags)"
		L_EX+=" $(pkg-config $PKGCONFIG --libs)"
	fi

	F_C="$BIN_FLAGS $CFLAGS -I$HEAD_INSTALL $I_EX"
	F_CXX="$BIN_FLAGS $CXXFLAGS -I$HEAD_INSTALL $I_EX"
	F_L="$LDFLAGS -L$LIB_INSTALL $L_EX"

	echo "APP_NAME      = $APP_NAME"	>> $CONF_FILE
	echo "APP_VERSION   = $VERNUM"		>> $CONF_FILE
	echo "CC            = $CC"		>> $CONF_FILE
	echo "CXX           = $CXX"		>> $CONF_FILE
	echo "CFLAGS        = $F_C"		>> $CONF_FILE
	echo "CXXFLAGS      = $F_CXX"		>> $CONF_FILE
	echo "LDFLAGS       = $F_L"		>> $CONF_FILE

	if [ "$LIB_NAME" != "" ]
	then
		echo "LIB_SONUM     = $LIB_SONUM"		>> $CONF_FILE
		echo "LIB_NAME      = $LIB_NAME.so"		>> $CONF_FILE
		echo "LIB_SONAME    = $LIB_NAME.so.$LIB_SONUM"	>> $CONF_FILE
		echo "LIB_INSTALL   = $LIB_INSTALL"		>> $CONF_FILE
		echo "HEAD_INSTALL  = $HEAD_INSTALL"		>> $CONF_FILE
	fi

	if [ "$BIN_NAME" != "" ]
	then
		echo "BIN_NAME      = $BIN_NAME"	>> $CONF_FILE
		echo "BIN_INSTALL   = $BIN_INSTALL"	>> $CONF_FILE
		echo "BIN_SUFFIX    = $BIN_SUFFIX"	>> $CONF_FILE
	fi

	if [ "$DATA_NAME" != "" ]
	then
		echo "DATA_NAME     = $DATA_NAME"	>> $CONF_FILE
	fi

	echo "DATA_INSTALL  = $DATA_INSTALL"	>> $CONF_FILE

	if [ "$ENABLE_MAN_PAGE" != "" ]
	then
		MTCF_MAKE_DIRS_ADD "$ENABLE_MAN_PAGE"
		echo "MAN_INSTALL   = $MAN_INSTALL" >> $CONF_FILE
	fi

	if [ "$ENABLE_HANDBOOK" != "" ]
	then
		MTCF_MAKE_DIRS_ADD "$ENABLE_HANDBOOK"
	fi

	if [ "$ENABLE_DESKTOP" != "" ]
	then
		MTCF_MAKE_DIRS_ADD "$ENABLE_DESKTOP"
	fi

	if [ "$MOC" != "" ]
	then
		echo "MOC           = $MOC"	>> $CONF_FILE
	fi

	if [ "$APPIMAGE_PREFIX" != "" ]
	then
		echo "APPIMAGE_PREFIX = $APPIMAGE_PREFIX" >> $CONF_FILE
	fi

	# Always comes last
	echo "MAKE_DIRS     = $MAKE_DIRS"	>> $CONF_FILE
}


MTCF_PRINT_CONF()
{
	echo
	echo

	cat $CONF_FILE

	echo
	echo
}


MT_GET_MOC_5()
{
	if hash moc-qt5 2>/dev/null
	then
		MOC="moc-qt5"
	else
		MOC="moc -qt5"
	fi

	MOC="$MOC $(pkg-config Qt5Widgets --cflags)"
}

MT_GET_MOC_6()
{
	# This works with Qt 6.4+ on the major distros
	MOC="$(pkg-config --variable=libexecdir Qt6Widgets)/moc"
	MOC="$MOC $(pkg-config $QT6_LIBS --cflags)"
}

