#!/bin/sh
# bin/install-app — download the BaoCut App pinned by app-release.json, verify
# its SHA-256 and open the installer. Nothing is installed unattended: no copy
# into /Applications, no sudo. Tell the user and get their consent before you
# run it; when it exits, ask them to finish the install, then rerun bin/connect.
#
# Options: --dir <download dir> (default: ~/Downloads), --json.
# Exit codes: 0 opened; 2 unsupported platform / bad pin; 3 download or checksum failed.
set -eu

skill_root=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd -P)
app_release="$skill_root/app-release.json"
dir="$HOME/Downloads"
json=0
while [ $# -gt 0 ]; do
    case "$1" in
        --dir) dir=${2:-}; shift 2 ;;
        --dir=*) dir=${1#--dir=}; shift ;;
        --json) json=1; shift ;;
        -h|--help) sed -n '2,8p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;;
        *) echo "install-app: unknown argument $1" >&2; exit 2 ;;
    esac
done

case "$(uname -s)-$(uname -m)" in
    Darwin-arm64) target=aarch64-apple-darwin ;;
    *)
        echo "install-app: no BaoCut App is published for $(uname -s)/$(uname -m); on Linux install the standalone bcut CLI instead (see references/hosts.md)." >&2
        exit 2 ;;
esac

block=$(sed -n "/\"$target\"/,/}/p" "$app_release")
url=$(printf '%s\n' "$block" | sed -n 's/^[[:space:]]*"url"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' | head -n 1)
sha=$(printf '%s\n' "$block" | sed -n 's/^[[:space:]]*"sha256"[[:space:]]*:[[:space:]]*"\([0-9a-fA-F]*\)".*/\1/p' | head -n 1)
version=$(sed -n 's/^[[:space:]]*"appVersion"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' "$app_release" | head -n 1)
if [ -z "$url" ]; then
    echo "install-app: app-release.json has no download for $target" >&2
    exit 2
fi
if [ -z "$sha" ] && [ "${BAOCUT_INSTALL_UNVERIFIED:-0}" != 1 ]; then
    echo "install-app: app-release.json pins no SHA-256 for $target (this is a source checkout, not a released skill); refusing to install an unverified package. Download BaoCut $version manually from $url or set BAOCUT_INSTALL_UNVERIFIED=1." >&2
    exit 2
fi

mkdir -p "$dir"
file="$dir/$(basename -- "$url")"
echo "install-app: downloading BaoCut $version → $file" >&2
if ! curl -fL --retry 2 --connect-timeout 15 -o "$file" "$url"; then
    echo "install-app: download failed: $url" >&2
    exit 3
fi
if [ -n "$sha" ]; then
    actual=$(shasum -a 256 "$file" | cut -d' ' -f1)
    if [ "$actual" != "$sha" ]; then
        echo "install-app: SHA-256 mismatch for $file (expected $sha, got $actual); deleting it." >&2
        rm -f "$file"
        exit 3
    fi
fi

# A .zip unpacks to BaoCut.app next to itself; a .dmg opens as a volume. Either
# way the user drags the App into Applications themselves.
case "$file" in
    *.zip)
        unpacked="$dir/BaoCut.app"
        rm -rf "$unpacked"
        ditto -x -k "$file" "$dir"
        open -R "$unpacked" 2>/dev/null || open "$dir"
        opened=$unpacked ;;
    *) open "$file"; opened=$file ;;
esac
if [ "$json" = 1 ]; then
    printf '{"ok":true,"appVersion":"%s","file":"%s","opened":"%s","next":"Move BaoCut.app into /Applications, launch it once, then rerun bin/connect."}\n' "$version" "$file" "$opened"
else
    echo "install-app: BaoCut $version is at $opened. Move it into /Applications, launch it once, then rerun bin/connect." >&2
fi
exit 0
