# Contributor: Vaporeon <vaporeon@vaporeon.io>
# Contributor: Andrew Gunnerson <andrewgunnerson@gmail.com>
# Contributor: Dan McGee <dan@archlinux.org>

pkgname=mingw-w64-clang-aarch64-libarchive-reduced
_pkgname=libarchive
pkgver=3.8.1
pkgrel=1.1
pkgdesc="library that can create and read several streaming archive formats (mingw-w64-clang-aarch64)"
arch=(any)
url="http://www.libarchive.org/"
license=('BSD-2-Clause')
depends=('mingw-w64-clang-aarch64-crt'
         'mingw-w64-clang-aarch64-bzip2'
         'mingw-w64-clang-aarch64-lz4'
         'mingw-w64-clang-aarch64-xz'
         'mingw-w64-clang-aarch64-zlib'
         'mingw-w64-clang-aarch64-zstd')
makedepends=('git' 'mingw-w64-clang-aarch64-configure')
conflicts=('mingw-w64-clang-aarch64-libarchive')
provides=('mingw-w64-clang-aarch64-libarchive')
options=('!buildflags' '!debug' 'staticlibs' '!strip')
validpgpkeys=('A5A45B12AD92D964B89EEE2DEC560C81CEC2276E'  # Martin Matuska <mm@FreeBSD.org>
              'DB2C7CF1B4C265FAEF56E3FC5848A18B8F14184B') # Martin Matuska <martin@matuska.org>
source=("git+https://github.com/${_pkgname}/${_pkgname}.git?signed#tag=v${pkgver}")
sha256sums=('1857fc0288334ba5de7a0e9acbec647f610925c692c513a1a8b2e06dc11035c4')

_architectures="aarch64-w64-mingw32"

_backports=(
)

_reverts=(
)

prepare() {
  cd "${_pkgname}"

  local _c _l
  for _c in "${_backports[@]}"; do
    if [[ "${_c}" == *..* ]]; then _l='--reverse'; else _l='--max-count=1'; fi
    git log --oneline "${_l}" "${_c}"
    git cherry-pick --mainline 1 --no-commit "${_c}"
  done
  for _c in "${_reverts[@]}"; do
    if [[ "${_c}" == *..* ]]; then _l='--reverse'; else _l='--max-count=1'; fi
    git log --oneline "${_l}" "${_c}"
    git revert --mainline 1 --no-commit "${_c}"
  done

  autoreconf -fiv
}

build() {
  export USE_COMPILER_WRAPPERS=1
  cd "${srcdir}/${_pkgname}"
  for _arch in ${_architectures}; do
    source mingw-clang-env $_arch
    mkdir -p build-${_arch} && pushd build-${_arch}
    ${_arch}-configure \
        --without-iconv \
        --without-libb2 \
        --without-lzo2 \
        --without-mbedtls \
        --without-openssl \
        --without-nettle \
        --without-xml2 \
        ..
    make
    popd
  done
}

package() {
  export USE_COMPILER_WRAPPERS=1
  for _arch in ${_architectures}; do
    source mingw-clang-env $_arch
    cd "${srcdir}/${_pkgname}/build-${_arch}"
    make install DESTDIR="${pkgdir}"
    $STRIP --strip-unneeded "${pkgdir}"/usr/${_arch}/bin/*.exe
    $STRIP --strip-unneeded "${pkgdir}"/usr/${_arch}/bin/*.dll
    [[ $pkgname =~ .*-clang-.* ]] || $STRIP -g "${pkgdir}"/usr/${_arch}/lib/*.a
    rm -r "${pkgdir}/usr/${_arch}/share"
  done

  cd "${srcdir}/${_pkgname}"
  install -Dm0644 COPYING "${pkgdir}/usr/share/licenses/$pkgname/COPYING"
}
