#!/bin/sh
set -eu

start_ui=1
case "${1:-}" in
  "") ;;
  --no-start) start_ui=0 ;;
  --help|-h)
    echo "Usage: install.sh [--no-start]"
    echo "  --no-start  Stage the graphical app without opening it (automation only)."
    exit 0
    ;;
  *)
    echo "unknown option: $1" >&2
    exit 2
    ;;
esac

base_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
xdg_data_home=${XDG_DATA_HOME:-"$HOME/.local/share"}
xdg_state_home=${XDG_STATE_HOME:-"$HOME/.local/state"}
install_parent="$xdg_data_home/platformless/apps"
install_root="$install_parent/com.platformless.button-game"
platformless_data_dir="$xdg_data_home/platformless"
state_file="$platformless_data_dir/state.json"
tmp_root="$install_root.tmp.$$"
backup_root="$install_root.previous.$$"
installer_log="$xdg_state_home/platformless/logs/com.platformless.button-game-install.log"
desktop_dir="$xdg_data_home/applications"
icon_dir="$xdg_data_home/icons/hicolor/256x256/apps"
runtime_swapped=0
runtime_committed=0

if [ "$start_ui" -eq 1 ] && [ -z "${DISPLAY:-}${WAYLAND_DISPLAY:-}" ]; then
  echo "Button Game uses a graphical installer, but no graphical session is available." >&2
  echo "Run install.sh from a terminal inside your desktop session." >&2
  echo "For explicit automation only, use: install.sh --no-start" >&2
  exit 1
fi

cleanup() {
  rm -rf "$tmp_root"
  if [ "$runtime_swapped" -eq 1 ] && [ "$runtime_committed" -eq 0 ]; then
    rm -rf "$install_root"
    if [ -d "$backup_root" ]; then
      mv "$backup_root" "$install_root"
    fi
  fi
}
trap cleanup EXIT

desktop_exec_quote() {
  printf '%s' "$1" | sed 's/\\/\\\\/g; s/"/\\"/g; s/`/\\`/g; s/\$/\\$/g; s/^/"/; s/$/"/'
}

case "$xdg_data_home" in
  *'
'*)
    echo "XDG_DATA_HOME must not contain a newline" >&2
    exit 1
    ;;
esac

mkdir -p "$install_parent" "$desktop_dir" "$icon_dir" "$platformless_data_dir/cache" "$platformless_data_dir/games" "$(dirname "$installer_log")"
if ! command -v sha256sum >/dev/null 2>&1; then
  echo "sha256sum is required to verify the Button Game installer" >&2
  echo "Installer log: $installer_log" >&2
  exit 1
fi
printf 'Preparing the Button Game graphical installer.\n' > "$installer_log"
printf 'Preparing the Button Game graphical installer…\n'
printf '[1/3] Verifying application files…\n'
rm -rf "$tmp_root"
mkdir -p "$tmp_root"
cp -R "$base_dir/files/app/." "$tmp_root/"
chmod +x "$tmp_root/bin/launcher-core"
(cd "$tmp_root" && sha256sum -c "$base_dir/bundle-manifest.sha256" >/dev/null)
platform_dir="$tmp_root/platform"
mkdir -p "$platform_dir/launcher-versions/button-game-single-20260719075853"
cp "$tmp_root/bin/launcher-core" "$platform_dir/launcher-versions/button-game-single-20260719075853/launcher-core"
chmod +x "$platform_dir/launcher-versions/button-game-single-20260719075853/launcher-core"
if [ -f "$tmp_root/bin/launcher-core.bin" ]; then
  cp "$tmp_root/bin/launcher-core.bin" "$platform_dir/launcher-versions/button-game-single-20260719075853/launcher-core.bin"
  chmod +x "$platform_dir/launcher-versions/button-game-single-20260719075853/launcher-core.bin"
fi
cat > "$platform_dir/active-launcher.json" <<'JSON'
{"schema":1,"active_version":"button-game-single-20260719075853","executable_path":"launcher-versions/button-game-single-20260719075853/launcher-core"}
JSON
cat > "$tmp_root/platformless-launcher" <<'EOF'
#!/bin/sh
set -eu
script_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
exec "$script_dir/bin/launcher-stub" run-core --platform-dir "$script_dir/platform" -- "$@"
EOF
chmod +x "$tmp_root/platformless-launcher"
cat > "$tmp_root/platformless-game-launcher" <<'EOF'
#!/bin/sh
set -eu
script_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
exec "$script_dir/bin/launcher-stub" launch-plan --plan "$script_dir/install-plan.json" -- "$@"
EOF
chmod +x "$tmp_root/platformless-game-launcher"
printf '[2/3] Staging the graphical application…\n'
rm -rf "$backup_root"
if [ -d "$install_root" ]; then
  mv "$install_root" "$backup_root"
fi
mv "$tmp_root" "$install_root"
runtime_swapped=1
cp "$base_dir/uninstall.sh" "$install_root/uninstall.sh"
chmod +x "$install_root/uninstall.sh"

launcher_args="--ui --state $state_file --data-dir $platformless_data_dir"
if [ -f "$base_dir/install-plan.template.json" ]; then
  "$install_root/bin/launcher-core" render-install-plan \
    --template "$base_dir/install-plan.template.json" \
    --output "$install_root/install-plan.json" \
    --install-root "$install_root" \
    --data-dir "$platformless_data_dir" \
    --state-file "$state_file"
  launcher_args="$launcher_args --install-plan $install_root/install-plan.json"
fi

if [ ! -f "$install_root/install-plan.json" ]; then
  echo "This installer does not contain a Button Game install plan." >&2
  echo "Installer log: $installer_log" >&2
  exit 1
fi

printf '[3/3] Registering the application shortcut…\n'
cp "$base_dir/files/share/icons/hicolor/256x256/apps/com.platformless.button-game.png" "$icon_dir/com.platformless.button-game.png"
game_desktop_exec=$(desktop_exec_quote "$install_root/platformless-game-launcher")
launcher_desktop_exec=$(desktop_exec_quote "$install_root/platformless-launcher")
state_desktop_arg=$(desktop_exec_quote "$state_file")
data_desktop_arg=$(desktop_exec_quote "$platformless_data_dir")
plan_desktop_arg=$(desktop_exec_quote "$install_root/install-plan.json")
cat > "$desktop_dir/com.platformless.button-game.desktop" <<EOF
[Desktop Entry]
Type=Application
Name=Button Game
Exec=$game_desktop_exec
Icon=com.platformless.button-game
Categories=Game;
Terminal=false
Actions=Manage;

[Desktop Action Manage]
Name=Manage Button Game
Exec=$launcher_desktop_exec --ui --state $state_desktop_arg --data-dir $data_desktop_arg --install-plan $plan_desktop_arg
EOF

runtime_committed=1
rm -rf "$backup_root"
trap - EXIT

cat >&2 <<EOF
The Button Game graphical installer is ready.
Desktop entry: $desktop_dir/com.platformless.button-game.desktop
Open it later from your application menu, or run:
  $install_root/platformless-game-launcher
To manage the installation explicitly, run:
  $install_root/platformless-launcher $launcher_args
Installer log: $installer_log
EOF

if [ "$start_ui" -eq 1 ]; then
  printf 'Opening the Button Game graphical installer…\n'
  exec "$install_root/platformless-launcher" \
    --ui \
    --state "$state_file" \
    --data-dir "$platformless_data_dir" \
    --install-plan "$install_root/install-plan.json"
fi
echo "$install_root"
