# Experimental binary package. The Rust executable is built on the designated
# host; both inputs are pinned below by the builder before makepkg is invoked.
pkgname=archipelago
pkgver=1.0.1.@BUILD_ID@
pkgrel=1
pkgdesc='Archipelago Core build, installation and deployment control'
arch=('x86_64')
depends=('bash' 'coreutils' 'util-linux' 'ostree' 'pacman' 'shadow' 'sudo'
         'openssh' 'systemd' 'gptfdisk' 'parted' 'dosfstools' 'e2fsprogs'
          'arch-install-scripts' 'diffutils' 'pyalpm' 'polkit')
options=('!strip' '!debug')
source=('archipelago' 'runtime.tar')
sha256sums=('@BINARY_SHA256@' '@RUNTIME_SHA256@')

package() {
    # Config shipped from the package belongs in vendor paths under /usr; a
    # runtime/etc tree was silently dropped by earlier recipes.
    if [ -d "$srcdir/runtime/etc" ]; then
        printf 'runtime/etc is not packaged: put vendor defaults under runtime/usr (e.g. /usr/lib/systemd)\n' >&2
        exit 1
    fi
    cp -a "$srcdir/runtime/usr" "$pkgdir/"
    find "$pkgdir" -type d -exec chmod 0755 '{}' +
    find "$pkgdir" -type f -exec chmod 0644 '{}' +
    # The blanket 0644 above must not strip staged executables. The console
    # onboarding wrapper is executed by agetty through --login-program; without
    # the execute bit first boot silently falls through to a plain login prompt.
    ( cd "$srcdir/runtime" && find usr/bin -type f -perm -u+x -print ) | while IFS= read -r path; do
        chmod 0755 "$pkgdir/$path"
    done
    install -Dm0755 "$srcdir/archipelago" "$pkgdir/usr/bin/archipelago"
    ln -s archipelago "$pkgdir/usr/bin/archipelagod"
    chown -R 0:0 "$pkgdir"
    install -d "$pkgdir/usr/share/archipelago"
    # Manifest covers every packaged regular file, including itself. No setuid,
    # setgid or writable-by-group/world files are required by this package.
    touch "$pkgdir/usr/share/archipelago/permissions.tsv"
    chmod 0644 "$pkgdir/usr/share/archipelago/permissions.tsv"
    (cd "$pkgdir" && find usr -type f -printf '%m %U %G %p\n' | LC_ALL=C sort) \
        > "$pkgdir/usr/share/archipelago/permissions.tsv"
    # OSTree materializes regular files with epoch mtimes. Match the package's
    # mtree to that representation so strict ALPM checks also pass after deploy.
    find "$pkgdir" -exec touch -h -d @0 '{}' +
}
