# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=dhcp
pkgver=4.4.1
_realver=${pkgver/_p/-P}
pkgrel=1
pkgdesc="ISC Dynamic Host Configuration Protocol (DHCP)"
url="https://www.isc.org/"
arch="all"
license="ISC"
depends=
pkgusers="dhcp"
pkggroups="dhcp"
makedepends="perl linux-headers file"
install="$pkgname.pre-install $pkgname.pre-upgrade $pkgname.post-upgrade"
subpackages="$pkgname-doc $pkgname-dev $pkgname-dbg dhclient dhcrelay"
source="http://ftp.isc.org/isc/$pkgname/${_realver}/$pkgname-$_realver.tar.gz
	dhclient-script-fix-bare-ip.patch
	dhcp-3.0-fix-perms.patch
	dhcrelay.initd
	dhcrelay.confd
	dhcpd.confd
	dhcpd.initd"

builddir="$srcdir/$pkgname-$_realver"

build() {
	cd "$builddir"
	# fix ipv6
	export CFLAGS="$CFLAGS -D_GNU_SOURCE \
		-D_PATH_DHCLIENT_CONF='\"/etc/dhcp/dhclient.conf\"'"
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc/dhcp \
		--with-cli-pid-file=/var/run/dhcp/dhclient.pid \
		--with-cli-lease-file=/var/lib/dhcp/dhclient.leases \
		--with-srv-pid-file=/var/run/dhcp/dhcpd.pid \
		--with-srv-lease-file=/var/lib/dhcp/dhcpd.leases \
		--with-relay-pid-file=/var/run/dhcp/dhcrelay.pid \
		--enable-dhcpv6 \
		--enable-paranoia \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info

	make -j1 -C bind && make
}

check() {
	cd "$builddir"
	make check
}

package() {
	cd "$builddir"
	make DESTDIR="$pkgdir" install

	install -m755 -D "$srcdir"/dhcpd.initd "$pkgdir"/etc/init.d/dhcpd
	install -m644 -D "$srcdir"/dhcpd.confd "$pkgdir"/etc/conf.d/dhcpd
	install -m751 -D "$builddir"/client/scripts/linux "$pkgdir"/sbin/dhclient-script
	install -d "$pkgdir"/var/lib/dhcp
	install -d "$pkgdir"/var/run/dhcp
}

dhclient() {
	pkgdesc="ISC dhcp client"
	install -d -o dhcp -g dhcp "$subpkgdir"/var/lib/dhcp
	install -d -o dhcp -g dhcp "$subpkgdir"/var/run/dhcp
	install -d "$subpkgdir"/usr/sbin
	install -d "$subpkgdir"/sbin
	install -d "$subpkgdir"/etc/dhcp
	mv "$pkgdir"/usr/sbin/dhclient "$subpkgdir"/usr/sbin/
	mv "$pkgdir"/sbin/dhclient-script "$subpkgdir"/sbin/
	ln -s ../sbin/dhclient-script "$subpkgdir"/etc/dhclient-script
	mv "$pkgdir"/etc/dhcp/dhclient.conf* "$subpkgdir"/etc/dhcp/
}

dhcrelay() {
	pkgdesc="ISC dhcp relay server"
	replaces="dhcp"
	install -d "$subpkgdir"/var/run/dhcp \
		"$subpkgdir"/usr/sbin
	mv "$pkgdir"/usr/sbin/dhcrelay "$subpkgdir"/usr/sbin/
	install -m755 -D "$srcdir"/dhcrelay.initd \
		"$subpkgdir"/etc/init.d/dhcrelay
	install -m644 -D "$srcdir"/dhcrelay.confd \
		"$subpkgdir"/etc/conf.d/dhcrelay
}

sha512sums="684ae349f224918c9f8cec7bd6c55cd0b83ad2b5827375b2876ca088eb05b7ff1364e50f6dc24f2485c610d9be94d4ba3020f60a0fa0ef63962349d191b887e7  dhcp-4.4.1.tar.gz
65ddc66553f6f21184dd82baa929f2414b7374e9bafecc22f0d27a653f66796fbcb3a72d2f52af03a6993ce70e1b8dd0ab4fd630824b9a4825799bec190007fb  dhclient-script-fix-bare-ip.patch
d5697a56fbbff25199962608986e7ffb533ed4afd3e344e3c79d2010dda73cc0b088f06c454e9f0c69eb054e09a374455fa71d3f73306e0c98fa76df4dd321b7  dhcp-3.0-fix-perms.patch
0c3eee11ee9771a3bd7f8076827e57296136a36e6c00c3149e7274fb7a479feae4dc7d744f3c66ac78ffc96f036e14a7a5a0806095495441f98d76737a34faa6  dhcrelay.initd
fd15dbaa4c61c3c26f407bf13dde859470a1adba134da064b653ccc152ce42635ee8de2fe113ae21ba8470e97e3caad8c1a47b69eb25e5e92b40e26790b96f6d  dhcrelay.confd
7b7a77b7826b475a4113ebeee54501ce417cc56e85754301a82a185d88b4713d198f615a366e63e0e2b0aef988c8137dcd1e18c4036d993378257079da17693d  dhcpd.confd
96ebc64e7c8a484133c23e9d1d7afe965247aaf54bc619b4fa81ed80f56b6bdb7e2c22d3f26db4c87c2a6ffd29ae2db6408954feac4c2104df712975cdfccc60  dhcpd.initd"
