#!/bin/bash
#
#  Archeve Wasser — installer for macOS
#  ---------------------------------------------------------------------------
#  Why this file exists, rather than just downloading the app:
#
#  macOS attaches a "quarantine" flag to anything a BROWSER downloads. For an app that
#  is not notarized by Apple — which costs $99/year — that flag makes macOS refuse to
#  open it at all, with no way through from the disk-image window.
#
#  The flag comes from the browser, not from the file. This script fetches the app with
#  curl instead, which never attaches it. Nothing is bypassed and nothing is disabled:
#  the app is code-signed and you can check it yourself with
#      codesign --verify --deep --strict "/Applications/Archeve Wasser.app"
#
set -euo pipefail

BASE="https://wasser.archeve.in/downloads"
APP="Archeve Wasser.app"
DEST="/Applications"
TMP="$(mktemp -d)"
MNT="$TMP/mnt"

# Always clean up the mount and temp files, including on failure or Ctrl-C — leaving a
# disk image mounted after a failed install is its own small mess.
cleanup() {
  [ -d "$MNT" ] && hdiutil detach "$MNT" -quiet 2>/dev/null || true
  rm -rf "$TMP" 2>/dev/null || true
}
trap cleanup EXIT INT TERM

printf '\n  \033[1mArcheve Wasser — installer\033[0m\n'
printf '  ─────────────────────────────────────────────\n\n'

# Apple silicon and Intel need different builds. uname -m reports the TRUE architecture
# here; a browser cannot tell the two apart, which is why the website has to show both.
ARCH="$(uname -m)"
if [ "$ARCH" = "arm64" ]; then
  DMG="Archeve.Wasser-1.0.0-arm64.dmg"
  printf '  Mac type      Apple silicon (%s)\n' "$ARCH"
else
  DMG="Archeve.Wasser-1.0.0.dmg"
  printf '  Mac type      Intel (%s)\n' "$ARCH"
fi
printf '  Installing    %s\n\n' "$DEST/$APP"

printf '  Downloading … '
if ! curl -fL --progress-bar "$BASE/$DMG" -o "$TMP/aip.dmg"; then
  printf '\n\n  \033[31mDownload failed.\033[0m Check your connection and run this again.\n'
  printf '  If it keeps failing, email info@archeve.in\n\n'
  exit 1
fi

printf '  Opening the disk image … '
mkdir -p "$MNT"
if ! hdiutil attach "$TMP/aip.dmg" -mountpoint "$MNT" -nobrowse -quiet; then
  printf '\n\n  \033[31mCould not open the downloaded file.\033[0m It may be incomplete — run this again.\n\n'
  exit 1
fi
printf 'done\n'

# Replace any previous version rather than failing on "already exists". Removing it first
# also avoids merging an old build's files into the new one, which produces an app that
# is neither version.
if [ -d "$DEST/$APP" ]; then
  printf '  Removing the previous version … '
  rm -rf "$DEST/$APP" 2>/dev/null || {
    printf '\n\n  Needs your password to replace the existing copy:\n'
    sudo rm -rf "$DEST/$APP"
  }
  printf 'done\n'
fi

printf '  Copying to Applications … '
if ! cp -R "$MNT/$APP" "$DEST/" 2>/dev/null; then
  printf '\n  Needs your password to write to Applications:\n'
  sudo cp -R "$MNT/$APP" "$DEST/"
fi
printf 'done\n'

# Belt and braces. curl does not set the quarantine flag, so there should be nothing to
# clear — but if this file was ever run after a browser download, clearing it costs
# nothing and removes the one thing that could still block the app.
xattr -dr com.apple.quarantine "$DEST/$APP" 2>/dev/null || true

printf '  Checking the signature … '
if codesign --verify --deep --strict "$DEST/$APP" >/dev/null 2>&1; then
  printf 'valid\n'
else
  printf '\n\n  \033[31mThe signature did not verify.\033[0m Do not run it — email info@archeve.in\n\n'
  exit 1
fi

printf '\n  \033[32mInstalled.\033[0m Opening it now — it will also be in your Applications folder.\n\n'
open "$DEST/$APP"
