#!/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.
#

. ./git.info

: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${LDFLAGS:=}
: ${CURL:=yes}
: ${NLS:=yes}
: ${PERL:=yes}
: ${REGEX:=yes}         # Note: regular expression through pcre2.
: ${SSL:=yes}
: ${XML:=yes}           # Note: HTTP support through expat.
: ${ZLIB:=yes}

if [ "$CURL" = "yes" ]; then
	PKGDEPENDS="curl $PKGDEPENDS"
else
	with_curl="NO_CURL=YesPlease"
fi

if [ "$NLS" = "yes" ]; then
	PKGDEPENDS="gettext $PKGDEPENDS"
else
	with_nls="NO_GETTEXT=YesPlease"
fi

if [ "$PERL" = "yes" ]; then
	PKGDEPENDS="perl $PKGDEPENDS"
	with_perl="NO_PERL_CPAN_FALLBACKS=1"
else
	with_perl="NO_PERL=YesPlease NO_PERL_CPAN_FALLBACKS=1"
fi

if [ "$REGEX" = "yes" ]; then
	PKGDEPENDS="pcre2 $PKGDEPENDS"
	with_regex="USE_LIBPCRE=Yes"
fi

if [ "$SSL" = "yes" ]; then
	PKGDEPENDS="libressl $PKGDEPENDS"
else
	with_ssl="NO_SSL=YesPlease"
fi

if [ "$XML" = "yes" ]; then
	PKGDEPENDS="expat $PKGDEPENDS"
else
	with_xml="NO_EXPAT=YesPlease"
fi

if [ "$ZLIB" = "yes" ]; then
	PKGDEPENDS="zlib $PKGDEPENDS"
else
	with_zlib="NO_ZLIB=YesPlease"
fi

perl_config()
{
	perl -e "use Config; print \$Config{$1};"
}

rm -rf $PKGNAME-$PKGVERSION
tar xvf $PKGNAME-$PKGVERSION.tar.xz
cd $PKGNAME-$PKGVERSION

# NO_PYTHON: only for 2.7 which is deprecated in Vanilla Linux.
# NO_REGEX: does not build without.
make \
	V=1 \
	CC="$CC" \
	CFLAGS="$CFLAGS" \
	LDFLAGS="$LDFLAGS" \
	DESTDIR="$DESTDIR" \
	NO_REGEX=YesPlease \
	prefix= \
	perllibdir=$(perl_config vendorlib) \
	$with_curl \
	$with_nls \
	$with_perl \
	$with_regex \
	$with_ssl \
	$with_xml \
	$with_zlib \
	NO_PYTHON=YesPlease \
	all man install install-man

cd ..
rm -rf $PKGNAME-$PKGVERSION
