# Maintainer: David P. <megver83@parabola.nu>
# Contributor: André Silva <emulatorman@hyperbola.info>
# Contributor: Márcio Silva <coadde@hyperbola.info>
# Contributor: Luke Shumaker <lukeshu@sbcglobal.net>

pkgname=linux-libre-firmware
pkgver=1.3.1
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
)
optdepends=('dfu-util: to flash the atusb firmware')
provides=(linux-firmware)
conflicts=(linux-firmware
           linux-firmware-git
           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[@]})
options=(!buildflags !makeflags)
source=(
  "$url/$pkgname-$pkgver.tar.lz"{,.asc}
  0001-Update-carl9170-to-latest-upstream.patch
  0002-Add-firmware-for-the-ATUSB-IEEE-802.15.4-USB-Adapter.patch
  0003-Update-INSTALL-document.patch
  0004-atusb-Build-updates.patch
  0006-Makefile-Change-spaces-for-atusb-to-tab.patch
  0007-Makefile-Add-atusb-to-all.patch
  0008-Makefile-Set-shell-to-bin-bash.patch
)
sha512sums=('d827ec7024b4900dcdf6af8cdd4e72b02596d0ade5c2262460cd573785300498e6aa655b9a59cd22e38a6a787826a564ae33acfac52e68df1d082445e66f18df'
            'SKIP'
            '07263a406cb15c9f91f41c8fb6d4307875665b8106adf526353d64306611458e11ce311842caa9efa623091c897ec9f87cfe54584c9c47590195867958cf4086'
            'a4e8752cfee8dfd0b17ff798d2b5ea7a773ebcf688f7614d1c1a92fe7f092c626a289069a472bc873a77c9cf52f1e37953b0ba47c37a9dfb2a763a695ca77625'
            '1665ce62de148383fe4f30aa5714ef6bb1dfd4eed110415995069830b36a4b1aafb05e4cd6cf428dc0817e2affb91740e41b3fab8d2385683d6c18790bbf5afd'
            '0168ba0f3a8f3938a96b7f72d081283ca04b7ba90c5474d8ccf0716d6964137980f49623c5489271158a48827319a20ee4f19bd94f5bb956ee97d5ba637070c1'
            'fa006627935beb7ae78fad5b63423a54e4eae384761ff618fff173250fb5d0c7eef9d5a707733d037edc95259ef59c9053c4245b80a004928b6809ea1cb43cf8'
            '8014d9eccb4c5193d1b4b056e163b5631c207adc78522e413650eb053c90e7b9e333c9e5cb6f73b63985e27020f4c69e7b791176702c55f711f6db95387e6bff'
            '59560f16978b2fd7d8c828d7609ee4f71cbdc902b70cfb87feca686958dd4df05c61c5e1bfa5e0c2a060f23185a0b9e5d07d487e172764767cdc4b26a6240b79')
validpgpkeys=('F611A908FFA165C699584ED49D0DB31B545A3198') # Jason Self

prepare(){
  cd $pkgname-$pkgver/src

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

  # Use our cross compilers and build carl9170fw with the default config
  sed -e 's|$PWD/../toolchain/inst/bin/||g' \
      -e 's|${CMAKE_SOURCE_DIR}/toolchain/inst/bin/||g' \
      -e '/CMAKE_FIND_ROOT_PATH/d' \
      -e 's/: ath9k_htc_toolchain$/:/' \
      -e 's/: carl9170fw-toolchain$/:/' \
      -e '/cd carl9170fw && .\/autogen.sh$/ s/$/ --alldefconfig/' \
      -i Makefile \
         ath9k_htc/target_firmware/configure \
         carl9170fw/extra/sh-elf-linux.cmake
}

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

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

  make prefix=$pkgdir/usr/lib/firmware install
  install -Dm644 WHENCE $pkgdir/usr/share/licenses/$pkgname/WHENCE
}
