# Based on the AUR openclaw package:
# Maintainer: Ralph Torres <mail at ralphptorr dot es>
# Contributor: Evert Vorster <evorster@gmail.com>
# Contributor: Emir-Eins <emir-eins@outlook.com>
# Contributor: Wuxxin <wuxxin@gmail.com>
# Followed here straight from the npm registry (see .omarchy/package.json), since
# upstream's release cadence outruns the AUR.

pkgname=openclaw
pkgver=2026.9.6
pkgrel=3
pkgdesc='Multi-channel AI gateway with extensible messaging integrations'
arch=(x86_64 aarch64)
url=https://github.com/openclaw/openclaw
license=(MIT)

depends=('nodejs>=22')
makedepends=(npm)
optdepends=(
    '1password-cli: 1password skill'
    'curl: weather, openai-whisper-api skills'
    'ffmpeg: video-frames skill'
    'github-cli: github, gh-issues skills'
    'jq: session-logs, trello skills'
    'python-openai-whisper: openai-whisper skill'
    'ripgrep: session-logs skill'
    'tmux: tmux skill'
    'uv: nano-pdf skill'
    'go: for installing skill tools not packaged for Arch'
)
source=($pkgname-$pkgver.tgz::https://registry.npmjs.org/$pkgname/-/$pkgname-$pkgver.tgz)
sha256sums=('1a7355691bc0e605222ba818f1f72c1787253c78dfeb0df6be2086ec73b71e63')
options=(!debug !strip)
install=$pkgname.install
noextract=($pkgname-$pkgver.tgz)

package() {
    export SHARP_IGNORE_GLOBAL_LIBVIPS=1
    # npm 12 blocks install-time lifecycle scripts unless the package is
    # allow-listed, and for a local tarball the allow-list key is the tarball's
    # own file: spec, not the package name (arborist matches node.resolved).
    # Upstream's postinstall prunes stale dist files and clears the
    # .openclaw-lifecycle-pending marker the tarball ships with; without it,
    # every openclaw invocation tries to finish that lifecycle itself and dies
    # with EACCES on the root-owned install.
    # That postinstall also migrates legacy state under whatever home it sees
    # (pruning plugin-runtime-deps in ~/.openclaw and friends), so it gets a
    # scratch home: a maintainer's own OpenClaw is not the build's to touch.
    mkdir -p "$srcdir"/home
    env -u OPENCLAW_HOME -u OPENCLAW_STATE_DIR -u OPENCLAW_CONFIG_PATH HOME="$srcdir"/home \
        npm install --silent --global --cache "$srcdir"/npm-cache \
        --allow-scripts="file:$srcdir/$pkgname-$pkgver.tgz" \
        --prefix "$pkgdir"/usr "$srcdir"/$pkgname-$pkgver.tgz
    if [[ -e "$pkgdir"/usr/lib/node_modules/$pkgname/.openclaw-lifecycle-pending ]]; then
        error "openclaw's postinstall did not run; the package would fail on first use"
        return 1
    fi

    cat > $pkgname <<'EOF'
#!/bin/sh
export SHARP_IGNORE_GLOBAL_LIBVIPS=1
exec node /usr/lib/node_modules/openclaw/openclaw.mjs "$@"
EOF
    install -Dm755 -t "$pkgdir"/usr/bin $pkgname

    cd "$pkgdir"/usr/lib/node_modules/$pkgname
    install -Dm644 -t "$pkgdir"/usr/share/licenses/$pkgname LICENSE
    install -Dm644 -t "$pkgdir"/usr/share/doc/$pkgname README.md CHANGELOG.md
    for f in docs/*
    do ln -s /usr/lib/node_modules/$pkgname/"$f" "$pkgdir"/usr/share/doc/$pkgname/
    done
}
