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

pkgname=pacman
pkgver=6.0.1
pkgrel=1
pkgdesc="A library-based package manager with dependency support (MSYS2 port)"
arch=('i686' 'x86_64')
url="https://www.archlinux.org/pacman/"
license=('GPL')
groups=('base-devel')
depends=('bash>=4.2.045'
         'gettext'
         'gnupg'
         'curl'
         'pacman-mirrors'
         'msys2-keyring'
         'which'
         'bzip2'
         'xz'
         'zstd')
checkdepends=('python')
makedepends=('asciidoc'
             'doxygen'
             'git'
             '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="0a6fecd07271a54d9009ea7204c0e6288a44212b"
source=(pacman-${pkgver}::git+https://gitlab.archlinux.org/pacman/pacman.git#commit=${_commit}
        "pacman.conf"
        "makepkg.conf"
        "makepkg_mingw.conf"
        "makepkg-mingw"
        0001-Msysize.patch
        0002-More-debugging-info.patch
        0003-Core-update.patch
        0004-Remove-ldconfig.patch
        0005-Change-the-epoch-separator.patch
        0006-makepkg-avoid-creating-.tar-files-with-extended-attr.patch
        0007-pacman-libalpm-ignore-file-conflicts-for-foo.exe-foo.patch
        0008-Change-default-answer-of-all-queries-to-yes.patch
        0009-Use-pipe-instead-of-socket.patch
        0010-pacman-make-file-list-comparisons-between-packages-c.patch
        0011-Translate-MSYS2-specific-messages-into-Japanese.patch
        0012-Workaround-compressing-packages-with-bsdtar-under-Ac.patch
        0013-Fix-asciidoc-argparse.patch
        0014-Remove-fakeroot.patch
        0015-Remove-sudo.patch
        0016-Use-msys-tools.patch
        0017-Export-CC-and-CXX-variables-explicitly.patch
        0018-pacman.c-handle-cr-on-stdin-as-well.patch
        0019-Add-a-CI-job.patch)
validpgpkeys=('6645B0A8C7005E78DB1D7864F99FFE0FEAE999BD'  # Allan McRae <allan@archlinux.org>
              'B8151B117037781095514CA7BBDFFC92306B1121') # Andrew Gregory (pacman) <andrew@archlinux.org>
sha256sums=('SKIP'
            'fff047d9514c4810eee9ab798b51d3300cbf7ee1b5e2e494e1350499d28dd448'
            'ee9f8a5ec60a1334725adb102f531f38badfe1bb66bde82c4aeb3131a2becc2f'
            '606e4b2808a40e856b7043244fc993d426dab25bc2e03b2b8ee5b869f5102507'
            '2bd27c3fc5443b367e5025c9b9a35670b02202e48e92eead90755fef8d08fa83'
            '57fd01cd215294e2a9c91b616fe3854e96acddfb3de6a758912d9d8ea61fdc18'
            '9c1b99443d26732b21aec3943c17678aa9c7add584b413476873dafb1de4a9c0'
            'b1f3ade9e06f2cdfc23ae09a8598ae3c5603182c13d53d664af8a06eed74590b'
            '3379e1c85bfc6a5cc23f13e62354f9542679ab3d99c88fd6065456302b879613'
            '7391a51cf2fd3061acb839e5b08ca9a3ba9bfb18f5b9aba529c329a0c38a4b63'
            '2467f8e15bc8e9aa7cd7cdaad25d543760bb54c4698586e34db2a20c51e38e83'
            '0ff33efa51e92f7e6b095f19cccaa94ddd47e7639e93e2f43fffbd8b83eab5de'
            'ccc0b5d6a6c0bd716d5810916f5539f47517ebb305c70cf04c3b380607bd46dc'
            '3ad5dd5cfff806d8548062bb3e6cd62ebff82631743106d3792f631a96c18820'
            '57dfe16e07ab4ad927906a8b7a86ada92cc11473e1839791cbfbd491a7030dc7'
            '9fdaa03800ed3be96e6191b765fa54660735f92e1637cf9696b78fe4d552ef0b'
            '0c21c0c675a2fa5582f32b3a96d7f9f828049c23b09a52f2f2a8432986554f26'
            '85d279412e923a575ec07f02fff5f961afb44bd89f7864c7ecc3d73a592df4d8'
            '43b9d741a4624e0dbe3b41fa8523789cbd483b5751d738ffaf04c9f3f51d819c'
            'bfd2235372bdf013254f0dbadd7ab398fc118ce0f7b6d50d4001c014366b65f9'
            '48a0b3b9e5206cc46b7092f2736da2493194a4ce44a154ba8f7740d690ccedb2'
            '5029415842e9c51dd8aa6e380636316cf3f8f2b3ef0ae3f9430658213cb56a2a'
            '0ac2184c8419041f2948517a1c8b66ed740cf0bb1ac5856b384008495317e87b'
            '17604e359cbc1666b678359a5f46b5f822ca0d3a62c6c672b74d45399d78ab5e')

apply_git_am_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    git am --committer-date-is-author-date "${srcdir}/${_patch}"
  done
}

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

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

 apply_git_am_with_msg 0001-Msysize.patch \
  0002-More-debugging-info.patch \
  0003-Core-update.patch \
  0004-Remove-ldconfig.patch \
  0005-Change-the-epoch-separator.patch \
  0006-makepkg-avoid-creating-.tar-files-with-extended-attr.patch \
  0007-pacman-libalpm-ignore-file-conflicts-for-foo.exe-foo.patch \
  0008-Change-default-answer-of-all-queries-to-yes.patch \
  0009-Use-pipe-instead-of-socket.patch \
  0010-pacman-make-file-list-comparisons-between-packages-c.patch \
  0011-Translate-MSYS2-specific-messages-into-Japanese.patch \
  0012-Workaround-compressing-packages-with-bsdtar-under-Ac.patch \
  0013-Fix-asciidoc-argparse.patch \
  0014-Remove-fakeroot.patch \
  0015-Remove-sudo.patch \
  0016-Use-msys-tools.patch \
  0017-Export-CC-and-CXX-variables-explicitly.patch \
  0018-pacman.c-handle-cr-on-stdin-as-well.patch \
  0019-Add-a-CI-job.patch
}

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

  LDFLAGS+=" -static-libgcc" \
  meson 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

  case "${CARCH}" in
  i686)
    mycarch="i686"
    mychost="i686-pc-msys"
    myflags="-march=i686"
  ;;
  x86_64)
    mycarch="x86_64"
    mychost="x86_64-pc-msys"
    myflags="-march=x86-64"
  ;;
  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"
}
