#!/usr/bin/env bash
set -euo pipefail

APP_PATH="/Applications/Kasa Cue.app"
DMG_PATH="$(cd "$(dirname "$0")" && pwd)/Kasa-Cue-mac-arm64.dmg"

if [[ ! -d "$APP_PATH" ]]; then
  echo "Kasa Cue is not installed in Applications yet."
  echo "Opening the installer DMG now. Drag Kasa Cue.app to Applications, then run this file again."
  if [[ -f "$DMG_PATH" ]]; then
    xattr -dr com.apple.quarantine "$DMG_PATH" 2>/dev/null || true
    xattr -cr "$DMG_PATH" 2>/dev/null || true
    open "$DMG_PATH"
  fi
  read -r -p "Press Return to close."
  exit 0
fi

echo "Allowing this local Kasa Cue install to open..."
xattr -dr com.apple.quarantine "$APP_PATH" 2>/dev/null || true
xattr -cr "$APP_PATH" 2>/dev/null || true
codesign --verify --deep --strict "$APP_PATH" 2>/dev/null || true

echo "Opening Kasa Cue..."
open "$APP_PATH"
echo "Done. You can close this window."
