# Maintainer (Arch): David Runge <dvzrv@archlinux.org>
# Maintainer: André Silva <emulatorman@hyperbola.info>
# Contributor: Tobias Dausend <throgh@hyperbola.info>

pkgname=gnu-efi
pkgver=3.0.9
_debver=$pkgver
_debrel=2
pkgrel=2
pkgdesc="Develop EFI applications using the GNU toolchain and the EFI development environment"
arch=('i686' 'x86_64')
url='https://sourceforge.net/projects/gnu-efi/'
license=('Simplified-BSD')
makedepends=('quilt')
source=("https://download.sourceforge.net/${pkgname}/${pkgname}-${pkgver}.tar.bz2"
        "https://deb.debian.org/debian/pool/main/g/gnu-efi/gnu-efi_${_debver}-${_debrel}.debian.tar.xz"
        "${pkgname}-3.0.9-ldflags.patch")
sha512sums=('1a775476fcbe354e57c9db258b1c6ad4346b84d1794178ab5d5195b3ffba250066ca4c42a553d5b2866fa1b4e03019d61df0197f269188c73297042990adf316'
            'fbee202d818be85b576222157cb5191f301c2962f77ee904f370fb8841a722ea37f8bf919e1f0c7c8889bd0e00c590028d5a24d49eb2b8293583438bdac29350'
            '7fd1c8d09ff870c33ae62887b657a7367f85a07f6709d33471ca13117a08e755853fd1e99db492d1db47444b95ae871078c4a913ed70aa7236e49349046e06b6')

prepare() {
  cd "$pkgname-$pkgver"
  if [[ ${pkgver%.*} = ${_debver%.*} ]]; then
    # Debian patches
    export QUILT_PATCHES=debian/patches
    export QUILT_REFRESH_ARGS='-p ab --no-timestamps --no-index'
    export QUILT_DIFF_ARGS='--no-timestamps'

    mv "$srcdir"/debian .

    quilt push -av
  fi

  # -Werror, not even once
  sed -e 's/-Werror//g' -i Make.defaults

  # insert LDFLAGS into custom linker for applications
  patch -Np1 -i "${srcdir}/${pkgname}-3.0.9-ldflags.patch"
}

build() {
  cd "$pkgname-$pkgver"
  make
  make -C lib
  make -C gnuefi
  make -C inc

  # unset LDFLAGS for custom linker used in applications, as we have patched our
  # LDFLAGS in manually in prepare()
  LDFLAGS=""
  make -C apps
}

package() {
  cd "$pkgname-$pkgver"
  make INSTALLROOT="$pkgdir/" PREFIX='/usr' install
  install -Dm644 apps/*.efi -t "${pkgdir}/usr/share/${pkgname}/apps/$CARCH"
  install -Dm644 README.efilib -t "${pkgdir}/usr/share/licenses/${pkgname}"
  install -Dm644 {ChangeLog,README.{gnuefi,git,elilo}} -t "${pkgdir}/usr/share/doc/${pkgname}"
}
