# Maintainer: Levente Polyak <anthraxx[at]archlinux[dot]org>
# Maintainer: Morten Linderud <foxboron@archlinux.org>

# ALARM: Kevin Mihelich <kevin@archlinuxarm.org>
#  - arch/host/flags for armv7h, and aarch64
#  - pacman.conf: architecture=armv7h/aarch64, added our aur and alarm repos
#  - makepkg.conf: adjusted C/CXX/LDFLAGS
#  - patch to sync filesystem after install/remove
#  - reverts to allow scriplet input on stdin
#  - patch to fix application/gzip change in file 5.37

pkgname=pacman
pkgver=7.0.0.r3.g7736133
pkgrel=1
# use annotated tag and patch level commit from release branch (can be empty for no patches)
_git_tag=7.0.0
_git_patch_level_commit=77361331ae3864c6ea880e715c5864d59336f275
pkgdesc="A library-based package manager with dependency support"
arch=('x86_64')
url="https://www.archlinux.org/pacman/"
license=('GPL-2.0-or-later')
depends=(
  bash
  coreutils
  curl
  gawk
  gettext
  glibc
  gnupg
  gpgme
  grep
  libarchive
  pacman-mirrorlist
  systemd
)
makedepends=(
  asciidoc
  doxygen
  git
  meson
)
checkdepends=(
  fakechroot
  python
)
optdepends=(
  'base-devel: required to use makepkg'
  'perl-locale-gettext: translation support in makepkg-template'
)
provides=('libalpm.so')
backup=(etc/pacman.conf
        etc/makepkg.conf
        etc/makepkg.conf.d/rust.conf)
validpgpkeys=('6645B0A8C7005E78DB1D7864F99FFE0FEAE999BD'  # Allan McRae <allan@archlinux.org>
              'B8151B117037781095514CA7BBDFFC92306B1121') # Andrew Gregory (pacman) <andrew@archlinux.org>
source=("git+https://gitlab.archlinux.org/pacman/pacman.git#tag=v${_git_tag}?signed"
        revertme-makepkg-remove-libdepends-and-libprovides.patch::https://gitlab.archlinux.org/pacman/pacman/-/commit/354a300cd26bb1c7e6551473596be5ecced921de.patch
        0001-Sychronize-filesystem.patch
        0002-Revert-close-stdin-before-running-install-scripts.patch
        0003-Revert-alpm_run_chroot-always-connect-parent2child-p.patch
        pacman.conf
        makepkg.conf
        alpm.sysusers
        rust.conf)
sha256sums=('06d082c3ce6f0811ca728515aa82d69d372800bd3ada99f5c445ef9429b6e3a6'
            'b3bce9d662e189e8e49013b818f255d08494a57e13fc264625f852f087d3def2'
            'e055c072608bac62f6d74ab980b3851ac740ae476847de2f5dd5021fd97613ab'
            'b36c4975286f8dea0fc3c64abf52706c31cb6d6e2c05a4983834b5417e23e802'
            '7ea05a9457f3b6b1edd9dc82f8a1231b0b9048e8c0a7e75fb99abb330a304234'
            'f3d8a177092afdd883e9eb20977807d6a3e9a6f4a3874490dab7c1a1a2adf3f0'
            '1fae8471726bbda9521ce4d54f9fc646751d450cc4c9e6ebcae5eaeaf4909efe'
            'c8760d7ebb6c9817d508c691c67084be251cd9c8811ee1ccf92c1278bad74c1c'
            '3d7579f4fa52ef512dc82187c010f273aa45e6e8349f8fda9839f808c7dae042')

pkgver() {
  cd "$pkgname"
  git describe --abbrev=7 --match 'v*' | sed 's/^v//;s/\([^-]*-g\)/r\1/;s/-/./g'
}

prepare() {
  cd "$pkgname"

  # apply patch level commits on top of annotated tag
  if [[ -n ${_git_patch_level_commit} ]]; then
    if [[ v${_git_tag} != $(git describe --tags --abbrev=0 "${_git_patch_level_commit}") ]]; then
      error "patch level commit ${_git_patch_level_commit} is not a descendant of v${_git_tag}"
      exit 1
    fi
    git rebase "${_git_patch_level_commit}"
  fi

  # handle patches
  local -a patches
  patches=($(printf '%s\n' "${source[@]}" | grep '.patch'))
  patches=("${patches[@]%%::*}")
  patches=("${patches[@]##*/}")

  if (( ${#patches[@]} != 0 )); then
    for patch in "${patches[@]}"; do
      if [[ $patch =~ revertme-* ]]; then
        msg2 "Reverting patch $patch..."
        patch -RNp1 < "../$patch"
      else
        msg2 "Applying patch $patch..."
        patch -Np1 < "../$patch"
      fi
    done
  fi
}

build() {
  cd "$pkgname"

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

  meson compile -C build
}

check() {
  cd "$pkgname"

  meson test -C build
}

package() {
  cd "$pkgname"

  DESTDIR="$pkgdir" meson install -C build

  # install Arch specific stuff
  install -dm755 "$pkgdir/etc"
  install -m644 "$srcdir/pacman.conf" "$pkgdir/etc"
  install -D -m644 "$srcdir/alpm.sysusers" "${pkgdir}"/usr/lib/sysusers.d/alpm.conf
  install -m644 "$srcdir/rust.conf" "$pkgdir/etc/makepkg.conf.d"

  case $CARCH in
    armv7h)
      mycarch="armv7h"
      mychost="armv7l-unknown-linux-gnueabihf"
      myflags="-march=armv7-a -mfloat-abi=hard -mfpu=neon "
      ;;
    aarch64)
      mycarch="aarch64"
      mychost="aarch64-unknown-linux-gnu"
      myflags="-march=armv8-a "
      ;;
  esac

  install -m644 "$srcdir/makepkg.conf" "$pkgdir/etc"
  sed -i "$pkgdir/etc/makepkg.conf" \
    -e "s|@CARCH[@]|$mycarch|g" \
    -e "s|@CHOST[@]|$mychost|g" \
    -e "s|@CARCHFLAGS[@]|$myflags|g"
  sed -i $pkgdir/etc/pacman.conf -e "s|@CARCH[@]|$mycarch|g"

  local wantsdir="$pkgdir/usr/lib/systemd/system/sockets.target.wants"
  install -dm755 "$wantsdir"

  local unit
  for unit in dirmngr gpg-agent gpg-agent-{browser,extra,ssh} keyboxd; do
    ln -s "../${unit}@.socket" "$wantsdir/${unit}@etc-pacman.d-gnupg.socket"
  done
}

# vim: set ts=2 sw=2 et: