# Maintainer: Developer <dev@opalos.org>
# Maintainer: Developer <dev@opalos.org>

pkgname=wpa_supplicant
pkgver=%version%
pkgrel=%release%
epoch=2
pkgdesc='A utility providing key negotiation for WPA wireless networks'
url='https://w1.fi/wpa_supplicant/'
arch=(x86_64)
license=(BSD-3-Clause)
depends=(openssl libdbus readline libnl pcsclite)
install=wpa_supplicant.install
source=(
  https://w1.fi/releases/${pkgname}-${pkgver}.tar.gz{,.asc}
  wpa_supplicant_config

  # More permissive TLS fallback
  0001-Enable-TLSv1.0-by-default.patch

  # Unit improvements from Ubuntu
  0002-Tweak-D-Bus-systemd-service-activation-configuration.patch
  0003-Add-IgnoreOnIsolate-yes-to-keep-wpa-supplicant-runni.patch

  # http://lists.infradead.org/pipermail/hostap/2022-May/040511.html
  # https://bugs.archlinux.org/task/76474
  0004-Allow-legacy-renegotiation-to-fix-PEAP-issues-with-s.patch

  # http://lists.infradead.org/pipermail/hostap/2022-May/040571.html
  # https://bugs.archlinux.org/task/76474
  0005-OpenSSL-Drop-security-level-to-0-with-OpenSSL-3.0-wh.patch

  # https://salsa.debian.org/debian/wpa/-/commit/13e1d28e4f987a220c546df94df86bb9b2371874
  0006-Disable-Werror-for-eapol_test.patch

  # http://lists.infradead.org/pipermail/hostap/2022-January/040178.html
  0007-nl80211-add-extra-ies-only-if-allowed-by-driver.patch

  # https://lists.infradead.org/pipermail/hostap/2022-April/040352.html
  0008-AP-guard-FT-SAE-code-with-CONFIG_IEEE80211R_AP.patch

  # https://w1.fi/cgit/hostap/commit/?id=e9b4ad2364c68130c7618a88a171e29e0e15007e
  0009-OpenSSL-Apply-connection-flags-before-reading-certif.patch

  # https://bugs.archlinux.org/task/78770
  0010-Don-t-upgrade-SSL-security-level-to-1-when-setting-c.patch

  # More unit improvements from Debian
  0011-Add-reload-support-to-the-systemd-unit-files.patch

  # https://salsa.debian.org/debian/wpa/-/commit/5e9486d9de777c9145bcc8bf8048a08abf539cac
  0012-WNM-Choose-the-best-available-BSS-not-just-the-first.patch
  0013-wpa_supplicant-Fix-wpa_supplicant-configuration-pars.patch

  # https://salsa.debian.org/debian/wpa/-/commit/f0d078409df01fa466523c2ab015274c351a83c0
  0014-Abort-ongoing-scan.patch

  # https://salsa.debian.org/debian/wpa/-/commit/fdcff9f7b4c699f94e38e519e34220e528bee2f9
  0015-Override-ieee80211w-from-pmf-for-AP-mode-in-wpa_supp.patch
)
validpgpkeys=('EC4AA0A991A5F2464582D52D2B6EF432EFC895FA') # Jouni Malinen
sha256sums=('20df7ae5154b3830355f8ab4269123a87affdea59fe74fe9292a91d0d7e17b2f'
            'SKIP'
            '1e32af4a1a147ee75358fd0b5636fb41332a7b91ec7a904292178256f735e9d2'
            'cec1f952a47a5688fe3d23bc998f266a0a5a6214047590a19410f6521ed68811'
            '7aecdb7ce85a399eee75dae4595645b34260739725b5922726a51a8db76148f9'
            'fb82577605c960a53b9376ae3928ab0c052c6f371096e0d692fbf95ef9117475'
            '03630ca4c54a28bd1fe4f87d5afe49caaa67d6d6937534de25bbfbd306d1f4c1'
            'cf25c805c9f65f529d7bbfbeabba4317fe7ae1529aca146fc3e8f0c777118d0e'
            'fa285fdbde33a345d1d5c91344c7a393d40e8cb4d16ac9c9297982fe16bfad77'
            'b15f14529bdcc1f81c60dec5f6ecab7a99d707e4f2803cc3b2bdb12b6cb5d609'
            '19083e95a4e2d55d49c1a17ba69b0f4a672f7ec9a0a0a068c2aa59b57d781895'
            '3091f0628935452938a4a5be4eef47a2dfe69d7e81afdd4275f0a01ec7e6b9c5'
            'ce900a58e015fc7bbcbf13a9f5c56122d69882f66c38d35c1e44bc25c133285a'
            '0a76943fdc64e80015bc21c873a350ac82d23e662541712a4f92f51f6fe10c72'
            'cf1f5de43f825305f1bf4a219aee1a521ec3a59e919e5e5ac3367139faac1b28'
            '70cc90bdb9349b7dea06ebbd776c94c76b497d8ca26d328d8ab12d504c578787'
            '3a98842a405df9b3e5a10734ab106744c392fd677ac652f994610420ccf039a0'
            '39ee3f6dbf665f167cec8d33f0ba9f97a64aaa22f87f9cc5ac3e41f7b42ca559')

prepare() {
  cd $pkgname-$pkgver

  local src
  for src in "${source[@]}"; do
    [[ $src = *.patch ]] || continue
    echo "Applying patch $src..."
    patch -Np1 < "../$src"
  done

  cp ../wpa_supplicant_config $pkgname/.config
}

build() {
  cd $pkgname-$pkgver/$pkgname

  make LIBDIR=/usr/lib BINDIR=/usr/bin
  make LIBDIR=/usr/lib BINDIR=/usr/bin eapol_test
}

package() {
  cd $pkgname-$pkgver/$pkgname

  make LIBDIR=/usr/lib BINDIR=/usr/bin DESTDIR="$pkgdir" install

  install -Dm755 eapol_test "$pkgdir/usr/bin/eapol_test"

  install -dm755 "$pkgdir/etc/wpa_supplicant"
  install -Dm644 wpa_supplicant.conf -t "$pkgdir/usr/share/doc/wpa_supplicant"

  install -Dm644 dbus/fi.w1.wpa_supplicant1.service \
    -t "$pkgdir/usr/share/dbus-1/system-services"

  install -Dm644 dbus/dbus-wpa_supplicant.conf \
    "$pkgdir/usr/share/dbus-1/system.d/wpa_supplicant.conf"

  install -Dm644 doc/docbook/*.5 -t "$pkgdir/usr/share/man/man5"
  install -Dm644 doc/docbook/*.8 -t "$pkgdir/usr/share/man/man8"
  rm "$pkgdir"/usr/share/man/man8/wpa_{priv,gui}.8

  install -Dm644 systemd/*.service -t "$pkgdir/usr/lib/systemd/system"

  install -Dm644 ../README "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
}
