#!/usr/bin/env bash
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BUNDLE_DIR="$SCRIPT_DIR/sync-client"
INSTALL_DIR="${1:-$HOME/.local/opt/sda-sync}"
ICON_MODE="${2:-auto}"
AUTOSTART_MODE="${3:-on}"
DESKTOP_DIR="$HOME/.local/share/applications"
AUTOSTART_DIR="$HOME/.config/autostart"
ICON_DIR="$HOME/.local/share/icons/hicolor/256x256/apps"
DESKTOP_FILE="$DESKTOP_DIR/sda-sync.desktop"
AUTOSTART_FILE="$AUTOSTART_DIR/sda-sync.desktop"
EXEC_PATH="$INSTALL_DIR/sync-client"
BRAND="${SDA_BRAND:-default}"
normalize_brand() {
  echo "$1" | tr '[:upper:]' '[:lower:]' | tr -d '\r'
}

resolve_brand() {
  local explicit_brand marker_path marker_brand
  explicit_brand="$(normalize_brand "${SDA_BRAND:-}")"
  if [[ -n "$explicit_brand" ]]; then
    echo "$explicit_brand"
    return
  fi

  marker_path="$BUNDLE_DIR/.sda-brand"
  if [[ -f "$marker_path" ]]; then
    marker_brand="$(head -n 1 "$marker_path" | sed 's/^\xEF\xBB\xBF//')"
    marker_brand="$(normalize_brand "$marker_brand")"
    if [[ -n "$marker_brand" ]]; then
      echo "$marker_brand"
      return
    fi
  fi

  echo "default"
}

detect_icon_name() {
  if [[ "$BRAND_LOWER" == "sew" ]]; then
    echo "SEW_LOGO.svg"
    return
  fi

  if [[ "$ICON_MODE" == "dark" ]]; then
    echo "icond.png"
    return
  fi

  if [[ "$ICON_MODE" == "light" ]]; then
    echo "iconw.png"
    return
  fi

  # Prefer GNOME dark mode when detectable; otherwise default to the light icon.
  if command -v gsettings >/dev/null 2>&1; then
    local color_scheme
    color_scheme="$(gsettings get org.gnome.desktop.interface color-scheme 2>/dev/null || true)"
    if [[ "$color_scheme" == *"prefer-dark"* ]]; then
      echo "icond.png"
      return
    fi
  fi

  echo "iconw.png"
}

resolve_autostart_mode() {
  if [[ -n "${3:-}" ]]; then
    echo "$AUTOSTART_MODE"
    return
  fi

  echo "on"
}

SOURCE_EXE_NAME="sync-client"

if [[ ! -x "$BUNDLE_DIR/$SOURCE_EXE_NAME" ]]; then
  echo "Bundled app not found at $BUNDLE_DIR"
  echo "Make sure install.sh is next to the sync-client/ directory from the package."
  exit 1
fi

if [[ "$ICON_MODE" != "auto" && "$ICON_MODE" != "dark" && "$ICON_MODE" != "light" ]]; then
  echo "Invalid icon mode: $ICON_MODE"
  echo "Use one of: auto, dark, light"
  exit 1
fi

AUTOSTART_MODE="$(resolve_autostart_mode "$@")"
BRAND_LOWER="$(resolve_brand)"
EXEC_CMD="$EXEC_PATH"
if [[ "$BRAND_LOWER" == "sew" ]]; then
  EXEC_CMD="env SDA_BRAND=sew $EXEC_PATH"
fi

if [[ "$AUTOSTART_MODE" != "on" && "$AUTOSTART_MODE" != "off" ]]; then
  echo "Invalid autostart mode: $AUTOSTART_MODE"
  echo "Use one of: on, off"
  exit 1
fi

ICON_NAME="$(detect_icon_name)"
ICON_PATH="$INSTALL_DIR/$ICON_NAME"

mkdir -p "$INSTALL_DIR" "$DESKTOP_DIR" "$AUTOSTART_DIR" "$ICON_DIR"
rm -rf "$INSTALL_DIR"
mkdir -p "$INSTALL_DIR"

cp -a "$BUNDLE_DIR"/. "$INSTALL_DIR"/
if [[ "$ICON_NAME" == "SEW_LOGO.svg" ]]; then
  install -m 0644 "$BUNDLE_DIR/$ICON_NAME" "$ICON_DIR/sda-sync.svg"
else
  install -m 0644 "$BUNDLE_DIR/$ICON_NAME" "$ICON_DIR/sda-sync.png"
fi

sed \
  -e "s|__EXEC_PATH__|$EXEC_CMD|g" \
  -e "s|__ICON_PATH__|$ICON_PATH|g" \
  "$BUNDLE_DIR/sync-client.desktop" > "$DESKTOP_FILE"

chmod 0644 "$DESKTOP_FILE"

if [[ "$AUTOSTART_MODE" == "on" ]]; then
  cp "$DESKTOP_FILE" "$AUTOSTART_FILE"
else
  rm -f "$AUTOSTART_FILE"
fi

update-desktop-database "$DESKTOP_DIR" 2>/dev/null || true
gtk-update-icon-cache "$HOME/.local/share/icons/hicolor" 2>/dev/null || true

echo "Installed SDA Sync"
echo "Bundle: $INSTALL_DIR"
echo "Desktop entry: $DESKTOP_FILE"
echo "Brand: $BRAND"
echo "Launcher icon: $ICON_NAME"
echo "Autostart: $AUTOSTART_MODE"
