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

pkgbase=linux-libre-firmware
pkgname=(
  linux-libre-firmware
  linux-libre-firmware-whence
  linux-libre-firmware-other

  # Splits
  linux-libre-firmware-atheros
  linux-libre-firmware-broadcom
  linux-libre-firmware-intel
)
pkgver=1.5
pkgrel=1
epoch=1
pkgdesc='Firmware files for Linux-libre'
url='https://jxself.org/firmware'
license=(LicenseRef-WHENCE)
arch=(any)
provides=(linux-firmware)
conflicts=(linux-firmware)
replaces=(linux-firmware)
makedepends=(
  cmake                       # ath9k_htc and carl9170fw
  arm-linux-gnueabi-binutils  # av7110
  arm-none-eabi-gcc           # aica
  avr-libc dfu-util           # atusb
  sh-elf-gcc sh-elf-newlib    # carl9170fw
  xtensa-elf-gcc              # ath9k_htc
)
options=(!buildflags !makeflags)
source=("$url/$pkgbase-$pkgver.tar.lz"{,.asc}
        0001-av7110-Build-Boot.S-with-arm-linux-gnueabi-as-instea.patch
        0002-ath9k_htc-bump-CMAKE_MINIMUM_REQUIRED-to-version-3.5.patch)
sha256sums=('77858e842ee249d32b8b950cdfd12824d63f515f9317dd030fb89ee9e0bc889f'
            'SKIP'
            '2b1f6ca72e8b71096c9375cf2276914677bb6c3bd8c055d1895d8790882999cc'
            '7029edeb4c25fe45ba448d7950ca61fba1e9b55c0508dd3ff79226de07d89a3a')
validpgpkeys=('F611A908FFA165C699584ED49D0DB31B545A3198') # Jason Self <j@jxself.org>

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

  patch -Np1 -i "$srcdir"/0001-av7110-Build-Boot.S-with-arm-linux-gnueabi-as-instea.patch
  patch -Np1 -i "$srcdir"/0002-ath9k_htc-bump-CMAKE_MINIMUM_REQUIRED-to-version-3.5.patch

  # Use the system sh-elf toolchain
  sed -e 's|${CMAKE_SOURCE_DIR}/toolchain/inst/bin/||g' \
      -e '/CMAKE_FIND_ROOT_PATH/d' \
      -i carl9170fw/extra/sh-elf-linux.cmake

  # Set dummy Makefiles to skip building the toolchains
  cat <<EOF | tee ath9k_htc/Makefile carl9170fw/toolchain/Makefile >/dev/null
.DEFAULT_GOAL := all

all:
	@:

%:
	@:
EOF
}

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

  # a56 uses legacy K&R C code and requires GNU89 compatibility
  make CPPFLAGS="-std=gnu89" a56

  # Build carl9170fw with the default config
  pushd carl9170fw
  ./autogen.sh --alldefconfig
  popd

  # ath9k_htc firmware uses old Xtensa code that triggers
  # incompatible-pointer-types errors with modern GCC versions.
  # The firmware build system sets the compiler through CROSS_COMPILE.
  CFLAGS="$CFLAGS -Wno-error=incompatible-pointer-types" \
  CROSS_COMPILE=xtensa-elf- \
  make ath9k_htc

  # Build the rest of the firmware
  make
}

_pick() {
  local p="$1" f d; shift
  for f; do
    d="$srcdir/$p/${f#${pkgdir}/}"
    mkdir -p "$(dirname "$d")"
    mv "$f" "$d"
    rmdir -p --ignore-fail-on-non-empty "$(dirname "$f")"
  done
}

package_linux-libre-firmware() {
  pkgdesc+=" - Default set"
  license=(CC0-1.0)
  depends=(
    linux-libre-firmware-atheros
    linux-libre-firmware-broadcom
    linux-libre-firmware-intel
    linux-libre-firmware-other
  )
  provides=(${provides[@]/%/${pkgname#${pkgbase}}})
  conflicts=(${conflicts[@]/%/${pkgname#${pkgbase}}})
  replaces=(${replaces[@]/%/${pkgname#${pkgbase}}})
}

package_linux-libre-firmware-whence() {
  pkgdesc+=" - WHENCE file (vendor licenses)"
  provides=(${provides[@]/%/${pkgname#${pkgbase}}})
  conflicts=(${conflicts[@]/%/${pkgname#${pkgbase}}})
  replaces=(${replaces[@]/%/${pkgname#${pkgbase}}})

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

package_linux-libre-firmware-other() {
  pkgdesc+=" - Unsorted firmware for various devices"
  provides=(${provides[@]/%/${pkgname#${pkgbase}}})
  conflicts=(${conflicts[@]/%/${pkgname#${pkgbase}}})
  replaces=(${replaces[@]/%/${pkgname#${pkgbase}}})

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

  # split
  _pick atheros "${fwdir}"/{ath*,carl9170*,htc_*}

  _pick broadcom "${fwdir}"/b43-open

  _pick intel "${fwdir}"/isci
}

package_linux-libre-firmware-atheros() {
  pkgdesc+=" - Firmware for Qualcomm Atheros WiFi adapters"
  license+=('BSD-3-Clause AND MIT AND GPL-2.0-or-later')
  depends=(linux-firmware-whence)
  provides=(${provides[@]/%/${pkgname#${pkgbase}}})
  conflicts=(${conflicts[@]/%/${pkgname#${pkgbase}}})
  replaces=(${replaces[@]/%/${pkgname#${pkgbase}}})

  mv -v ${pkgname#linux-libre-firmware-}/* "${pkgdir}"

  install -Dm644 \
    $pkgbase-$pkgver/src/ath9k_htc/LICENCE.TXT \
    $pkgbase-$pkgver/src/ath9k_htc/NOTICE.TXT \
    -t "${pkgdir}/usr/share/licenses/${pkgname}"
}

package_linux-libre-firmware-broadcom() {
  pkgdesc+=" - Firmware for supported Broadcom 43xx network adapters"
  license+=(GPL-2.0-only)
  depends=(linux-firmware-whence)
  provides=(${provides[@]/%/${pkgname#${pkgbase}}})
  conflicts=(${conflicts[@]/%/${pkgname#${pkgbase}}})
  replaces=(${replaces[@]/%/${pkgname#${pkgbase}}})

  mv -v ${pkgname#linux-libre-firmware-}/* "${pkgdir}"

  install -Dm644 $pkgbase-$pkgver/src/openfwwf/LICENSE \
    -t "${pkgdir}/usr/share/licenses/${pkgname}"
}

package_linux-libre-firmware-intel() {
  pkgdesc+=" - Firmware for Intel devices"
  license+=(GPL-2.0-only)
  depends=(linux-firmware-whence)
  provides=(${provides[@]/%/${pkgname#${pkgbase}}})
  conflicts=(${conflicts[@]/%/${pkgname#${pkgbase}}})
  replaces=(${replaces[@]/%/${pkgname#${pkgbase}}})

  mv -v ${pkgname#linux-libre-firmware-}/* "${pkgdir}"
}
