#!/bin/sh
# -*- tab-width: 4; -*-
# ex:ts=4
#
# $FreeBSD: ports/databases/postgresql72/scripts/configure.postgresql,v 1.1 2003/02/07 11:00:47 seanc Exp $
#
if [ -f ${WRKDIRPREFIX}${CURDIR}/Makefile.inc ]; then
	exit
fi

if [ ! "${BATCH}" ]; then
	dialog --title "Backup your data NOW" \
	--yesno "As always, backup your data before
upgrading. If the upgrade leads to a higher
minor revision (e.g. 7.1.x -> 7.2, a dump
and restore of all databases is
required. This is *NOT* done by the port!

Select 'Yes' to continue the installation." -1 -1
	if [ $? -eq 1 ] ; then exit 1; fi
fi

if [ "${BATCH}" ]; then
	set \"MultiByte\" \"KRB5\"
else
	/usr/bin/dialog --title "configuration options" --clear \
					--checklist "\n\
Please select desired options:" -1 -1 3 \
MultiByte  "Multibyte for Multilingualism" ON \
KRB5       "Kerberos 5 (only if it exists)" ON \
CLIENT     "PostgreSQL client only, no server" 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} ${WRKDIRPREFIX}${CURDIR}
exec > ${WRKDIRPREFIX}${CURDIR}/Makefile.inc

while [ "$1" ]; do
	case $1 in
		\"MultiByte\")
			MULTIBYTE=1
			;;
		\"KRB5\")
			KRB5=1
			;;
		\"CLIENT\")
			CLIENT=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 default charset
echo "# Multibyte" >> ${WRKDIRPREFIX}${CURDIR}/Makefile.inc

if [ ! "${MULTIBYTE}" ]; then
	echo "WITHOUT_MULTIBYTE=YES" >> ${WRKDIRPREFIX}${CURDIR}/Makefile.inc
else
	if [ "${BATCH}" ]; then
		set "SQL_ASCII"
	else
		/usr/bin/dialog --title "Default encoding system" --clear \
						--radiolist "\n\
Please select the default encoding:" -1 -1 16 \
SQL_ASCII     "SQL_ASCII - ASCII" ON \
EUC_JP        "Japanese EUC" OFF \
EUC_CN        "Chinese EUC" OFF \
EUC_KR        "Korean EUC" OFF \
EUC_TW        "Taiwan EUC" OFF \
UNICODE       "Unicode (UTF-8)" OFF \
MULE_INTERNAL "Mule internal code" OFF \
LATIN1        "ISO 8859-1" OFF \
LATIN2        "ISO 8859-2" OFF \
LATIN3        "ISO 8859-3" OFF \
LATIN4        "ISO 8859-4" OFF \
LATIN5        "ISO 8859-9" OFF \
LATIN6        "ISO 8859-10" OFF \
LATIN7        "ISO 8859-13" OFF \
LATIN8        "ISO 8859-14" OFF \
LATIN9        "ISO 8859-15" OFF \
LATIN10       "ISO 8859-16" OFF \
ISO-8859-5    "ECMA-113 Latin/Cyrillic" OFF \
ISO-8859-6    "ECMA-114 Latin/Arabic" OFF \
ISO-8859-7    "ECMA-118 Latin/Greek" OFF \
ISO-8859-8    "ECMA-121 Latin/Hebrew" OFF \
KOI8          "KOI8-R(U)" OFF \
WIN           "Windows CP1251" OFF \
ALT           "Windows CP866" 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

	echo "MULTIBYTE_ENCODING=$1" \
			>> ${WRKDIRPREFIX}${CURDIR}/Makefile.inc
fi

if [ "${KRB5}" ]; then
	if [ -r "/usr/bin/krb5-config" ]; then
		echo "	########################################################################"
		echo "	### PostgreSQL does not build with Hiemdal kerberos. Edit            ###"
		echo "	### /etc/make.conf and turn the knob MAKE_KERBEROS5 off and make     ###"
		echo "	### world (or remove all traces of Hiemdal from your system by:      ###"
		echo "	### rm -f /usr/bin/krb5-config /usr/lib/libkrb5.* /usr/include/krb5* ###"
		echo "	########################################################################"
		KRB5=""
	else
		KRB5CONF="`which krb5-config`"
		if [ "$KRB5CONF" ]; then
			cat <<-EOF >> ${WRKDIRPREFIX}${CURDIR}/Makefile.inc
				.if exists($KRB5CONF)
				LIB_DEPENDS+=	krb5.3:${PORTSDIR}/security/krb5
				CONFIGURE_ARGS+=	--with-krb5="`${KRB5CONF} --prefix krb5`"
				CFLAGS+=	"'`${KRB5CONF} --cflags krb5`'"
				LDFLAGS+=	"'`${KRB5CONF} --libs krb5`'"
				.endif
				EOF
		else
			echo "	########################################################"
			echo "	### Unable to find krb5-config in your path.         ###"
			echo "	### Please correct and build PostgreSQL again if you ###"
			echo "	### want PostgreSQL to be compiled with kerberos     ###"
			echo "	### support (ports/security/krb5).                   ###"
			echo "	########################################################"
		fi
	fi
fi

if [ ${CLIENT} ]; then
	echo "WITHOUT_SERVER=yes" >> ${WRKDIRPREFIX}${CURDIR}/Makefile.inc
fi
