# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Martell Malone <martellmalone@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

pkgname=pacman
pkgver=6.1.0
pkgrel=20
pkgdesc="A library-based package manager with dependency support (MSYS2 port)"
arch=('i686' 'x86_64')
url="https://www.archlinux.org/pacman/"
msys2_repository_url="https://github.com/msys2/msys2-pacman"
msys2_references=(
  "cpe: cpe:/a:pacman_project:pacman"
)
license=('spdx:GPL-2.0-or-later')
depends=('bash>=4.2.045'
         'gettext'
         'gnupg'
         'curl'
         'pacman-mirrors'
         'msys2-keyring'
         'which'
         'bzip2'
         'xz'
         'zstd')
checkdepends=('python')
makedepends=('asciidoc'
             'doxygen'
             'git'
             'gcc'
             'meson'
             'ninja'
             'gettext-devel'
             'heimdal-devel'
             'libarchive-devel'
             'libcurl-devel'
             'libgpgme-devel'
             'libsqlite-devel'
             'libunistring-devel')
backup=("etc/pacman.conf"
        "etc/makepkg.conf"
        "etc/makepkg_mingw.conf")
_commit="e3dc296ba35d5039775c6e53decc7296b3bce396"
source=(pacman::git+https://gitlab.archlinux.org/pacman/pacman.git#commit=${_commit}
        "pacman.conf"
        "makepkg.conf"
        "makepkg_mingw.conf"
        "makepkg_mingw.d.clang64.conf"
        "makepkg_mingw.d.clangarm64.conf"
        "makepkg_mingw.d.mingw32.conf"
        "makepkg_mingw.d.mingw64.conf"
        "makepkg_mingw.d.ucrt64.conf"
        "makepkg-mingw"
        0001-makepkg-clean-up-more-things.patch
        0002-makepkg-build-env-export.patch
        0003-fill_progress.patch
        0004-test-fixes.patch
        0005-Remove-everything-related-to-sudo-fakeroot-file-perm.patch
        0006-change-shebang.patch
        0007-Set-fixed-paths-for-various-tools.patch
        0008-makepkg-port-staticlibs-to-Windows.patch
        0009-makepkg-port-strip-debug-to-Windows.patch
        0010-makepkg-lint_package-Warn-on-package-files-referenci.patch
        0011-Core-update.patch
        0012-Remove-ldconfig.patch
        0013-Change-the-epoch-separator.patch
        0014-makepkg-avoid-creating-.tar-files-with-extended-attr.patch
        0015-pacman-libalpm-ignore-file-conflicts-for-foo.exe-foo.patch
        0016-Change-default-answer-of-all-queries-to-yes.patch
        0017-Use-pipe-instead-of-socket.patch
        0018-pacman-make-file-list-comparisons-between-packages-c.patch
        0019-Workaround-compressing-packages-with-bsdtar-under-Ac.patch
        0020-Fix-asciidoc-argparse.patch
        0021-Export-CC-and-CXX-variables-explicitly.patch
        0022-pacman.c-handle-cr-on-stdin-as-well.patch
        0023-Fix-building-staticlibs-with-LTO.patch
        0024-Make-lint_pkgbuild-opt-in-via-MAKEPKG_LINT_PKGBUILD-.patch
        0025-pacman-key-pass-allow-weak-key-signatures-during-imp.patch
        0026-Disable-autodep-scripts.patch
        0027-Fix-read-after-free-issue-parsing-config-files.patch
        0028-Fetch-signature-and-database-from-the-same-URL.patch
        0029-libmakepkg-remove-MAKEFLAGS-from-buildenv_vars.patch
        0030-libmakepkg-do-not-unset-CHOST-with-buildflags.patch
        0031-repo-add-unconditionally-create-the-database-if-it-i.patch
        0032-Translate-MSYS2-specific-messages-into-Japanese.patch
        0033-fixup-Core-update.patch
        0034-pacman-key-disable-WKD-by-default.patch)
validpgpkeys=('6645B0A8C7005E78DB1D7864F99FFE0FEAE999BD'  # Allan McRae <allan@archlinux.org>
              'B8151B117037781095514CA7BBDFFC92306B1121') # Andrew Gregory (pacman) <andrew@archlinux.org>
