#!/bin/sh
# Stenvakt installer: curl -fsSL https://stenvakt.com/install.sh | sh
# Downloads the app, checks its checksum, puts it in /Applications (or ~/Applications, or wherever Stenvakt already is)
# and opens it. A download made with curl is not quarantined, so macOS opens the app without the "could not verify" prompt.
# Everything sits in main(), called on the last line: a download cut short runs nothing.
set -eu

main() {
  URL="https://stenvakt.com/download/Stenvakt.zip"
  SUM_URL="https://stenvakt.com/download/Stenvakt.zip.sha256"

  [ "$(uname -s)" = "Darwin" ] || { echo "Stenvakt is a Mac app."; exit 1; }
  # hw.optional.arm64 is 1 on Apple silicon, also when this shell runs under Rosetta (where uname -m says x86_64)
  [ "$(sysctl -n hw.optional.arm64 2>/dev/null || echo 0)" = "1" ] || { echo "Stenvakt needs a Mac with Apple silicon (M1 or newer)."; exit 1; }
  major=$(sw_vers -productVersion | cut -d. -f1)
  case "$major" in ''|*[!0-9]*) major=0 ;; esac
  [ "$major" -ge 14 ] || { echo "Stenvakt needs macOS 14 (Sonoma) or newer. This Mac has $(sw_vers -productVersion)."; exit 1; }
  if pgrep -x -u "$(id -u)" Stenvakt >/dev/null 2>&1; then echo "Quit Stenvakt first, then run this again."; exit 1; fi

  TMP=$(mktemp -d)
  trap 'rm -rf "$TMP"' EXIT
  echo "Downloading Stenvakt..."
  curl -fsSL "$URL" -o "$TMP/Stenvakt.zip"
  want=$(curl -fsSL "$SUM_URL" | cut -d' ' -f1)
  have=$(shasum -a 256 "$TMP/Stenvakt.zip" | cut -d' ' -f1)
  [ -n "$want" ] && [ "$want" = "$have" ] || { echo "Checksum does not match; nothing was installed."; exit 1; }
  ditto -x -k "$TMP/Stenvakt.zip" "$TMP/unpacked"
  [ -d "$TMP/unpacked/Stenvakt.app" ] || { echo "The download has no Stenvakt.app; nothing was installed."; exit 1; }

  # an existing copy is replaced where it is; otherwise /Applications, or ~/Applications when that is not writable
  if [ -d /Applications/Stenvakt.app ]; then DEST=/Applications
  elif [ -d "$HOME/Applications/Stenvakt.app" ]; then DEST="$HOME/Applications"
  elif [ -w /Applications ]; then DEST=/Applications
  else DEST="$HOME/Applications"; mkdir -p "$DEST"
  fi
  [ -w "$DEST" ] || { echo "Cannot write to $DEST. Move Stenvakt out of it, or install from the DMG."; exit 1; }

  # copy next to the old app first, then swap with renames: the old copy goes only after the new one is complete
  NEW="$DEST/.Stenvakt.app.new"
  OLD="$DEST/.Stenvakt.app.old"
  rm -rf "$NEW" "$OLD"
  ditto "$TMP/unpacked/Stenvakt.app" "$NEW" || { rm -rf "$NEW"; echo "Copying to $DEST failed; nothing was changed."; exit 1; }
  if [ -d "$DEST/Stenvakt.app" ]; then
    mv "$DEST/Stenvakt.app" "$OLD" || { rm -rf "$NEW"; echo "Could not move the old copy aside; nothing was changed."; exit 1; }
  fi
  if ! mv "$NEW" "$DEST/Stenvakt.app"; then
    [ -d "$OLD" ] && mv "$OLD" "$DEST/Stenvakt.app"
    rm -rf "$NEW"
    echo "Could not put the new copy in place; the old one is back."
    exit 1
  fi
  rm -rf "$OLD" || echo "The old copy stayed at $OLD; you can delete it."
  echo "Installed to $DEST/Stenvakt.app"
  open "$DEST/Stenvakt.app"
}

main "$@"
