#!/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=libpcap
PKGVERSION=1.9.1
PKGREVISION=1
PKGLICENSE="BSD3CLAUSE"
PKGSUMMARY="system-independent interface for user-level packet capture"
PKGWWW="https://www.tcpdump.org"
PKGDOWNLOAD="https://www.tcpdump.org/release/$PKGNAME-$PKGVERSION.tar.gz"
PKGDEPENDS="bison:build flex:build linux-headers:build"
PKGOPTIONS="DBUS IPV6 NETLINK USB"

: ${CHOST:=$(uname -m)-linux-musl}
: ${CBUILD:=$(uname -m)-linux-musl}
: ${AR:=llvm-ar}
: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${LDFLAGS:=}
: ${LIBS:=}
: ${RANLIB:=llvm-ranlib}
: ${DBUS:=yes}
: ${IPV6:=yes}
: ${NETLINK:=yes}
: ${USB:=yes}

if [ "$DBUS" = "yes" ]; then
	PKGDEPENDS="dbus $PKGDEPENDS"
	with_dbus="--enable-dbus"
else
	with_dbus="--disable-dbus"
fi

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

if [ "$NETLINK" = "yes" ]; then
	PKGDEPENDS="libnl $PKGDEPENDS"
	with_netlink="--with-libnl"
else
	with_netlink="--without-libnl"
fi

if [ "$USB" = "yes" ]; then
	PKGDEPENDS="eudev:build $PKGDEPENDS"
	with_usb="--enable-usb"
else
	with_usb="--disable-usb"
fi

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

AR="$AR" \
CC="$CC" \
CFLAGS="$CFLAGS" \
LDFLAGS="$LDFLAGS" \
LIBS="$LIBS" \
RANLIB="$RANLIB" \
./configure \
	--build=$CBUILD \
	--host=$CHOST \
	--prefix=/usr \
	$with_dbus \
	$with_ipv6 \
	$with_netlink \
	$with_usb
make
make install DESTDIR=$DESTDIR

cd ..
rm -rf $PKGNAME-$PKGVERSION
