#!/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=iproute2
PKGVERSION=4.20.0
PKGREVISION=1
PKGLICENSE="GPL2"
PKGSUMMARY="IP routing utilities"
PKGDOWNLOAD="https://www.kernel.org/pub/linux/utils/net/$PKGNAME/$PKGNAME-$PKGVERSION.tar.xz"
PKGDEPENDS="core/elfutils"
PKGOPTIONS="CAP MNL"

: ${CHOST:=$(uname -m)-linux-musl}
: ${CBUILD:=$(uname -m)-linux-musl}
: ${CTARGET:=$(uname -m)-linux-musl}
: ${CC:=clang}
: ${CFLAGS:=-O2 -DHAVE_SETNS -DHAVE_ELF -DNEED_STRLCPY}
: ${LIBS:=-lelf}
: ${CAP:=yes}
: ${MNL:=yes}

if [ "$CAP" = "yes" ]; then
	PKGDEPENDS="core/libcap $PKGDEPENDS"
	CFLAGS="-DHAVE_LIBCAP $CFLAGS"
	LIBS="-lcap $LIBS"
fi

if [ "$MNL" = "yes" ]; then
	PKGDEPENDS="lib/libmnl $PKGDEPENDS"
	CFLAGS="-DHAVE_LIBMNL $CFLAGS"
	LIBS="-lmnl $LIBS"
fi

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

	# custom configure script is not customizable.
	echo CC="$CC" > config.mk
	echo CFLAGS+="$CFLAGS" >> config.mk
	echo PKG_CONFIG="pkgconf" >> config.mk
	echo IP_CONFIG_SETNS=y >> config.mk
	echo LDLIBS+="$LIBS" >> config.mk
	echo TC_CONFIG_NO_XT=y >> config.mk

	if [ "$CAP" = "yes" ]; then
		echo HAVE_CAP=y >> config.mk
	fi
	if [ "$MNL" = "yes" ]; then
		echo HAVE_MNL=y >> config.mk
	fi

	patch -p0 < ../musl.patch
	make CC="$CC"
	make SBINDIR=/usr/sbin DESTDIR=$DESTDIR install

	popd
	rm -rf $PKGNAME-$PKGVERSION
}
