#!/usr/bin/env bash
# Prints (and, if qrencode is installed, renders as a terminal QR code) the
# onboarding link for the scrot PWA: opening it on a phone saves the device
# token (via app.js's #t= handler) and, once the app is added to the home
# screen, enables "Share to scrot" from the OS share sheet.
set -euo pipefail

: "${SCROT_SERVER:?set SCROT_SERVER, e.g. https://scrot.example.com}"
: "${DEVICE_TOKEN:?set DEVICE_TOKEN}"

url="$SCROT_SERVER/#t=$DEVICE_TOKEN"

echo "$url"
echo

if command -v qrencode >/dev/null 2>&1; then
  qrencode -t ANSIUTF8 "$url"
else
  echo "(install qrencode to render this as a scannable QR code in-terminal)"
fi

cat <<'EOF'

On the phone:
  1. Open the link/QR above — saves the device token, this device is now onboarded.
  2. Add scrot to the home screen (share/menu -> "Add to Home Screen").
  3. Reopen from the home screen icon once — this re-fetches the manifest
     with the token baked into the share target, enabling "Share to scrot"
     from the OS share sheet (photos, browser selection, etc).
EOF
