#!/bin/bash

# ============================================================
# Flash Ostrich OS - zypper wrapper
# 功能：
#   1. 攔截 removelock / rl，防止用戶解除系統鎖定
#   2. 桌面環境安裝 → 彈 [y/N] 警示
#   3. 其他應用程式安裝 → 引導去 KDE Discover（Flathub）
#   4. up / update → 自動轉 dup
# ============================================================

# === 桌面環境關鍵詞 ===
DESKTOP_PATTERNS="gnome|xfce|lxqt|mate|cinnamon|enlightenment|budgie|deepin|pantheon"
DESKTOP_PACKAGES="gnome-shell|gnome-session|gnome-desktop|xfce4-session|xfwm4|lxqt-session|mate-session-manager|cinnamon|enlightenment|budgie-desktop|deepin-desktop-environment|pantheon-session"

# === 第一層：攔截 removelock / rl ===
for arg in "$@"; do
  case "$arg" in
    removelock|rl)
      MSG="⚠️ Ostrich Technology 不建議您移除系統鎖定。\n\n移除鎖定可能會導致系統不穩定或損壞 OS。\n\n如果您真的需要解除鎖定，請先聯繫 Ostrich Technology 支援團隊。"
      if [ -n "$DISPLAY" ] && command -v kdialog &>/dev/null; then
        kdialog --title "Flash Ostrich OS" --sorry "$MSG"
      elif [ -n "$DISPLAY" ] && command -v zenity &>/dev/null; then
        zenity --warning --title="Flash Ostrich OS" --text="$MSG" --width=400
      else
        echo ""
        echo -e "\033[0;31m⚠️  Ostrich Technology 不建議您移除系統鎖定。\033[0m"
        echo ""
        echo "   移除鎖定可能會導致系統不穩定或損壞 OS。"
        echo "   如果您真的需要解除鎖定，請先聯繫 Ostrich Technology 支援團隊。"
        echo ""
      fi
      exit 1
      ;;
  esac
done

# === 檢查係咪安裝命令 ===
has_install=false
for arg in "$@"; do
  case "$arg" in
    install|in)
      has_install=true
      ;;
  esac
done

# === 第二層：安裝命令處理 ===
if $has_install; then
  is_desktop=false
  prev=""
  for arg in "$@"; do
    if [[ "$prev" == "pattern" && "$arg" =~ ^($DESKTOP_PATTERNS)$ ]]; then
      is_desktop=true
      break
    fi
    if [[ "$arg" =~ ^pattern:($DESKTOP_PATTERNS)$ ]]; then
      is_desktop=true
      break
    fi
    if [[ "$arg" =~ ^($DESKTOP_PACKAGES)$ ]]; then
      is_desktop=true
      break
    fi
    prev="$arg"
  done

  if $is_desktop; then
    # ---- 桌面環境：彈 y/N 警示 ----
    MSG="⚠️  Ostrich Technology does not recommend installing other desktop environments, as it may damage the OS.\n\nDo you want to continue? [y/N]"
    if [ -n "$DISPLAY" ] && command -v kdialog &>/dev/null; then
      if kdialog --title "Flash Ostrich OS" --warningyesno "$MSG"; then
        echo "    Proceeding with desktop environment installation..."
      else
        echo "    Installation cancelled."
        exit 1
      fi
    else
      echo ""
      echo "⚠️  Ostrich Technology does not recommend installing other desktop environments, as it may damage the OS."
      echo ""
      echo -n "    Do you want to continue? [y/N] "
      if [ -t 0 ]; then
        read -r answer
        case "$answer" in
          y|Y|yes|YES)
            echo "    Proceeding with desktop environment installation..."
            ;;
          *)
            echo "    Installation cancelled."
            exit 1
            ;;
        esac
      else
        echo ""
        echo "    Non-interactive session detected. Installation cancelled for safety."
        exit 1
      fi
    fi
  else
    # ---- 其他應用程式：引導去 Discover ----
    MSG="⚠️  Ostrich Technology recommends using KDE Discover to install applications (Flathub version) for system stability.\n\nSystem updates: please continue using sudo zypper dup."
    if [ -n "$DISPLAY" ] && command -v kdialog &>/dev/null; then
      kdialog --title "Flash Ostrich OS" --sorry "$MSG"
    elif [ -n "$DISPLAY" ] && command -v zenity &>/dev/null; then
      zenity --warning --title="Flash Ostrich OS" --text="$MSG" --width=400
    else
      echo ""
      echo -e "\033[0;31m⚠️  Please use KDE Discover to install applications!\033[0m"
      echo ""
      echo "    Ostrich Technology recommends using KDE Discover to install Flathub versions"
      echo "    for system stability."
      echo ""
      echo "    System updates: please continue using: sudo zypper dup"
      echo ""
    fi
    exit 1
  fi
fi

# === 第三層：up / update 自動轉 dup ===
args=()
has_warning=false
for arg in "$@"; do
  if [[ "$arg" == "up" || "$arg" == "update" ]]; then
    if [ "$has_warning" = false ]; then
      echo -e "\033[0;31m⚠️ FOOS是滾動更新版本，請使用 dup。已自動轉換為 zypper dup 並正在執行...\033[0m"
      has_warning=true
    fi
    args+=("dup")
  else
    args+=("$arg")
  fi
done

exec /usr/bin/zypper "${args[@]}"
