#!/bin/sh
#
# Copyright (c) 2019 FirstName LastName <mailaddress>
#
# 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=libarchive
PKGVERSION=3.3.3
PKGREVISION=1
PKGLICENSE="BSD"
PKGSUMMARY="multi-format archive and compression library"
PKGDOWNLOAD="http://www.libarchive.org/downloads/$PKGNAME-$PKGVERSION.tar.gz"
PKGOPTIONS="ACL BZIP2 LZ4 LZMA LZO SSL ZLIB ZSTD XML"

: ${CHOST:=$(uname -m)-linux-musl}
: ${CBUILD:=$(uname -m)-linux-musl}
: ${CC:=gcc}
: ${CFLAGS:=-O2}
: ${CXX:=g++}
: ${CXXFLAGS:=-O2}
: ${LDFLAGS:=}
: ${LIBS:=}
: ${ACL:=yes}
: ${BZIP2:=yes}
: ${LZ4:=yes}
: ${LZMA:=yes}
: ${LZO:=no}            # upstream default as of 3.3.3
: ${SSL:=yes}
: ${XML:=yes}
: ${ZLIB:=yes}
: ${ZSTD:=yes}

if [ "$ACL" = "yes" ]; then
	PKGDEPENDS="core/acl $PKGDEPENDS"
	with_acl="--enable-acl"
else
	with_acl="--disable-acl"
fi

if [ "$BZIP2" = "yes" ]; then
	PKGDEPENDS="core/bzip2 $PKGDEPENDS"
	with_bzip2="--with-bz2lib"
else
	with_bzip2="--without-bz2lib"
fi

if [ "$XML" = "yes" ]; then
	PKGDEPENDS="lib/expat $PKGDEPENDS"
	with_expat="--with-expat"
else
	with_expat="--without-expat"
fi

if [ "$LZ4" = "yes" ]; then
	PKGDEPENDS="core/lz4 $PKGDEPENDS"
	with_lz4="--with-lz4"
else
	with_lz4="--without-lz4"
fi

if [ "$LZMA" = "yes" ]; then
	PKGDEPENDS="core/xz $PKGDEPENDS"
	with_lzma="--with-lzma"
else
	with_lzma="--without-lzma"
fi

if [ "$LZO" = "yes" ]; then
	PKGDEPENDS="lib/lzo $PKGDEPENDS"
	with_lzo="--with-lzo2"
else
	with_lzo="--without-lzo2"
fi

if [ "$NETTLE" = "yes" ]; then
	PKGDEPENDS="lib/nettle $PKGDEPENDS"
	with_nettle="--with-nettle"
else
	with_nettle="--without-nettle"
fi

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

if [ "$ZSTD" = "yes" ]; then
	PKGDEPENDS="core/zstd $PKGDEPENDS"
	with_zstd="--with-zstd"
else
	with_zstd="--without-zstd"
fi

build()
{
	rm -rf $PKGNAME-$PKGVERSION
	tar xvaf $PKGNAME-$PKGVERSION.tar.gz
	pushd $PKGNAME-$PKGVERSION

	CC="$CC" \
	CFLAGS="$CFLAGS" \
	CXX="$CXX" \
	CXXFLAGS="$CXXFLAGS" \
	LDFLAGS="$LDFLAGS" \
	LIBS="$LIBS" \
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--disable-static \
		--enable-shared \
		--enable-bsdtar=shared \
		--enable-bsdcat=shared \
		--enable-bsdcpio=shared \
		--without-xml2 \
		$with_acl \
		$with_bzip2 \
		$with_expat \
		$with_lz4 \
		$with_lzma \
		$with_lzo \
		$with_nettle \
		$with_ssl \
		$with_zstd

	make
	make install DESTDIR=$DESTDIR
        rm -f $DESTDIR/usr/lib/libarchive.la

	popd
	rm -rf $PKGNAME-$PKGVERSION
}
