# Maintainer: Alexander Baldeck <alex.bldck@gmail.com>

_arch=powerpc64le
_target=${_arch}-unknown-linux-gnu
pkgname=${_target}-pacman
pkgver=2024.10
pkgrel=1
pkgdesc="Pacman configurations for ${_target} to install dependencies for cross-compilation"
arch=(any)
url="https://archlinuxpower.org"
install=pacman.install
depends=(
  ${_target}-gcc
  ${_target}-glibc
)
source=(pacman-arch.sh
        pacman.conf
        fakebase.desc)
backup=(etc/pacman-${_arch}.conf)
sha256sums=('5fd18c74198312560579d900af2b0e52b757b47fb70e4f78e995b29cf0b5ee7a'
            '2fb7c0ecd3005b7657d5c0eeb72d98026acc83d91f52711406c22f0984bd6f66'
            'a36c46d05728a3e7a5806e4bf215b0d3cfa76a66bb9357bc852476b654dcdaf8')

prepare() {
  if [ -d /usr/${_target}/var/lib/pacman ]; then
    echo "Remove existing synced dbs etc from /usr/${_target}/var/lib/pacman/ and try again"
    exit 1
  fi
}

build() {
  sed "s/@@CARCH@@/${_arch}/g" -i pacman-arch.sh
  sed "s/@@CARCH@@/${_arch}/g" -i pacman.conf
  sed "s/@@CHOST_PREFIX@@/\/usr\/${_target}\//g" -i pacman.conf
  sed "s/@@TIMESTAMP@@/$(date "+%s")/g" -i fakebase.desc
  sed "s/@@FAKEBASE_VERSION@@/${pkgver}-${pkgrel}/g" -i fakebase.desc
}

package() {
  install -d -m755 ${pkgdir}/usr/bin
  install -m755 ${srcdir}/pacman-arch.sh ${pkgdir}/usr/bin/pacman-${_arch}

  install -d -m 755 ${pkgdir}/etc
  install -m644 ${srcdir}/pacman.conf ${pkgdir}/etc/pacman-${_arch}.conf

  # fake that we install a few basic packages via pacman
  install -d -m 755 ${pkgdir}/usr/${_target}/var/lib/pacman/local/fakebase-${pkgver}-${pkgrel}

  pushd /usr/${_target}/
  shopt -s dotglob globstar
  printf '%s\0' **/* | LANG=C bsdtar -cnf - --format=mtree --options='!all,use-set,type,uid,gid,mode,time,size,md5,sha256,link' --null --files-from - --exclude .MTREE | gzip -c -f -n > ${pkgdir}/usr/${_target}/var/lib/pacman/local/fakebase-${pkgver}-${pkgrel}/mtree
  shopt -u dotglob globstar
 
  echo '%FILES%' > ${pkgdir}/usr/${_target}/var/lib/pacman/local/fakebase-${pkgver}-${pkgrel}/files
  find | sed 's@^./@@g' | grep -v '^\.$' | grep -v '.*mtree$' >> ${pkgdir}/usr/${_target}/var/lib/pacman/local/fakebase-${pkgver}-${pkgrel}/files
  echo 9 > ${pkgdir}/usr/${_target}/var/lib/pacman/local/ALPM_DB_VERSION
  install -m644 ${srcdir}/fakebase.desc ${pkgdir}/usr/${_target}/var/lib/pacman/local/fakebase-${pkgver}-${pkgrel}/desc

  popd
}
