# Maintainer (Arch): 
# Contributor (Hyperbola): André Silva <emulatorman@hyperbola.info>
# Contributor (Hyperbola): Márcio Silva <coadde@hyperbola.info>
# Maintainer: Luke Shumaker <lukeshu@parabola.nu>
# Contributor: Omar Vega Ramos <ovruni@gnu.org.pe>
# Contributor: Nicolás Reynolds <fauno@kiwwwi.com.ar>
# Contributor: Daniel Milewski <niitotantei@riseup.net>

pkgname=pacman
pkgver=6.0.0
pkgrel=3
pkgrel+=.parabola1
pkgdesc="A library-based package manager with dependency support"
arch=('x86_64')
arch+=('i686' 'armv7h')
url="https://www.archlinux.org/pacman/"
license=('GPL')
groups=('base-devel')
depends=('bash' 'glibc' 'libarchive' 'curl'
         'gpgme' 'pacman-mirrorlist' 'archlinux-keyring')
depends+=('parabola-keyring' 'archlinuxarm-keyring' 'archlinux32-keyring')
makedepends=('meson' 'asciidoc' 'doxygen')
checkdepends=('python' 'fakechroot')
optdepends=('perl-locale-gettext: translation support in makepkg-template')
provides=('libalpm.so')
provides+=('pacman-parabola')
conflicts=('pacman-parabola')
replaces=('pacman-parabola')
backup=(etc/pacman.conf
        etc/makepkg.conf)
options=('strip' 'debug')
validpgpkeys=('6645B0A8C7005E78DB1D7864F99FFE0FEAE999BD'  # Allan McRae <allan@archlinux.org>
              'B8151B117037781095514CA7BBDFFC92306B1121') # Andrew Gregory (pacman) <andrew@archlinux.org>
source=(https://sources.archlinux.org/other/pacman/$pkgname-$pkgver.tar.xz{,.sig}
        pacman-6.0.0-fix-404-download.patch::https://git.archlinux.org/pacman.git/patch/?id=3401f9e142ac4c701cd98c52618cb13164f2146b
        pacman-6.0.0-fix-key-import-double-free.patch::https://git.archlinux.org/pacman.git/patch/?id=542910d684191eb7f25ddc5d3d8fe3060028a267
        "${arch[@]/#/pacman.conf.}"
        makepkg.conf.in
        pacman-keyring.service
        pacman-keyring.timer
        0004-makepkg-Treat-pkgrel-more-similarly-to-pkgver.patch)
source_armv7h=(0001-Sychronize-filesystem.patch
               0002-Revert-close-stdin-before-running-install-scripts.patch
               0003-Revert-alpm_run_chroot-always-connect-parent2child-p.patch)
sha256sums=('004448085a7747bdc7a0a4dd5d1fb7556c6b890111a06e029ab088f9905d4808'
            'SKIP'
            'fe7e037e1b84bfa5bc401650d148c2a2e87d827705a6ec18a596ff5eea3cc0fd'
            'a51b57dd47818d97e29ac0b8604b4b409916bbe6029adfbb03ac7e7c2b2a0819'
            '29815f65c9dc402cd22651ae1b24def9a6b1525554b348b28a398530a79a1092'
            '4a4198d6269a8a6b76edf66df4185c599f0b12e143fb7cc9a3497cc78f5e33f8'
            'a7bc662bda8e5707eddc2b8891fd756afed4aeea76e5a0145ecba128a8210d7f'
            '20e9de1b612c3f24736e76a753cd38b7ea0f80160185012a538a77297acfb31f'
            '220f1b25a64727041dc6fa3fd486b0a043f735a3f6cecedc4e2f7c47ec6ce66d'
            '2a857061f032ff5485f5c75ab74e6f6532621e08963ef48640a792cca16cacd6'
            '9ccc7ef5bd27a68d8788f10c6e5b36495c5d9038d4eb160f9ea4dc9901b622d8')
sha256sums_armv7h=('8d70fb5094f58aad98b601bbc42be354c2014b9fe734a1ee0b1e14bb041cc9cc'
                   '0e771370da68c855bfb4eaad4c2ae137883a474886a049b934dac2e775574cb9'
                   '2f586f72c34150330389854575a21be1d3ef3637c4f94bec2e948c2717a5aecb')

