#!/bin/bash
# Download and verify Nuthead AI, then install it in /Applications.
set -euo pipefail
url='https://bewegtbild-bock.de/nuthead-ai/downloads/files/nuthead-ai.dmg'
expected_sha256='3367178a28940b21d65b5d8d1aded27d7011e7704c31a950900f56fa19418a47'
work=$(mktemp -d "${TMPDIR:-/tmp}/nuthead-install.XXXXXX")
mounted=0
cleanup() {
  if [ "$mounted" = 1 ]; then hdiutil detach "$work/mount" -quiet || true; fi
  rm -rf "$work"
}
trap cleanup EXIT
if [ "$(uname -s)" != Darwin ] || [ "$(uname -m)" != arm64 ]; then
  echo 'Nuthead AI requires a Mac with Apple Silicon.' >&2; exit 1
fi
curl -fL --retry 2 "$url" -o "$work/nuthead-ai.dmg"
printf '%s  %s\n' "$expected_sha256" "$work/nuthead-ai.dmg" | shasum -a 256 -c -
mkdir "$work/mount"
hdiutil attach "$work/nuthead-ai.dmg" -mountpoint "$work/mount" -nobrowse -readonly -quiet
mounted=1
app=''
for candidate in "$work/mount"/Nuthead*.app; do
  [ -d "$candidate" ] || continue
  if [ -n "$app" ]; then echo 'More than one Nuthead app found in the disk image.' >&2; exit 1; fi
  app="$candidate"
done
[ -n "$app" ] || { echo 'No Nuthead app found in the disk image.' >&2; exit 1; }
destination="/Applications/$(basename "$app")"
# Use the same transactional replacement both with and without admin rights.
cat > "$work/replace-app.sh" <<'REPLACE'
#!/bin/bash
set -euo pipefail
source_app=$1
destination=$2
install_dir=$(mktemp -d "$(dirname "$destination")/.nuthead-install.XXXXXX")
backup="$install_dir/previous.app"
staged="$install_dir/$(basename "$destination")"
installed=0
cleanup_install() {
  if [ "$installed" = 0 ] && [ -e "$backup" ]; then
    if [ -e "$destination" ]; then rm -rf "$destination"; fi
    mv "$backup" "$destination" || { echo "Restore the previous app from: $backup" >&2; return; }
  fi
  rm -rf "$install_dir"
}
trap cleanup_install EXIT
ditto "$source_app" "$staged"
xattr -dr com.apple.quarantine "$staged" 2>/dev/null || true
if [ -e "$destination" ]; then mv "$destination" "$backup"; fi
mv "$staged" "$destination"
installed=1
REPLACE
if [ -w /Applications ] && { [ ! -e "$destination" ] || [ -w "$destination" ]; }; then
  /bin/bash "$work/replace-app.sh" "$app" "$destination"
else
  echo 'Please authorize installation in the macOS administrator dialog.'
  mac_locale=$(defaults read -g AppleLocale 2>/dev/null || printf '%s' "${LANG:-en}")
  case "$mac_locale" in
    de*) prompt='Nuthead AI benötigt Zugriff, um die App im Programme-Ordner zu installieren oder zu aktualisieren.' ;;
    *) prompt='Nuthead AI needs access to install or update the app in Applications.' ;;
  esac
  osascript - "$work/replace-app.sh" "$app" "$destination" "$prompt" <<'APPLESCRIPT'
on run argv
  set installCommand to "/bin/bash " & quoted form of (item 1 of argv) & " " & quoted form of (item 2 of argv) & " " & quoted form of (item 3 of argv)
  do shell script installCommand with prompt (item 4 of argv) with administrator privileges
end run
APPLESCRIPT
fi
echo "Installed: $destination"
echo 'Open Nuthead AI from Applications.'
