#!/bin/sh
# -*- tab-width: 4; -*-
# ex:ts=4
#
# $FreeBSD$
#
if [ -f ${WRKDIRPREFIX}${CURDIR}/Makefile.inc ]; then
	exit
fi

if [ ! "${BATCH}" ]; then
	dialog --title "Backup your data NOW" \
	--msgbox "`sed -e \"s#%%WRKDIR%%#${WRKDIR}#; s#%%DISTNAME%%#${DISTNAME}#\" < ${FILESDIR}/pre-install-notes`" \
	-1 -1
fi

#SSL=${NO_OPENSSL:+OFF}
#SSL=${NOSSL:-ON}

if [ "${BATCH}" ]; then
	set \"MultiByte\"
else
	/usr/bin/dialog --title "configuration options" --clear \
					--checklist "\n\
Please select desired options:" -1 -1 8 \
JDBC       "Java DataBase Connectivity" OFF \
MultiByte  "Multibyte for Multilingualism" ON \
ODBC       "Open Database Connectivity" OFF \
Perl       "Perl" OFF \
TCL        "Tcl" ON \
TCLTK      "Tcl/Tk" ON \
SSL        "Secure Socket Layer (requires certificate)" OFF \
KRB5       "Kerberos 5" OFF \
2> /tmp/checklist.tmp.$$

	retval=$?

	if [ -s /tmp/checklist.tmp.$$ ]; then
		set `cat /tmp/checklist.tmp.$$`
	fi
	rm -f /tmp/checklist.tmp.$$

	case $retval in
		0)	if [ -z "$*" ]; then
				echo "Nothing selected"
			fi
			;;
		1)	echo "Cancel pressed."
			exit 1
			;;
	esac
fi

${MKDIR} -p ${WRKDIRPREFIX}${CURDIR}
exec > ${WRKDIRPREFIX}${CURDIR}/Makefile.inc

while [ "$1" ]; do
	case $1 in
		\"JDBC\")
			# use JAVA_HOME if set correctly
			echo "# JDBC"
			if [ "${JAVA_HOME}" -a -x ${JAVA_HOME}/bin/javac ]; then
				echo JAVA_HOME=${JAVA_HOME}
				if [ "$(${JAVA_HOME}/bin/java -version 2>&1 | egrep \"1.1)" ]; then
				    JAVAVERSION=1
					echo BUILD_DEPENDS+=    ${JAVA_HOME}/bin/javac:\${PORTSDIR}/java/jdk
				else
					JAVAVERSION=2
					echo BUILD_DEPENDS+=    ${JAVA_HOME}/bin/javac:\${PORTSDIR}/java/jdk12-beta
				fi
			else
				JAVAVERSION=1
				echo BUILD_DEPENDS+=    \${LOCALBASE}/jdk1.1.8/bin/javac:\${PORTSDIR}/java/jdk
				echo JAVA_HOME=\${LOCALBASE}/jdk1.1.8
			fi
			cat <<-EOF
				JAVAVERSION=${JAVAVERSION}
				WITH_JDBC=YES
				SCRIPTS_ENV+=     JAVA_HOME=\${JAVA_HOME}

			EOF
			;;
		\"MultiByte\")
			MULTIBYTE=1
			;;
		\"ODBC\")
			cat <<-EOF
				# ODBC
				WITH_ODBC=YES
				CONFIGURE_ARGS+=  --with-odbc

			EOF
			;;
		\"Perl\")
			cat <<-EOF
				# PERL
				WITH_PERL=YES
				CONFIGURE_ARGS+=  --with-perl

			EOF
			;;
		\"TCL\")
			TCL=1
			;;
		\"TCLTK\")
			echo "#TCLTK"
			echo WITH_TK=YES
			echo
			TCL=1
			;;
		\"SSL\")
			cat <<-EOF
				# SSL
				USE_OPENSSL=    yes
				CFLAGS+=	-DUSE_SSL
				WITH_SSL=	yes
				CONFIGURE_ENV+=	LDFLAGS="-L\${OPENSSLLIB} -lssl -lcrypto"

			EOF
			;;
		\"KRB5\")
			KRB5=1
			;;
		\"nothing\"|true)
			;;
		*)
			echo "Invalid option(s): $*" > /dev/stderr
			rm -f ${WRKDIRPREFIX}${CURDIR}/Makefile.inc
			exit 1
			;;
	esac
    shift
done

exec > /dev/stderr

# if multibyte, determine charset
echo "# Multibyte" >> ${WRKDIRPREFIX}${CURDIR}/Makefile.inc

if [ ! "${MULTIBYTE}" ]; then
	echo "WITHOUT_MULTIBYTE=YES" >> ${WRKDIRPREFIX}${CURDIR}/Makefile.inc