prepare() {
  cd "$pkgname-$pkgver"

  patch -p1 -i "$srcdir"/pacman-6.0.0-fix-404-download.patch
  patch -p1 -i "$srcdir"/pacman-6.0.0-fix-key-import-double-free.patch

  # From Arch ARM
  if [ "${CARCH}" = "armv7h" ]; then
    patch -p1 -i ../0001-Sychronize-filesystem.patch
    patch -p1 -i ../0002-Revert-close-stdin-before-running-install-scripts.patch
    patch -p1 -i ../0003-Revert-alpm_run_chroot-always-connect-parent2child-p.patch
  fi

  # From Parabola
  patch -p1 -i ../0004-makepkg-Treat-pkgrel-more-similarly-to-pkgver.patch
}

build() {
  cd "$pkgname-$pkgver"

  meson --prefix=/usr \
        --buildtype=plain \
        -Ddoc=enabled \
        -Ddoxygen=enabled \
        -Dscriptlet-shell=/usr/bin/bash \
        -Dldconfig=/usr/bin/ldconfig \
        build

  meson compile -C build

  # Generate the architecture-specific makepkg.conf files
  local carch mycarch mychost myflags myldflags
  for carch in "${arch[@]}"; do
    case $carch in
      i686)
        mycarch="i686"
        mychost="i686-pc-linux-gnu"
        myflags="-march=i686 -mtune=generic -O2 -pipe -fno-plt -fexceptions \
                 -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security \
                 -fstack-clash-protection -fcf-protection"
        ;;
      x86_64)
        mycarch="x86_64"
        mychost="x86_64-pc-linux-gnu"
        myflags="-march=x86-64 -mtune=generic -O2 -pipe -fno-plt -fexceptions \
                 -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security \
                 -fstack-clash-protection -fcf-protection"
        ;;
      armv7h)
        mycarch="armv7h"
        mychost="armv7l-unknown-linux-gnueabihf"
        myflags="-march=armv7-a -mfloat-abi=hard -mfpu=vfpv3-d16 -O2 -pipe -fno-plt -fexceptions \
                 -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security \
                 -fstack-clash-protection"
        ;;
    esac
    myldflags="-Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now"

    # set things correctly in the default conf file
    sed < "$srcdir/makepkg.conf.in" > "$srcdir/makepkg.conf.$carch" \
        -e "s|@CARCH[@]|$mycarch|g" \
        -e "s|@CHOST[@]|$mychost|g" \
        -e "s|@CARCHFLAGS[@]|$myflags|g" \
        -e "s|@LDFLAGS[@]|$myldflags|g"
  done
}

check() {
  cd "$pkgname-$pkgver"

  meson test -C build
}

package() {
  cd "$pkgname-$pkgver"

  DESTDIR="$pkgdir" meson install -C build

  # install distro-specific stuff
  install -dm755 "$pkgdir"/etc/{makepkg,pacman}.d
  install -m644 "$srcdir/pacman.conf.$CARCH" "$pkgdir/etc/pacman.conf"
  install -m644 "$srcdir/makepkg.conf.$CARCH" "$pkgdir/etc/makepkg.conf"
  echo '# There must be at least 1 file matching /etc/pacman.d/*.conf' > "$pkgdir/etc/pacman.d/empty.conf"

  install -dm755 "$pkgdir/usr/share/pacman/defaults"
  local carch
  for carch in "${arch[@]}"; do
    install -m644 "$srcdir/pacman.conf.$carch" "$pkgdir/usr/share/pacman/defaults/"
    install -m644 "$srcdir/makepkg.conf.$carch" "$pkgdir/usr/share/pacman/defaults/"
  done

  # Parabola's pacman began shipping a weekly job for this back in
  # 2014.  Why is it nescessary for Parabola installs, but it
  # seemingly isn't for Arch installs?
  install -Dm644 "$srcdir/pacman-keyring.service" "$pkgdir/usr/lib/systemd/system/pacman-keyring.service"
  install -Dm644 "$srcdir/pacman-keyring.timer" "$pkgdir/usr/lib/systemd/system/pacman-keyring.timer"
  install -Dm644 /dev/stdin "$pkgdir/usr/lib/systemd/system-preset/90-pacman.preset" <<<"enable pacman-keyring.timer"
}
