#!/bin/sh
# Installs Lumina on the iPhone or iPad it runs on, from Open Minis.
#
# In Open Minis, tap the top-right button, choose Shell Terminal and run:
#
#   wget -qO- https://lumina-executor.pages.dev/install-ios.sh | sh
#
# What it does:
#   1. Downloads siboot (https://github.com/FrizzleM/siboot), pinned to one
#      commit and checked by SHA-256, so a changed or broken copy never runs.
#   2. Runs `siboot developer-mode`, which pairs this device with itself through
#      LocalDevVPN and checks Developer Mode. If it's off, siboot shows its
#      switch in Settings and this script stops: turn it on, then run it again.
#   3. Downloads Lumina.ipa and checks its SHA-256.
#   4. Runs siboot again, which signs Lumina with your own Apple Account and
#      installs it.
#
# siboot handles Apple Account sign-in and uses third-party anisette services.
# Needs iOS 27 or later, Developer Mode, Wi-Fi, LocalDevVPN
# connected and Open Minis 1.16 or later. Running it again renews the app.
#
# If Developer Mode is missing from Settings, add `-s developer-mode` after
# `sh`. That only makes Developer Mode appear there and installs nothing.
set -eu

IPA_URL="https://gitlab.com/api/v4/projects/89453071/packages/generic/lumina-ios/10.60/Lumina.ipa"
IPA_SHA256="7850192d1deffe244a6721c4e6766cb853049be61f5c8fffa6dcaf1c4bd6d14b"
IPA_SIZE=148313895

SIBOOT_COMMIT="117cb3b993d07b63b64fd9fd6652ee36f1d85219"
SIBOOT_SHA256="05d0a2ef07f99caca762efc6a6e4a7a8b1a691186dd1158759bf18755093c760"
SIBOOT_SOURCES="https://raw.githubusercontent.com/FrizzleM/siboot/$SIBOOT_COMMIT/minis/bin/siboot-aarch64
https://fastly.jsdelivr.net/gh/FrizzleM/siboot@$SIBOOT_COMMIT/minis/bin/siboot-aarch64
https://cdn.jsdelivr.net/gh/FrizzleM/siboot@$SIBOOT_COMMIT/minis/bin/siboot-aarch64"

DIR="$HOME/.lumina"
IPA="$DIR/Lumina.ipa"
SIBOOT="$DIR/siboot"

ok() { printf '  \033[1;32m✓\033[0m %s\n' "$1"; }
step() { printf '  \033[1;36m→\033[0m %s\n' "$1"; }
fail() {
  printf '  \033[1;31m✗\033[0m %s\n' "$1" >&2
  exit 1
}

case "$(uname -m)" in
  aarch64 | arm64) ;;
  *) fail "Run this in Open Minis' Shell Terminal on an iPhone or iPad." ;;
esac

sha() { sha256sum "$1" | cut -d ' ' -f 1; }

# A host that is blocked can hang instead of failing, so give up on one that
# sends nothing for 30 seconds.
fetch() {
  if command -v curl > /dev/null 2>&1; then
    if [ -t 2 ]; then
      curl -fL --connect-timeout 20 -y 30 -Y 1 --progress-bar "$1" -o "$2"
    else
      curl -fsSL --connect-timeout 20 -y 30 -Y 1 "$1" -o "$2"
    fi
  else
    wget -q -T 30 -O "$2" "$1"
  fi
}

printf '\n  \033[1mLumina for iOS\033[0m\n\n'
mkdir -p "$DIR"

# Lumina.ipa, kept between runs while it still matches.
get_lumina() {
  if [ -f "$IPA" ] && [ "$(sha "$IPA")" = "$IPA_SHA256" ]; then
    ok "Lumina is already downloaded"
    return
  fi
  step "Downloading Lumina ($((IPA_SIZE / 1000000)) MB)"
  rm -f "$IPA.part"
  fetch "$IPA_URL" "$IPA.part" || fail "Couldn't download Lumina. Check the internet connection and try again."
  [ "$(sha "$IPA.part")" = "$IPA_SHA256" ] || {
    rm -f "$IPA.part"
    fail "The Lumina download didn't match its checksum. Try again in a few minutes."
  }
  mv "$IPA.part" "$IPA"
  ok "Lumina downloaded and checked"
}

# siboot, from the pinned commit through GitHub or its jsDelivr mirrors.
if [ -f "$SIBOOT" ] && [ "$(sha "$SIBOOT")" = "$SIBOOT_SHA256" ]; then
  ok "Installer is ready"
else
  got=
  for url in $SIBOOT_SOURCES; do
    step "Downloading the installer from $(printf '%s' "$url" | cut -d / -f 3)"
    rm -f "$SIBOOT.part"
    if fetch "$url" "$SIBOOT.part" && [ "$(sha "$SIBOOT.part")" = "$SIBOOT_SHA256" ]; then
      got=1
      break
    fi
  done
  [ -n "$got" ] || {
    rm -f "$SIBOOT.part"
    fail "Couldn't download the installer. Where GitHub is blocked, use a VPN that reaches it for this step, then switch back to LocalDevVPN."
  }
  chmod 755 "$SIBOOT.part"
  mv "$SIBOOT.part" "$SIBOOT"
  ok "Installer downloaded and checked"
fi

# Piped into sh, stdin is this script; the installer's prompts need the terminal.
TTY=
if [ ! -t 0 ] && [ -r /dev/tty ] && (: < /dev/tty) 2> /dev/null; then
  TTY=/dev/tty
fi
run() {
  if [ -n "$TTY" ]; then
    "$SIBOOT" "$@" < "$TTY"
  else
    "$SIBOOT" "$@"
  fi
}

# `developer-mode` on its own only asks the device to show Developer Mode.
if [ "${1:-}" = developer-mode ]; then
  step "Showing Developer Mode in Settings. Follow the steps on screen."
  printf '\n'
  run "$@"
  exit
fi

# First, Developer Mode. The installer pairs this device if it hasn't yet, then
# reports whether Developer Mode is on, or shows its switch in Settings.
step "Checking Developer Mode. Follow the steps on screen."
printf '\n'
LOG="$DIR/developer-mode.log"
rm -f "$LOG" "$LOG.status"
{
  run developer-mode "$@" && echo 0 > "$LOG.status" || echo $? > "$LOG.status"
} | tee "$LOG"
status=$(cat "$LOG.status" 2> /dev/null || echo 1)
[ "$status" = 0 ] || exit "$status"
if ! grep -q "Developer Mode is already on" "$LOG"; then
  printf '\n'
  step "Turn on Developer Mode in Settings › Privacy & Security › Developer Mode."
  step "Your device restarts. Unlock it, tap Turn On, then run this command again."
  exit 0
fi

# Developer Mode is on: install Lumina.
printf '\n'
get_lumina
step "Starting the install. Follow the steps on screen."
printf '\n'
run --ipa "$IPA" "$@"
