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

pkgname=pacman
pkgver=6.0.2
pkgrel=14
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="c2d4568d35173f92c17b6b93222bc101a63c9928"
source=(pacman-${pkgver}::git+https://gitlab.archlinux.org/pacman/pacman.git#commit=${_commit}
        "pacman.conf"
        "makepkg.conf"
        "makepkg_mingw.conf"
        "makepkg-mingw"
        0001-makepkg-clean-up-more-things.patch
        0002-makepkg-bash-v4-compat.patch
        0003-makepkg-find_libdepends-port-to-Windows.patch
        0004-makepkg-build-env-export.patch
        0005-makepkg-find_libprovides-port-to-Windows.patch
        0006-fill_progress.patch
        0007-test-fixes.patch
        0008-Remove-everything-related-to-sudo-fakeroot-file-perm.patch
        0009-change-shebang.patch
        0010-Set-fixed-paths-for-various-tools.patch
        0011-makepkg-port-staticlibs-to-Windows.patch
        0012-makepkg-port-strip-debug-to-Windows.patch
        0013-makepkg-lint_package-Warn-on-package-files-referenci.patch
        0014-More-debugging-info.patch
        0015-Core-update.patch
        0016-Remove-ldconfig.patch
        0017-Change-the-epoch-separator.patch
        0018-makepkg-avoid-creating-.tar-files-with-extended-attr.patch
        0019-pacman-libalpm-ignore-file-conflicts-for-foo.exe-foo.patch
        0020-Change-default-answer-of-all-queries-to-yes.patch
        0021-Use-pipe-instead-of-socket.patch
        0022-pacman-make-file-list-comparisons-between-packages-c.patch
        0023-Translate-MSYS2-specific-messages-into-Japanese.patch
        0024-Workaround-compressing-packages-with-bsdtar-under-Ac.patch
        0025-Fix-asciidoc-argparse.patch
        0026-Export-CC-and-CXX-variables-explicitly.patch
        0027-pacman.c-handle-cr-on-stdin-as-well.patch
        0028-Add-a-CI-job.patch
        0029-Fix-building-staticlibs-with-LTO.patch
        0030-Make-lint_pkgbuild-opt-in-via-MAKEPKG_LINT_PKGBUILD-.patch
        0031-libmakepkg-fix-compatibility-with-bash-5.2-patsub_re.patch
        0032-pacman-key-pass-allow-weak-key-signatures-during-imp.patch)
validpgpkeys=('6645B0A8C7005E78DB1D7864F99FFE0FEAE999BD'  # Allan McRae <allan@archlinux.org>
              'B8151B117037781095514CA7BBDFFC92306B1121') # Andrew Gregory (pacman) <andrew@archlinux.org>
