# POWER Maintainer: Alexander Baldeck <alex.bldck@gmail.com>
# Maintainer: Felix Yan <felixonmars@archlinux.org>
# Contributor: Daniel Isenmann <daniel @archlinux.org>

pkgbase=dhcp
pkgname=('dhcp' 'dhclient')

# separate patch levels with a period to maintain proper versioning.
_realver=4.4.3-P1
pkgver=${_realver/-/.}
pkgrel=4
arch=(x86_64 powerpc64le powerpc64 powerpc riscv64)
license=('custom:isc-dhcp')
url='https://www.isc.org/dhcp/'
makedepends=('bash' 'iproute2' 'openldap')
options=('!lto')
validpgpkeys=('7E1C91AC8030A5A59D1EFAB9750F3C87723E4012') # Internet Systems Consortium, Inc. (Signing key, 2021-2022) <codesign@isc.org>
source=("https://downloads.isc.org/isc/${pkgbase}/${_realver}/${pkgbase}-${_realver}.tar.gz"{,.asc}
        '0001-99-replace-hostname-with-uname-n.patch'
        'dhcp-sysusers.conf'
        'dhcp-tmpfiles.conf'
        'dhcpd4.service'
        'dhcpd6.service'
        'dhclient@.service')
sha256sums=('0ac416bb55997ca8632174fd10737fd61cdb8dba2752160a335775bc21dc73c7'
            'SKIP'
            '559ed9ce6318f1a3700f8b879552bd22844c7c5b96622202dbf3164cd99c444e'
            '62335e1043e40ea96a7447db499eafcb9b5a262356c5d9951a43ca4bc981fecc'
            'abcd30e9e8428e34d22ab4d3074ef4bd84c2b11f5868597111b47d6f56d204da'
            '03fce30efab819b2d928085b0bab962a33ce56fc376acae98ad9b30aa278c9c8'
            'f98a4438f4f69cab7cc5cce6927df4790ee993ebc8f88a169e63043c53d25625'
            '86cd0b1e0ea1d47ab096f6ee925eee60545116fb887a155761eda589b30e4f0e')

prepare() {
  cd "${srcdir}/${pkgbase}-${_realver}"

  patch -Np1 < ../0001-99-replace-hostname-with-uname-n.patch

  sed "s@^bindconfig = @bindconfig = --build=${CHOST} @g" -i bind/Makefile.in
}

build() {
  cd "${srcdir}/${pkgbase}-${_realver}"

  case "${CARCH}" in
    powerpc|riscv64) export CFLAGS+=' -latomic' ;;
  esac

  ./configure \
      --build=${CHOST} \
      --prefix=/usr \
      --sbindir=/usr/bin \
      --sysconfdir=/etc \
      --enable-binary-leases \
      --enable-paranoia \
      --with-ldap \
      --with-ldapcrypto \
      --with-srv-lease-file=/var/lib/dhcp/dhcpd.leases \
      --with-srv6-lease-file=/var/lib/dhcp/dhcpd6.leases \
      --with-cli-lease-file=/var/lib/dhclient/dhclient.leases \
      --with-cli6-lease-file=/var/lib/dhclient/dhclient6.leases

  make -j1
}

package_dhcp(){
  pkgdesc="A DHCP server, client, and relay agent"
  depends=('glibc' 'libldap')
  backup=('etc/dhcpd.conf' 'etc/dhcpd6.conf')

  cd "${srcdir}/${pkgbase}-${_realver}"

  make DESTDIR="${pkgdir}" install

  install -D -m644 "${srcdir}/dhcp-sysusers.conf" "${pkgdir}/usr/lib/sysusers.d/dhcp.conf"
  install -D -m644 "${srcdir}/dhcp-tmpfiles.conf" "${pkgdir}/usr/lib/tmpfiles.d/dhcp.conf"
  install -D -m644 "${srcdir}/dhcpd4.service" "${pkgdir}/usr/lib/systemd/system/dhcpd4.service"
  install -D -m644 "${srcdir}/dhcpd6.service" "${pkgdir}/usr/lib/systemd/system/dhcpd6.service"

  # move and install config files in place
  mv "${pkgdir}/etc/dhcpd.conf.example" "${pkgdir}/etc/dhcpd.conf"
  install -D -m644 doc/examples/dhcpd-dhcpv6.conf "${pkgdir}/etc/dhcpd6.conf"

  # Remove dhclient
  make -C client DESTDIR="${pkgdir}" uninstall

  # install license
  install -m644 -D LICENSE "${pkgdir}/usr/share/licenses/dhcp/LICENSE"
}

package_dhclient(){
  pkgdesc="A standalone DHCP client from the dhcp package"
  depends=('glibc' 'bash' 'iproute2' 'run-parts')
  provides=('dhcp-client')

  cd "${srcdir}/${pkgbase}-${_realver}"

  make -C client DESTDIR="${pkgdir}" install

  install -m755 -d "${pkgdir}/usr/share/dhclient"
  mv "${pkgdir}/etc/dhclient.conf.example" "${pkgdir}/usr/share/dhclient/"

  install -d "${pkgdir}/var/lib/dhclient"

  # install dhclient linux script
  install -m755 client/scripts/linux "${pkgdir}/usr/bin/dhclient-script"

  # install license
  install -m644 -D LICENSE "${pkgdir}/usr/share/licenses/dhclient/LICENSE"

  # install systemd service unit
  install -m644 -D "$srcdir/dhclient@.service" "${pkgdir}/usr/lib/systemd/system/dhclient@.service"
}
