#!/bin/bash
# Double-click after downloading Ringline-mac.zip — installs to Applications.
set -euo pipefail

ZIP_DIR="$(cd "$(dirname "$0")" && pwd)"
ZIP_FILE="$ZIP_DIR/Ringline-mac.zip"
APP_DEST="/Applications/Ringline.app"
WORK="$(mktemp -d)"
trap 'rm -rf "$WORK"' EXIT

if [[ ! -f "$ZIP_FILE" ]]; then
  osascript -e 'display alert "Put this file in the same folder as Ringline-mac.zip" message "Download Ringline-mac.zip from v3719.com, then move Install-Ringline-from-Zip.command into your Downloads folder next to the zip."' || true
  exit 1
fi

echo "Unpacking..."
ditto -xk "$ZIP_FILE" "$WORK"
APP_SRC="$WORK/Ringline.app"
if [[ ! -d "$APP_SRC" ]]; then
  echo "Ringline.app not found inside zip" >&2
  exit 1
fi

echo "Installing to Applications..."
rm -rf "$APP_DEST"
ditto "$APP_SRC" "$APP_DEST"
xattr -cr "$APP_DEST" 2>/dev/null || true

osascript <<'APPLESCRIPT' || true
display dialog "RINGLINE is in Applications.

The FIRST time you play:
1. Open Applications
2. RIGHT-CLICK Ringline
3. Choose Open
4. Click Open again" buttons {"Show in Applications"} default button 1 with title "Almost done"
APPLESCRIPT

open -R "$APP_DEST"
