#!/bin/sh
#
# Copyright (c) 2019 David Demelier <markand@malikania.fr>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

# TODO: add KERBEROS, SELINUX option.

PKGNAME=postgresql
PKGVERSION=12.0
PKGREVISION=1
PKGLICENSE="CUSTOM"
PKGSUMMARY="popular object-relational database management system"
PKGDOWNLOAD="https://ftp.postgresql.org/pub/source/v$PKGVERSION/$PKGNAME-$PKGVERSION.tar.gz"
PKGDEPENDS=""
PKGOPTIONS="COMPLETION ICU LLVM NLS PAM PERL PYTHON SSL TCL XML XSLT ZLIB"
PKGUIDS="postgresql:110"
PKGGIDS="postgresql:110"
PKGPROTECT="etc/rc.d/postgresql"

: ${CHOST:=$(uname -m)-linux-musl}
: ${CBUILD:=$(uname -m)-linux-musl}
: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${CXX:=clang++}
: ${CXXFLAGS:=-O2}
: ${LDFLAGS:=}
: ${LIBS:=}
: ${COMPLETION:=yes}    # Note: can be readline (or yes) or libedit.
: ${ICU:=no}
: ${LLVM:=no}
: ${NLS:=yes}
: ${PAM:=no}
: ${PERL:=no}
: ${PYTHON:=no}
: ${SSL:=yes}           # Note: SSL support through libressl.
: ${TCL:=no}
: ${XML:=no}            # Note: XML: support through libxml2.
: ${XSLT:=no}
: ${ZLIB:=yes}

case "$COMPLETION" in
"readline"|"yes")
	PKGDEPENDS="readline $PKGDEPENDS"
	with_completion="--with-readline"
	;;
"libedit")
	PKGDEPENDS="libedit $PKGDEPENDS"
	with_completion="--without-readline --with-libedit-preferred"
	;;
*)
	with_completion="--without-readline"
	;;
esac

if [ "$ICU" = "yes" ]; then
	PKGDEPENDS="icu $PKGDEPENDS"
	with_icu="--with-icu"
else
	with_icu="--without-icu"
fi

if [ "$LLVM" = "yes" ]; then
	PKGDEPENDS="llvm $PKGDEPENDS"
	with_llvm="--with-llvm"
else
	with_llvm="--without-llvm"
fi

if [ "$NLS" = "yes" ]; then
	PKGDEPENDS="gettext $PKGDEPENDS"
	with_nls="--enable-nls"
else
	with_nls="--disable-nls"
fi

if [ "$PAM" = "yes" ]; then
	PKGDEPENDS="linux-pam $PKGDEPENDS"
	with_pam="--with-pam"
else
	with_pam="--without-pam"
fi

if [ "$PERL" = "yes" ]; then
	PKGDEPENDS="perl $PKGDEPENDS"
	with_perl="--with-perl"
else
	with_perl="--without-perl"
fi

if [ "$PYTHON" = "yes" ]; then
	PKGDEPENDS="python $PKGDEPENDS"
	with_python="--with-python"
else
	with_python="--without-python"
fi

if [ "$SSL" = "yes" ]; then
	PKGDEPENDS="libressl $PKGDEPENDS"
	with_ssl="--with-openssl"
else
	with_ssl="--without-openssl"
fi

if [ "$TCL" = "yes" ]; then
	PKGDEPENDS="tcl $PKGDEPENDS"
	with_tcl="--with-tcl"
else
	with_tcl="--without-tcl"
fi

if [ "$XML" = "yes" ]; then
	PKGDEPENDS="libxml2 $PKGDEPENDS"
	with_xml="--with-libxml"
else
	with_xml="--without-libxml"
fi

if [ "$XSLT" = "yes" ]; then
	PKGDEPENDS="libxslt $PKGDEPENDS"
	with_xslt="--with-libxslt"
else
	with_xslt="--without-libxslt"
fi

if [ "$ZLIB" = "yes" ]; then
	PKGDEPENDS="zlib $PKGDEPENDS"
	with_zlib="--with-zlib"
else
	with_zlib="--without-zlib"
fi

build()
{
	rm -rf $PKGNAME-$PKGVERSION
	tar xvf $PKGNAME-$PKGVERSION.tar.gz
	cd $PKGNAME-$PKGVERSION

	CC="$CC" \
	CFLAGS="$CFLAGS" \
	CXX="$CXX" \
	CXXFLAGS="$CXXFLAGS" \
	LDFLAGS="$LDFLAGS" \
	LIBS="$LIBS" \
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix= \
		$with_completion \
		$with_icu \
		$with_llvm \
		$with_nls \
		$with_pam \
		$with_perl \
		$with_python \
		$with_ssl \
		$with_tcl \
		$with_xml \
		$with_xslt \
		$with_zlib
	make
	make install DESTDIR=$DESTDIR
	install -Dm0644 ../postgresql $DESTDIR/etc/rc.d/postgresql

	cd ..
	rm -rf $PKGNAME-$PKGVERSION
}
