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

PKGNAME=nspr
PKGVERSION=4.21
PKGREVISION=1
PKGLICENSE="MPLv20"
PKGSUMMARY="Netscape Portable Runtime"
PKGDOWNLOAD="https://archive.mozilla.org/pub/$PKGNAME/releases/v$PKGVERSION/src/$PKGNAME-$PKGVERSION.tar.gz"
PKGOPTIONS="IPV6"

: ${CHOST:=$(uname -m)-linux-musl}
: ${CBUILD:=$(uname -m)-linux-musl}
: ${CC:=clang}
: ${CFLAGS:=-O2 -D_PR_POLL_AVAILABLE -D_PR_HAVE_OFF64_T -D_PR_INET6 -D_PR_HAVE_INET_NTOP -D_PR_HAVE_GETHOSTBYNAME2 -D_PR_HAVE_GETADDRINFO -D_PR_INET6_PROBE}
: ${CXX:=clang++}
: ${CXXFLAGS:=-O2 -D_PR_POLL_AVAILABLE -D_PR_HAVE_OFF64_T -D_PR_INET6 -D_PR_HAVE_INET_NTOP -D_PR_HAVE_GETHOSTBYNAME2 -D_PR_HAVE_GETADDRINFO -D_PR_INET6_PROBE}
: ${LDFLAGS:=}
: ${LIBS:=}
: ${IPV6:=yes}

if [ "$IPV6" = "yes" ]; then
	with_ipv6="--enable-ipv6"
else
	with_ipv6="--disable-ipv6"
fi

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

	sed -i -e 's#$(LIBRARY) ##' config/rules.mk
	CC="$CC" \
	CFLAGS="$CFLAGS" \
	CXX="$CXX" \
	CXXFLAGS="$CXXFLAGS" \
	LDFLAGS="$LDFLAGS" \
	LIBS="$LIBS" \
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix= \
		--enable-64bit \
		--with-mozilla \
		$with_ipv6
	make
	make install DESTDIR=$DESTDIR

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