# Maintainer: Johannes Schindelin <johannes.schindelin@gmx.de>

_realname=7zip
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=25.00
pkgrel=1
pkgdesc="A file archiver with a high compression ratio (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://7-zip.org"
msys2_references=(
  'aur: 7-zip'
  "cpe: cpe:/a:7-zip:7-zip"
)
license=('spdx:LGPL-2.1-or-later AND BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc")
source=("$url/a/7z${pkgver//./}-src.tar.xz"
        001-disable-static.patch
        002-fix-build-on-aarch64.patch)
sha256sums=('bff9e69b6ca73a5b8715d7623870a39dc90ad6ce1f4d1070685843987af1af9b'
            '80678c664cefb38e6402c83fc0878911c142e471ce42665291bb313d8c8e5185'
            '1162ef09dfec3a6622998141e8b1b8841e71ab94e3a9fffac057c5034550ead5')

prepare () {
  if [[ ${CC} == clang ]] ; then
    sed -i 's/-Werror/ -Wno-missing-exception-spec -Wno-unused-but-set-variable -Wno-unknown-attributes/' */*.mak */*/*.mak
  fi
  patch -p1 -i "${srcdir}"/001-disable-static.patch

  patch -p1 -i "${srcdir}"/002-fix-build-on-aarch64.patch
}

build() {
  [[ -d build-${MSYSTEM} ]] && rm -rf build-${MSYSTEM}
  mkdir -p build-${MSYSTEM}
  cp -r CPP build-${MSYSTEM}/CPP
  cp -r C build-${MSYSTEM}/C
  cd build-${MSYSTEM}

  make -C CPP/7zip/Bundles/Format7zF -f makefile.gcc
  make -C CPP/7zip/UI/Console -f makefile.gcc
  # undo 001-disable-static.patch for SFXCon in particular, it should be
  # standalone
  make -C CPP/7zip/Bundles/SFXCon -f makefile.gcc LDFLAGS_STATIC_2=-static
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}

  install -D -m755 $(find CPP/7zip/Bundles/ -name \*.dll) -t "${pkgdir}"${MINGW_PREFIX}/bin/
  install -D -m755 CPP/7zip/UI/Console/_o/7z.exe "${pkgdir}"${MINGW_PREFIX}/bin/7z.exe
  install -D -m755 CPP/7zip/Bundles/SFXCon/_o/7zCon.exe "${pkgdir}"${MINGW_PREFIX}/lib/7zip/7zCon.sfx

  install -d "${pkgdir}"${MINGW_PREFIX}/share/doc/7zip
  install -D -m644 "${srcdir}"/DOC/* "${pkgdir}"${MINGW_PREFIX}/share/doc/7zip

  install -Dm644 "${srcdir}"/DOC/copying.txt -t "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/
  install -Dm644 "${srcdir}"/DOC/License.txt -t "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/
  install -Dm644 "${srcdir}"/DOC/unRarLicense.txt -t "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/
}
