# Maintainer: David P. <megver83@parabola.nu>

pkgbase=linux-libre-firmware
pkgname=(linux-libre-firmware{,-whence})
pkgver=1.4
pkgrel=2
epoch=1
pkgdesc='Firmware files for Linux-libre'
arch=(any)
url='https://jxself.org/firmware'
license=(GPL3)
makedepends=(
  arm-linux-gnueabi-gcc           # av7110
  arm-none-eabi-gcc               # aica
  avr-gcc avr-libc dfu-util       # atusb
  sh-elf-gcc sh-elf-newlib cmake  # carl9170fw
  xtensa-elf-gcc                  # ath9k_htc
)
options=(!buildflags !makeflags)
source=("$url/$pkgbase-$pkgver.tar.lz"{,.asc})
sha512sums=('3b4c8afb63c55f05df0b2686459c6bb2c0cb56db8384d5527506ff4a14b8de80e1a3037a9b57cccccd6953d869c6a71a6500c4c7b735253ea0b57c79854d9457'
            'SKIP')
validpgpkeys=('F611A908FFA165C699584ED49D0DB31B545A3198') # Jason Self <j@jxself.org>

prepare() {
  cd "$pkgbase-$pkgver/src"

  # Use our cross compilers
  sed -e 's|$PWD/../toolchain/inst/bin/||g' \
      -i ath9k_htc/target_firmware/configure
  sed -e 's|${CMAKE_SOURCE_DIR}/toolchain/inst/bin/||g' \
      -e '/CMAKE_FIND_ROOT_PATH/d' \
      -i carl9170fw/extra/sh-elf-linux.cmake

  # Remove toolchains and build carl9170fw with the default config
  sed -e 's/: ath9k_htc_toolchain$/:/' \
      -e 's/: carl9170fw-toolchain$/:/' \
      -e '/cd carl9170fw && .\/autogen.sh$/ s/$/ --alldefconfig/' \
      -i Makefile
}

build() {
  make -C "$pkgbase-$pkgver/src"
}

package_linux-libre-firmware() {
  provides=(linux-firmware)
  conflicts=(
    linux-firmware
    linux-firmware-git
    linux-firmware-bnx2x
    linux-firmware-liquidio
    linux-firmware-marvell
    linux-firmware-mellanox
    linux-firmware-nfp
    linux-firmware-qcom
    linux-firmware-qlogic
    kernel26-firmware
    ar9170-fw
    iwlwifi-1000-ucode
    iwlwifi-3945-ucode
    iwlwifi-4965-ucode
    iwlwifi-5000-ucode
    iwlwifi-5150-ucode
    iwlwifi-6000-ucode
    rt2870usb-fw
    rt2x00-rt61-fw
    rt2x00-rt71w-fw
    openfwwf
    ath9k-htc-firmware
  )
  replaces=("${conflicts[@]}")
  optdepends=('dfu-util: to flash the atusb firmware')

  make -C "$pkgbase-$pkgver/src" prefix="$pkgdir/usr/lib/firmware" install
}

package_linux-libre-firmware-whence() {
  pkgdesc+=" - contains the WHENCE license file which documents the vendor license details"
  provides=(linux-firmware-whence)
  conflicts=(linux-firmware-whence)
  replaces=(linux-firmware-whence)

  cd "$pkgbase-$pkgver/src"
  install -Dm644 WHENCE "$pkgdir/usr/share/licenses/$pkgname/WHENCE"
}
