# 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.tuxcore.org/"
license=('GPL-2.0-or-later')
depends=('bash' 'glibc' 'libarchive' 'curl' 'gpgme' 'gettext' 'gawk' 'coreutils' 'gnupg' 'grep' 'systemd')
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
        etc/makepkg.conf.d/rust.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
        tpkg.conf
        makepkg.conf
        alpm.sysusers
        rust.conf)
sha256sums=('SKIP'
            'b3bce9d662e189e8e49013b818f255d08494a57e13fc264625f852f087d3def2'
            'SKIP'
            'SKIP'
            '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 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 specific stuff
  install -dm755 "$pkgdir/etc"
  install -m644 "$srcdir/tpkg.conf" "$pkgdir/etc/tpkg.conf"
  install -m644 "$srcdir/makepkg.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"

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

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