else
	if [ "${BATCH}" ]; then
		set "nothing"
	else
		/usr/bin/dialog --title "configuration options" --clear \
						--radiolist "\n\
Please select desired options:" -1 -1 16 \
nothing       "Default Encoding" ON \
SQL_ASCII     "SQL_ASCII" OFF \
LATIN1        "LATIN1" OFF \
LATIN2        "LATIN2" OFF \
LATIN3        "LATIN3" OFF \
LATIN4        "LATIN4" OFF \
LATIN5        "LATIN5" OFF \
EUC_JP        "EUC_JP" OFF \
EUC_CN        "EUC_CN" OFF \
EUC_KR        "EUC_KR" OFF \
EUC_TW        "EUC_TW" OFF \
KOI8          "KOI8" OFF \
UNICODE       "UNICODE" OFF \
MULE_INTERNAL "MULE_INTERNAL" OFF \
WIN           "WIN" OFF \
ALT           "ALT" OFF \
2> /tmp/checklist.tmp.$$

		retval=$?

		if [ -s /tmp/checklist.tmp.$$ ]; then
			set `cat /tmp/checklist.tmp.$$`
		fi
		rm -f /tmp/checklist.tmp.$$
		if [ $retval = 1 ]; then
			echo "Cancel pressed."
			rm ${WRKDIRPREFIX}${CURDIR}/Makefile.inc
			exit 1
		fi
	fi

    if [ "$1" = "nothing" ]; then
		echo "CONFIGURE_ARGS+=	--enable-multibyte" \
			>> ${WRKDIRPREFIX}${CURDIR}/Makefile.inc
	else
		echo "CONFIGURE_ARGS+=	--enable-multibyte=$1" \
			>> ${WRKDIRPREFIX}${CURDIR}/Makefile.inc
	fi
fi
echo

if [ "${TCL}" ]; then
	echo "# TCL" >> ${WRKDIRPREFIX}${CURDIR}/Makefile.inc
	if [ "${BATCH}" ]; then
		echo WITH_TCL=8.3  >> ${WRKDIRPREFIX}${CURDIR}/Makefile.inc
	else
		dialog --title "TCL version" \
			   --inputbox "Please enter TCL version. Default is 8.3." -1 -1 "8.3" \
			2> /tmp/checklist.tmp.$$
		retval=$?
		if [ -s /tmp/checklist.tmp.$$ ]; then
			set `cat /tmp/checklist.tmp.$$`
		fi
		rm -f /tmp/checklist.tmp.$$
		case $retval in
			0)	if [ -z "$*" ]; then
					echo "Nothing selected"
				fi
				;;
			1)	echo "Cancel pressed."
				rm -f ${WRKDIRPREFIX}${CURDIR}/Makefile.inc
				exit 1
				;;
		esac
		echo WITH_TCL=\"$1\" >> ${WRKDIRPREFIX}${CURDIR}/Makefile.inc
	fi
	cat <<-EOF >> ${WRKDIRPREFIX}${CURDIR}/Makefile.inc
		CONFIGURE_ARGS+= --with-tcl --with-tclconfig="\${TCLCONFIG}"
		TCLV=\${WITH_TCL:S/.//}
		TCL_INCDIR=    \${LOCALBASE}/include/tcl\${WITH_TCL}
		MAKE_ENV=      TCL_INCDIR=\${TCL_INCDIR}
		LIB_DEPENDS=   tcl\${TCLV}.1:\${PORTSDIR}/lang/tcl\${TCLV}
		TCLCONFIG=     '\${LOCALBASE}/lib/tcl\${WITH_TCL}'
		.if defined(WITH_TK)
		TK_INCDIR=     \${LOCALBASE}/include/tk\${WITH_TCL}
		CONFIGURE_ENV+=        WISH=\${LOCALBASE}/bin/wish\${WITH_TCL}
		LIB_DEPENDS+=  tk\${TCLV}.1:\${PORTSDIR}/x11-toolkits/tk\${TCLV}
		TCLCONFIG+=    '\${LOCALBASE}/lib/tk\${WITH_TCL}'
		TCL_PLIST=     pkg-plist.tcl
		.else
		TCL_PLIST=     pkg-plist.notk
		CONFIGURE_ARGS+=        --without-tk
		.endif

	EOF
fi

if [ "${KRB5}" ]; then
	if [ "${BATCH}" ]; then
		# never gets here, but what the heck...
		echo KRB5_HOME=\${LOCALBASE} >> ${WRKDIRPREFIX}${CURDIR}/Makefile.inc
	else
		dialog --inputbox "Please enter the KRB5_HOME path" -1 -1 "${KRB_HOME:-${LOCALBASE}}" \
			2> /tmp/checklist.tmp.$$
		retval=$?
		if [ -s /tmp/checklist.tmp.$$ ]; then
			set `cat /tmp/checklist.tmp.$$`
		fi
		rm -f /tmp/checklist.tmp.$$
		case $retval in
			0)	if [ -z "$*" ]; then
					echo "No input"
				fi
				;;
			1)	echo "Cancel pressed."
				rm -f ${WRKDIRPREFIX}${CURDIR}/Makefile.inc
				exit 1
				;;
		esac
		echo KRB5_HOME=\"$1\" >> ${WRKDIRPREFIX}${CURDIR}/Makefile.inc
	fi
	cat <<-EOF >> ${WRKDIRPREFIX}${CURDIR}/Makefile.inc
		.if defined(KRB5_HOME) && exists(\${KRB5_HOME})
		CONFIGURE_KRB=	--with-krb5=\${KRB5_HOME} \
						--with-krb-keytab=FILE:\${PREFIX}/pgsql/etc/keytab
		.endif

	EOF
fi
