#!/bin/sh
# MakeMode alpha installer — https://www.makemode.eu/download
#
# Usage:  curl -fsSL https://www.makemode.eu/install.sh | sh
#
# Downloads the desktop app from EU infrastructure (Scaleway, fr-par) and
# installs it. Because curl doesn't set the macOS quarantine attribute, this
# path skips the "unidentified developer" warnings the unsigned alpha builds
# otherwise trigger. Covers macOS and Debian/Ubuntu Linux; Windows users:
# grab the installer from the download page.
#
# When installers are re-uploaded for a new app release, bump VER here and
# re-upload this script (see site/DEPLOY.md).
set -eu

VER="0.1.4"
BASE="https://www.makemode.eu/download"

say() { printf '%s\n' "$*"; }

os=$(uname -s 2>/dev/null || echo unknown)
case "$os" in
  Darwin)
    case "$(uname -m)" in
      arm64) dmg="MakeMode_${VER}_aarch64.dmg" ;;
      *)     dmg="MakeMode_${VER}_x64.dmg" ;;
    esac
    tmp=$(mktemp -d)
    trap 'rm -rf "$tmp"' EXIT
    say "- downloading $dmg from EU servers..."
    curl -fSL --progress-bar "$BASE/$dmg" -o "$tmp/makemode.dmg"
    say "- installing to /Applications..."
    mnt=$(hdiutil attach -nobrowse -readonly "$tmp/makemode.dmg" | awk -F'\t' '/\/Volumes\//{print $NF; exit}')
    [ -n "$mnt" ] && [ -d "$mnt/MakeMode.app" ] || { say "!! unexpected disk image layout — install by hand from $BASE"; exit 1; }
    rm -rf /Applications/MakeMode.app
    cp -R "$mnt/MakeMode.app" /Applications/
    hdiutil detach "$mnt" -quiet || true
    say "- launching..."
    open /Applications/MakeMode.app
    say "done — MakeMode is in your Applications folder."
    ;;
  Linux)
    deb="MakeMode_${VER}_amd64.deb"
    tmp=$(mktemp -d)
    trap 'rm -rf "$tmp"' EXIT
    say "- downloading $deb from EU servers..."
    curl -fSL --progress-bar "$BASE/$deb" -o "$tmp/$deb"
    say "- installing (asks for sudo)..."
    sudo apt-get install -y "$tmp/$deb" || sudo dpkg -i "$tmp/$deb"
    say "done — launch MakeMode from your app menu."
    ;;
  *)
    say "This installer covers macOS and Debian/Ubuntu Linux."
    say "On Windows, download the installer at https://www.makemode.eu/download"
    exit 1
    ;;
esac
