# Maintainer: Tuxcore <dev@tuxcore.org>

pkgname=tpkg
pkgver=%version%
pkgrel=%release%
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' 'glibc' 'libarchive' 'curl' 'gpgme' 'gettext' 'gawk' 'coreutils' 'gnupg' 'grep')
makedepends=('meson' 'asciidoc' 'doxygen')
checkdepends=('python' 'fakechroot')
optdepends=('perl-locale-gettext: translation support in makepkg-template')
provides=('libalpm.so')
backup=(etc/tpkg.conf
        etc/makepkg.conf)
options=('strip')
source=(https://gitlab.archlinux.org/pacman/pacman/-/releases/v$pkgver/downloads/pacman-$pkgver.tar.xz
        revertme-makepkg-remove-libdepends-and-libprovides.patch::https://gitlab.archlinux.org/pacman/pacman/-/commit/354a300cd26bb1c7e6551473596be5ecced921de.patch
        "$pkgname-fix-msg-unknown-key.patch::https://gitlab.archlinux.org/pacman/pacman/-/commit/6bb95c8856437513ee0ab19226bc090d6fd0fb06.patch"
        "$pkgname-man-gitlab.patch::https://gitlab.archlinux.org/pacman/pacman/-/commit/95f148c2222db608a0d72d5c5577d0c71e7fa199.patch"
        "$pkgname-make-aligned-titles.patch::https://gitlab.archlinux.org/pacman/pacman/-/commit/5e0496260b7d3f9c9fcf2b1c4899e4dbcc20ff03.patch"
        "$pkgname-repo-add-parseopts.patch::https://gitlab.archlinux.org/pacman/pacman/-/commit/0571ee82bff0edbd5ffac2228d4e6ac510b9008e.patch"
        "$pkgname-drop-result-warn.patch::https://gitlab.archlinux.org/pacman/pacman/-/commit/111eed0251238a9d3f90e76d62f2ac01aeccce48.patch"
        "$pkgname-fix-debugedit.patch::https://gitlab.archlinux.org/pacman/pacman/-/commit/bae9594ac1806ce30f2af1de27c49bb101a00d44.patch"
        tpkg.conf
        makepkg.conf)
sha256sums=('5a60ac6e6bf995ba6140c7d038c34448df1f3daa4ae7141d2cad88eeb5f1f9d9'
            'b3bce9d662e189e8e49013b818f255d08494a57e13fc264625f852f087d3def2'
            '94c987046c2ff232fa0d395cddc11644840d767806711e04ef34f876a9baf217'
            '0774d7035e34661f74b673d4b0a94be877bdc0158a555b873ec6bd4e2c936377'
            '7bb64910265ce2590f593cdfd302076e49f67a68f8cc792a9aaac572d36fc842'
            '2bbfe40539513ff5775aaf900644c8985ef618f5df9af856b9d571e2501365b0'
            '160515b741aadc876a67f213029f5f62a51ff072ea4aaeb687bbe614035bf72f'
            '1f4e4cc54332e60c9da2bdabf9a80dc11db466535f1a0be298cbf654f0723721'
            'SKIP'
            'SKIP')

prepare() {
  cd "pacman-$pkgver"

  # 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


    grep -rl PACMAN "$srcdir" | xargs sed -i 's/PACMAN/TPKG/g'
  grep -rl pacman . | xargs sed -i 's/pacman/tpkg/g'
  grep -rl pacnew . | xargs sed -i 's/pacnew/backup/g'
  echo "GREP DONE!"

    find . -mindepth 1 -maxdepth 1 -name "*pacman*" -exec bash -c ' mv $0 ${0/\pacman/tpkg}' {} \;
    find . -mindepth 2 -maxdepth 2 -name "*pacman*" -exec bash -c ' mv $0 ${0/\pacman/tpkg}' {} \;
    find . -mindepth 3 -maxdepth 3 -name "*pacman*" -exec bash -c ' mv $0 ${0/\pacman/tpkg}' {} \;
    find . -mindepth 4 -maxdepth 4 -name "*pacman*" -exec bash -c ' mv $0 ${0/\pacman/tpkg}' {} \;
    find . -mindepth 5 -maxdepth 5 -name "*pacman*" -exec bash -c ' mv $0 ${0/\pacman/tpkg}' {} \;

    find . -mindepth 1 -maxdepth 1 -name "*PACMAN*" -exec bash -c ' mv $0 ${0/\PACMAN/TPKG}' {} \;
    find . -mindepth 2 -maxdepth 2 -name "*PACMAN*" -exec bash -c ' mv $0 ${0/\PACMAN/TPKG}' {} \;
    find . -mindepth 3 -maxdepth 3 -name "*PACMAN*" -exec bash -c ' mv $0 ${0/\PACMAN/TPKG}' {} \;
    find . -mindepth 4 -maxdepth 4 -name "*PACMAN*" -exec bash -c ' mv $0 ${0/\PACMAN/TPKG}' {} \;
    find . -mindepth 5 -maxdepth 5 -name "*PACMAN*" -exec bash -c ' mv $0 ${0/\PACMAN/TPKG}' {} \;
}

build() {
  cd "pacman-$pkgver"

  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 "pacman-$pkgver"

  meson test -C build
}

package() {
  cd "pacman-$pkgver"

  DESTDIR="$pkgdir" meson install -C build

  # install Arch specific stuff
  install -dm755 "$pkgdir/etc"
  install -m644 "$srcdir/tpkg.conf" "$pkgdir/etc/tpkg.conf"
  install -m644 "$srcdir/makepkg.conf" "$pkgdir/etc"

  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-pkg.d-gnupg.socket"
  done
}

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