# Template file for 'ppp'
# rev-bump:
# * NetworkManager
# * NetworkManager-pptp
# * NetworkManager-l2tp
# When update this package
pkgname=ppp
version=2.4.7
revision=13
makedepends="libpcap-devel libressl-devel"
short_desc="PPP (Point-to-Point Protocol) daemon"
maintainer="Orphaned <orphan@voidlinux.org>"
license="BSD-3-Clause, LGPL-2.0-or-later, GPL-2.0-or-later, Public Domain"
homepage="https://ppp.samba.org/"
distfiles="https://ftp.samba.org/pub/ppp/ppp-$version.tar.gz"
checksum=02e0a3dd3e4799e33103f70ec7df75348c8540966ee7c948e4ed8a42bbccfb30
make_dirs="
 /etc/ppp/ipv6-down.d 0755 root root
 /etc/ppp/peers 0755 root root"
conf_files="
 /etc/ppp/ip-up
 /etc/ppp/ip-down
 /etc/ppp/ipv6-up
 /etc/ppp/ipv6-down
 /etc/ppp/options
 /etc/ppp/chap-secrets
 /etc/ppp/pap-secrets"

CFLAGS="-D_GNU_SOURCE"

if [ "$XBPS_TARGET_LIBC" = "musl" ]; then
	makedepends+=" musl-legacy-compat"
fi

do_configure() {
	# Custom CFLAGS.
	find -name "Makefile.linux" -exec sed -i "{}" \
		-e "s;CFLAGS\\s*=\(.*\);CFLAGS = ${CFLAGS} \1;" \;
	# Custom LDFLAGS for chat, pppoe-discovery and pppdump
	sed -i chat/Makefile.linux \
		-e "s;-o chat ;${LDFLAGS} -o chat ;"
	sed -i pppd/plugins/rp-pppoe/Makefile.linux \
		-e "s;-o pppoe-discovery ;${LDFLAGS} -o pppoe-discovery ;"
	sed -i pppdump/Makefile.linux \
		-e "s;-o pppdump ;${LDFLAGS} -o pppdump ;"
	# Enable active filter
	sed -i "s:^#FILTER=y:FILTER=y:" pppd/Makefile.linux
	# Enable ipv6 support
	sed -i "s:^#HAVE_INET6=y:HAVE_INET6=y:" pppd/Makefile.linux
	# Enable Microsoft Proprietary Callback Control Protocol
	sed -i "s:^#CBCP=y:CBCP=y:" pppd/Makefile.linux

	./configure ${configure_args}
}

do_build() {
	make COPTS="${CFLAGS} ${LDFLAGS}" ${makejobs}
}

do_install() {
	make DESTDIR=${DESTDIR}/usr install

	case "$XBPS_TARGET_MACHINE" in
		*-musl) vinstall include/net/ppp_defs.h 644 usr/include/net;;
	esac

	chmod 755 ${DESTDIR}/usr/lib/pppd/${version}/*.so

	vmkdir usr/bin
	mv ${DESTDIR}/usr/sbin/* ${DESTDIR}/usr/bin
	vbin pppd/pppd
	vbin chat/chat

	vinstall ${FILESDIR}/options 644 etc/ppp
	vinstall ${FILESDIR}/ip-up 755 etc/ppp
	vinstall ${FILESDIR}/ip-down 755 etc/ppp
	vinstall ${FILESDIR}/ipv6-up 755 etc/ppp
	vinstall ${FILESDIR}/ipv6-down 755 etc/ppp

	vinstall ${FILESDIR}/ip-up.d.dns.sh 755 etc/ppp/ip-up.d 00-dns.sh
	vinstall ${FILESDIR}/ip-down.d.dns.sh 755 etc/ppp/ip-down.d 00-dns.sh
	vinstall ${FILESDIR}/ipv6-up.d.iface-config.sh 755 etc/ppp/ipv6-up.d  00-iface-config.sh

	vbin scripts/pon
	vman scripts/pon.1
	vbin scripts/poff
	vbin scripts/plog

	vinstall etc.ppp/pap-secrets 600 etc/ppp
	vinstall etc.ppp/chap-secrets 600 etc/ppp

	head -n67 pppd/main.c > LICENSE
	vlicense LICENSE
}

ppp-devel_package() {
	depends="libpcap-devel"
	short_desc+=" - development files"
	pkg_install() {
		vmove usr/include
	}
}