sha256sums=('803cba087e713a59866797747c3c63abbc0e4354c45cc2c0467d11e78364d66f'
            '0f25288c70ade80c7fac57d3149209b64a7ba23f00232b7e42103f6330b0c1c0'
            '9ed1492c92d9c46e117738365eee25e8fa622a92b1f4db288e919b3102be3c6b'
            '2bd47103eedebc0a18619a8866f612c3accc89544d73649987e3289af62c35af'
            '5f62ef7f560c5215348bd26ff4a7fd9a7f8857193bbad43f139dbff7df449350'
            '88e163e1ad63cc29614a635cbd64b02ae811c0b22ed2dba27e63e49deec0cae9'
            '09d5073b03e2def2eeb87526904eba990bb05b503623199fa4188c3a4684b314'
            '96d7dfa9f17424918f4efbec2c08b6cc7aa31616324382be7483b96d7559e119'
            '274e5dc04f8213c170da2c15ffaad216846ae047a7f21a34156d64f12ce75677'
            '906b5558f5158143dfee2140a7ee464594a90c58622079f2b51a42d1db425d83'
            '167e9ffe59e55fb07d47e0c9a4bddd9d967c50ec2c8711a0a0bbe4b184fc8b0f'
            '1021dc5950ff9f9efac795688c4b53a1d3cda42b0f270ee755a038bbcf9eccab'
            'af0c734c6efa167bdd9505f0cf8d5d85a8d535a5062a69b6e38473bb43b37aff'
            '635a83766a0cdf47cfc51b648b80ceea1d05cfa664f44785caaeef93681f08bf'
            'ce5e0b89b5054f561585ab1e14ab9c160319def3e5249495d883492edb3341f4'
            '76a3764b5e556b5d68d2b56776082db45e00a9571e1ba6f53a1e4a2041f7d818'
            '69c4fd8cd0322dbdbe99cdb41020222ed84fbeda24781b39eaa154703b64a3fa'
            '7b699981a007c6c3acea5950db4ddee9c2c488083cd4266793d8b22d1843ebc7'
            '53811beed6feaacb0c5ebd7c1e4dcc6de4dde5c06e4d027bdd3ba85bc40a2926'
            '088a3f80ee0c81b9ca02ad83a35a1f45f86e85d37243e1f0ed521caee191ceab'
            'e7b11b8c847b3a4b7062026b73b97561b2a92c60d746e509acace63c65e29ee1'
            'd1432a11149402601d0542b8b9b61b0987714099eeaf18744d4ef5986e49f4d9'
            '09e8c61dc9622cdee94e4ad857675e6408953db3ca60a33288eb5404ad9937eb'
            '87fd787dbd11a7d3472828b1be3d1c283bd177831fcc4d39e8e52592e2c88afc'
            '0a5e65369539ee2fae3856e961609dbabef765fd0db41c61cfe8aa36dcafdd40'
            'c2ce3f1c4926073bcfd99e60be2b5c674f1e91cc1e1ddde33ab3d3b9470e964d'
            '5f40e2dab8459e69d36ad1fba2ee5b5bbdc487f7f4aa4acd052359b41fb1e3e9'
            '9e10f39679b2a047e0eff6da1cd4027b556bdac3106cc1cf0f5fec30cccb7ba7'
            '907c8e4c534cab259f7cc737eb43809c76d1d37514e9ae42efd77d5b74993215'
            '9331939cbb5f7b698855343186e28f1e3e6ecde16b3b63fceb0b030bcbda9eb0'
            'b1a374ab70382e20d00b9a7eee498824005f132dbceba8fdab4fd8c56167f3ef'
            '77b15b3ea34de3f1d0757287192e424fad61b40e2c066da9158cd2659d2b4e93'
            '3009d2c5c4b497eb3f1fd1ce3c2ee4baa8b6d804a213886286edc128786ad554'
            '4bfcc39459e422c842f5bb40db35fe212f110233ecb7372cce3cb10577eaf6fe'
            '9b59b70d8cebd2334f824a6ef6afa685f7301197f1fda7336b95a1ecf772519c'
            'c221e17ebb8637f92e699f27bbb0995a8d16a80fe583e4f6e84a1c5e4147ae7d'
            '8439e55bc3fb8e0e2dacffcd45a27762e538fdcbf6e39432773602e4a26dd9fd'
            '56f660d747241514aee05a497be3924b41d2bf53a0ac77e2d6aee7c718ee7c03'
            '25229ab3f14460d14b58e37984262e26655110d3b2cdee74b345a3ccb8536cf1'
            'c4762de7bb589b5ed84f0474e7a2ba8bf3c6447db153099816cba9cf8cf01cee'
            'f090e9c042c901c52f13a8639577f3a97fcac20fc4f2c21a27e7d9223f5dfe2c'
            '3de0512e99d5b13ecffd192b019ba2f77e0f238112d2578ae10cbb1cbdd39e47'
            'd093221c40c9a96e0303f299f5df31f5cd100e83e8ceb9eccaffee07a60ef338'
            '5a31b6b09db89935664ec3e640e3a24a03c75657c3d61463c0e38f7b7da34339')

apply_git_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    git apply "${srcdir}/${_patch}"
  done
}

