#!/bin/sh
# install-apk.sh — One-shot installer for `apk` + bar-install on a rooted BB10.
#
# What it does:
#   1. Fetches the apk binary + ldqnx.so.2 + libstdc++.so.6 + bar-install +
#      signing pubkey from $BB10_APK_BASE (default https://bb10repo.whytofly.de/).
#   2. Lays them out at /accounts/1000/shared/misc/apk/ (canonical location;
#      the apk binary's INTERP path is baked to this location at link time).
#   3. Writes etc/apk/{config,repositories,arch}, initializes lib/apk/db/.
#   4. chowns to devuser:1000_shared and chmods so it's read+exec for the
#      shared perimeter and read/write for devuser.
#   5. Adds `export APK_CONFIG=...` to /accounts/devuser/.profile so `apk`
#      just works in Term49 (though apk also falls back to this path by
#      default; the export is belt-and-suspenders).
#   6. Runs `apk add bb10-shell-shim bb10-apkui` to bring in the GUI.
#
# Requirements: must be run as root (via autoroot's getroot, the old root
# method, or SSH-as-root from a host). The script is self-contained — fetches
# everything it needs over HTTPS.
#
# Usage on the device:
#   curl -fsSL https://bb10repo.whytofly.de/install-apk.sh | sh
#
# Or with a custom base URL:
#   BB10_APK_BASE=http://host:8765 curl -fsSL "$BB10_APK_BASE/install-apk.sh" | sh

set -eu

BASE="${BB10_APK_BASE:-https://bb10repo.whytofly.de}"
APK_HOME=/accounts/1000/shared/misc/apk

# Pick a curl that works (BerryCore's is usually present + linked correctly)
CURL=""
for c in /accounts/devuser/berrycore/bin/curl /base/usr/bin/curl curl; do
    if command -v "$c" >/dev/null 2>&1 || [ -x "$c" ]; then CURL="$c"; break; fi
done
if [ -z "$CURL" ]; then
    echo "ERROR: no curl found. Install BerryCore first." >&2
    exit 1
fi

uid=$(id -u 2>/dev/null || echo 0)
if [ "$uid" != "0" ]; then
    echo "ERROR: must run as root (try ssh root@127.0.0.1 with the autoroot key)" >&2
    exit 1
fi

echo "==> Fetching binaries from $BASE/binaries/ ..."
mkdir -p "$APK_HOME/etc/apk/keys" "$APK_HOME/lib/apk/db" \
         "$APK_HOME/var/cache/apk" "$APK_HOME/var/log" \
         "$APK_HOME/var/lib/bb10-bars"
for f in apk ldqnx.so.2 libstdc++.so.6 bar-install; do
    "$CURL" -fsSL "$BASE/binaries/$f" -o "$APK_HOME/$f"
    chmod 755 "$APK_HOME/$f"
done

echo "==> Fetching repo signing key ..."
"$CURL" -fsSL "$BASE/keys/bb10-test.rsa.pub" \
        -o "$APK_HOME/etc/apk/keys/bb10-test.rsa.pub"

echo "==> Writing config ..."
cat > "$APK_HOME/etc/apk/config" <<EOF
root $APK_HOME
force-no-chroot
allow-untrusted
EOF
echo armhf > "$APK_HOME/etc/apk/arch"
echo "$BASE/repo" > "$APK_HOME/etc/apk/repositories"
: > "$APK_HOME/etc/apk/world"
: > "$APK_HOME/lib/apk/db/installed"
: > "$APK_HOME/lib/apk/db/lock"
: > "$APK_HOME/lib/apk/db/triggers"

echo "==> Setting permissions ..."
chown -R devuser:1000_shared "$APK_HOME"
chmod -R u=rwX,g=rwX,o=rX "$APK_HOME"
chmod 755 "$APK_HOME/apk" "$APK_HOME/bar-install"

# Also drop a symlink so users can type `apk` and `bar-install` from any shell.
ln -sf "$APK_HOME/apk" /accounts/devuser/bin/apk
ln -sf "$APK_HOME/bar-install" /accounts/devuser/bin/bar-install

# Belt-and-suspenders: export APK_CONFIG in the shell profile.
PROFILE=/accounts/devuser/.profile
if ! grep -q '^export APK_CONFIG=' "$PROFILE" 2>/dev/null; then
    echo "export APK_CONFIG=$APK_HOME/etc/apk/config" >> "$PROFILE"
fi

echo "==> apk database bootstrap ..."
"$APK_HOME/apk" update 2>&1 | sed 's/^/    /' || true

echo "==> Installing the apk_ui GUI (bb10-apkui) ..."
"$APK_HOME/apk" add bb10-shell-shim bb10-apkui 2>&1 | sed 's/^/    /' || true

echo
echo "Done. Quick sanity check:"
"$APK_HOME/apk" info
echo
echo "Open 'BB10 Packages' on the homescreen, or run \`apk\` in Term49."