sha256sums=('SKIP'
            '53c0c2d42bc10f265aa41bc412a6ebc2d98177d9356b0fa9a2a130caec46ac2d'
            'fa6c5fc52192484a3419a25bfae802d93be4103fc57281318b55dbea80cb9646'
            'bbc323d1fa057c31ff5b8329eb1e6fdfde5af98ece7a3e7e2982f91a9ec1f23d'
            '98198e1f0f252eae0560d271bee4b9149e127399dd0d3fd5d8d24579d9e0550f'
            'd272176dea508bf0972dde6396ca655e900d509099d0a496bd6a138f98bb48df'
            '7e2a2fc6799ed8a9dbc8b0712b162be963ed22351e4cb29b3a4d4a4d3f28d7ed'
            '953b66200dbe4f5b3a9caac2ce25f1ded7918232dce2e61b4bf64f5d87892d07'
            'ca1ec64e74ed557ccf3db8d0ff5e22a52b0b52b6451454f76fd02d1eac082484'
            'cdcdcdbc4496caa852131c72f65ac770d68fec09e259509b1398a99e6e9dc5d6'
            '4b3e3cd1ba7d0f513b75a83b27550f308a4a6731427ed27fc7fedf5aa57d57b2'
            'd49fd3812a824dceb2198bac392b7b635bf96ec4382ac899f9c3dac55ec6aeff'
            '98b7e35864a3a24bb882a2eeab1b1c4db28f4c4d15af1e4340a35e0afb3877f1'
            '11cfe55af4ebe46427f55ef4e02fd5862b9936dcc0cae46a2b9693dcd676a17e'
            '27d65613f58f3c114eda732a5683bfe10c48f36a576e5fa913492f55e8152a92'
            'c554b9fdccb3815e9db3594533fe2a9c890049d02eb884b19ac3c892e09910b6'
            'a188897adfcee5744a2932cfdbb906660b21c93f9b1318037d0d4267b4b574b6'
            '3061622244395175c7330d369f464c4a65c7e75b97dcc13437e8561fa29150fc'
            '9508ddbc126e97bcfb9e0c9c8de6573f3abbf8b2d18814ce2c8576b285eca502'
            '90aab35cf01950423638b7af3c3931d4a342b3671086814b99d53e3f847f3736'
            '9ade03d4ce8644542f79d847852bf37714567b99d5b183cb7d10a5f53597dcde'
            '6c5e124d483b64e7b0ad7aa001c943550d5be76965f2272708083b0995c99af2'
            '5d27de39618780371a627ed020624bdce05b3e11784b23941e9f5248e7087f3f'
            '7285a21f2c1082ca51befe90daaea19784c22a839e94da882d0d80f23a9f03e3'
            '01443d77fb152d631d921b09b9fb50283bee39c6929fcd7ffd3736fc814661d9'
            '609101251745f901ec7adcbd9e4e518360d75a8a41c5748f38a475630b79f739'
            '016b85da0d455604daac43a03af8aaf7240392ad628c1dd3ac3897f4723fe35b'
            '8ffe18df93d77caced48357e31995887f06f5685181a5a6e11f0a7d2740f2ca8'
            'ca60e60bfb4b35f04fec8c73aa551cf532c860c44c26b5b66c9c66a095c46491'
            '75035088a9030316977b7f953ba75489bac629d500827daa481f1ddd0dc2c0b1'
            '04a40d840df9c834c273849224ab462ec5eaa55850f4b9c71e7479484114e856'
            '06b0abe321240d19127e0578ee2b7607d3ce38c62c696e48f71b58f181134c56'
            '347222818ffbbc2b72bfe7cdb9e00247cae240372ac629aa5fd1e9386813d9ca'
            '80796220c074856f55a63d4ef576f5b5d45cfa4fdc5701b5c7f384e5d92d72bd'
            '7b8588bb8ac9c2c74bc5b537e3e17ea90c81d8cf70ea2bd8ff48e3583c24d67f'
            'c5cd177455d2f403f1d7d6dc7f9d19925c8b7662f1984ed9eae91af0f49697b2'
            'a2a77d0f37133874b4ee2636569afccebcc1b972833b215cd9c756a4c202ea30')

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

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

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

 apply_git_with_msg 0001-makepkg-clean-up-more-things.patch \
  0002-makepkg-bash-v4-compat.patch \
  0003-makepkg-find_libdepends-port-to-Windows.patch \
  0004-makepkg-build-env-export.patch \
  0005-makepkg-find_libprovides-port-to-Windows.patch \
  0006-fill_progress.patch \
  0007-test-fixes.patch \
  0008-Remove-everything-related-to-sudo-fakeroot-file-perm.patch \
  0009-change-shebang.patch \
  0010-Set-fixed-paths-for-various-tools.patch \
  0011-makepkg-port-staticlibs-to-Windows.patch \
  0012-makepkg-port-strip-debug-to-Windows.patch \
  0013-makepkg-lint_package-Warn-on-package-files-referenci.patch \
  0014-More-debugging-info.patch \
  0015-Core-update.patch \
  0016-Remove-ldconfig.patch \
  0017-Change-the-epoch-separator.patch \
  0018-makepkg-avoid-creating-.tar-files-with-extended-attr.patch \
  0019-pacman-libalpm-ignore-file-conflicts-for-foo.exe-foo.patch \
  0020-Change-default-answer-of-all-queries-to-yes.patch \
  0021-Use-pipe-instead-of-socket.patch \
  0022-pacman-make-file-list-comparisons-between-packages-c.patch \
  0023-Translate-MSYS2-specific-messages-into-Japanese.patch \
  0024-Workaround-compressing-packages-with-bsdtar-under-Ac.patch \
  0025-Fix-asciidoc-argparse.patch \
  0026-Export-CC-and-CXX-variables-explicitly.patch \
  0027-pacman.c-handle-cr-on-stdin-as-well.patch \
  0028-Add-a-CI-job.patch \
  0029-Fix-building-staticlibs-with-LTO.patch \
  0030-Make-lint_pkgbuild-opt-in-via-MAKEPKG_LINT_PKGBUILD-.patch \
  0031-libmakepkg-fix-compatibility-with-bash-5.2-patsub_re.patch \
  0032-pacman-key-pass-allow-weak-key-signatures-during-imp.patch
}

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

  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}-${pkgver}
  meson test -C build-${CARCH}
}

package() {
  cd ${srcdir}/${pkgname}-${pkgver}
  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-msys"
    myflags="-march=pentium4"
  ;;
  x86_64)
    mycarch="x86_64"
    mychost="x86_64-pc-msys"
    myflags="-march=nocona -msahf"
  ;;
  esac

  install -m644 ${srcdir}/makepkg.conf ${pkgdir}/etc/
  install -m644 ${srcdir}/makepkg_mingw.conf ${pkgdir}/etc/
  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"
}
