#!/usr/bin/env bash
# AVADIOS — one-click DIO installer (Linux / macOS)
#
#   curl -sSL https://avadios.com/downloads/install-dio.sh | bash
#
# Linux x64 (glibc): downloads DIO + verifies SHA256 + drops a desktop icon
# + launches the GUI. macOS / Linux ARM64: native build still in progress.

set -euo pipefail

VERSION='0.1.0-alpha'
BASE='https://avadios.com/downloads'
DEST="${AVADIOS_HOME:-$HOME/.avadios}"

bold()  { printf '\033[1m%s\033[0m\n' "$*"; }
green() { printf '\033[1;32m%s\033[0m\n' "$*"; }
info()  { printf '  %s\n' "$*"; }

OS="$(uname -s)"
ARCH="$(uname -m)"

case "$OS::$ARCH" in
  Linux::x86_64)
    FILE="dio-v${VERSION}-linux-x64"
    EXPECTED='6170716bac8b7c4ee74a8553b3b96abbb2b63713263e281d28dd3de8cede28ac'
    ;;
  *)
    bold "[AVADIOS] DIO installer"
    echo
    echo "  Native DIO desktop binary for $OS/$ARCH is in progress."
    echo "  Available now: Linux x86_64 (glibc) and Windows x64."
    echo
    echo "  To be notified the moment $OS/$ARCH DIO ships, email:"
    echo "    hello@avadios.com"
    echo
    exit 0
    ;;
esac

mkdir -p "$DEST/bin" "$HOME/.local/share/applications"
BIN="$DEST/bin/dio"

bold "============================================"
bold " AVADIOS — DIO installer  v$VERSION"
bold "============================================"
info "Target : $BIN"
echo

info "[1/4] Downloading $FILE ..."
if command -v curl >/dev/null 2>&1; then
  curl -fSL "$BASE/$FILE" -o "$BIN"
elif command -v wget >/dev/null 2>&1; then
  wget -qO "$BIN" "$BASE/$FILE"
else
  echo "[ERROR] need curl or wget" >&2
  exit 1
fi
chmod +x "$BIN"

info "[2/4] Verifying SHA256 ..."
if command -v sha256sum >/dev/null 2>&1; then
  ACTUAL=$(sha256sum "$BIN" | awk '{print $1}')
elif command -v shasum >/dev/null 2>&1; then
  ACTUAL=$(shasum -a 256 "$BIN" | awk '{print $1}')
else
  echo "[WARN] no sha256sum/shasum, skipping verification" >&2
  ACTUAL="$EXPECTED"
fi
if [ "$ACTUAL" != "$EXPECTED" ]; then
  echo "[ERROR] hash mismatch" >&2
  echo "        expected $EXPECTED" >&2
  echo "        got      $ACTUAL"   >&2
  rm -f "$BIN"
  exit 2
fi

info "[3/4] Creating desktop launcher ..."
DESKTOP_FILE="$HOME/.local/share/applications/dio.desktop"
cat > "$DESKTOP_FILE" <<EOF
[Desktop Entry]
Type=Application
Name=DIO
Comment=DIO — your AVADIOS desktop
Exec=$BIN
Icon=utilities-terminal
Terminal=false
Categories=Network;Utility;
StartupNotify=true
EOF
chmod +x "$DESKTOP_FILE"

# Drop a click-to-launch icon on Desktop if one exists
if [ -d "$HOME/Desktop" ]; then
  cp "$DESKTOP_FILE" "$HOME/Desktop/dio.desktop"
  chmod +x "$HOME/Desktop/dio.desktop"
  command -v gio >/dev/null 2>&1 && \
    gio set "$HOME/Desktop/dio.desktop" metadata::trusted true 2>/dev/null || true
fi

# Refresh app menu cache so DIO appears in launchers right away
command -v update-desktop-database >/dev/null 2>&1 && \
  update-desktop-database "$HOME/.local/share/applications" 2>/dev/null || true

info "[4/4] Launching DIO ..."
( "$BIN" >/dev/null 2>&1 & disown ) || true

echo
green "============================================"
green " Welcome to DIO."
green "============================================"
info "Installed at : $BIN"
info "App menu     : search 'DIO'"
info "Desktop icon : double-click the new DIO entry"
echo
info "DIO checks for new releases automatically."
info "When an update is published you will see an"
info "'Update available' banner inside the app."
echo