prepare() {
  cd ${srcdir}/${pkgname}

  # fixup symlinks
  git config core.symlinks true
  git reset HEAD --hard

 apply_git_with_msg 0001-makepkg-clean-up-more-things.patch \
  0002-makepkg-build-env-export.patch \
  0003-fill_progress.patch \
  0004-test-fixes.patch \
  0005-Remove-everything-related-to-sudo-fakeroot-file-perm.patch \
  0006-change-shebang.patch \
  0007-Set-fixed-paths-for-various-tools.patch \
  0008-makepkg-port-staticlibs-to-Windows.patch \
  0009-makepkg-port-strip-debug-to-Windows.patch \
  0010-makepkg-lint_package-Warn-on-package-files-referenci.patch \
  0011-Core-update.patch \
  0012-Remove-ldconfig.patch \
  0013-Change-the-epoch-separator.patch \
  0014-makepkg-avoid-creating-.tar-files-with-extended-attr.patch \
  0015-pacman-libalpm-ignore-file-conflicts-for-foo.exe-foo.patch \
  0016-Change-default-answer-of-all-queries-to-yes.patch \
  0017-Use-pipe-instead-of-socket.patch \
  0018-pacman-make-file-list-comparisons-between-packages-c.patch \
  0019-Workaround-compressing-packages-with-bsdtar-under-Ac.patch \
  0020-Fix-asciidoc-argparse.patch \
  0021-Export-CC-and-CXX-variables-explicitly.patch \
  0022-pacman.c-handle-cr-on-stdin-as-well.patch \
  0023-Fix-building-staticlibs-with-LTO.patch \
  0024-Make-lint_pkgbuild-opt-in-via-MAKEPKG_LINT_PKGBUILD-.patch \
  0025-pacman-key-pass-allow-weak-key-signatures-during-imp.patch \
  0026-Disable-autodep-scripts.patch \
  0027-Fix-read-after-free-issue-parsing-config-files.patch \
  0028-Fetch-signature-and-database-from-the-same-URL.patch \
  0029-libmakepkg-remove-MAKEFLAGS-from-buildenv_vars.patch \
  0030-libmakepkg-do-not-unset-CHOST-with-buildflags.patch \
  0031-repo-add-unconditionally-create-the-database-if-it-i.patch \
  0032-Translate-MSYS2-specific-messages-into-Japanese.patch \
  0033-fixup-Core-update.patch \
  0034-pacman-key-disable-WKD-by-default.patch
}

build() {
  cd ${srcdir}/${pkgname}

  # https://gitlab.archlinux.org/pacman/pacman/-/issues/254#note_289040
  CFLAGS+=" -fzero-init-padding-bits=unions"

  LDFLAGS+=" -static-libgcc" \
  meson setup build-${CARCH} \
    --buildtype=plain \
    --prefix=/usr \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --default-library=static \
    -Dbuildstatic=true \
    -Ddoc=enabled \
    -Ddoxygen=enabled \
    -Dgpgme=enabled \
    -Dcurl=enabled \
    -Duse-git-version=false \
    -Dpkg-ext=.pkg.tar.xz \
    -Dscriptlet-shell=/usr/bin/bash

  meson compile -C build-${CARCH}
}

check() {
  cd ${srcdir}/${pkgname}
  meson test -C build-${CARCH}
}

package() {
  cd ${srcdir}/${pkgname}
  DESTDIR=${pkgdir} meson install -C build-${CARCH}

  # install Arch specific stuff
  install -dm755 ${pkgdir}/etc
  install -m644 ${srcdir}/pacman.conf ${pkgdir}/etc/pacman.conf

  # Fixup .pc file
  sed -i "s|/usr/.*libintl.a|-lintl|g" ${pkgdir}/usr/lib/pkgconfig/libalpm.pc

  case "${CARCH}" in
  i686)
    mycarch="i686"
    mychost="i686-pc-cygwin"
    myflags="-march=pentium4"
  ;;
  x86_64)
    mycarch="x86_64"
    mychost="x86_64-pc-cygwin"
    myflags="-march=nocona -msahf"
  ;;
  esac

  install -m644 ${srcdir}/makepkg.conf ${pkgdir}/etc/
  install -m644 ${srcdir}/makepkg_mingw.conf ${pkgdir}/etc/
  install -dm755 ${pkgdir}/etc/makepkg_mingw.d
  for f in "${srcdir}/"makepkg_mingw.d.*.conf; do
    install -m644 "${f}" "${pkgdir}/etc/makepkg_mingw.d/${f#${srcdir}/makepkg_mingw.d.}"
  done
  install -m755 ${srcdir}/makepkg-mingw ${pkgdir}/usr/bin/

  # set things correctly in the default conf file
  sed -i ${pkgdir}/etc/makepkg.conf \
    -e "s|@CARCH[@]|${mycarch}|g" \
    -e "s|@CHOST[@]|${mychost}|g" \
    -e "s|@CARCHFLAGS[@]|${myflags}|g"
}
