#!/bin/bash
# omarchy:summary=Install a China app from the catalog with source confirmation
# omarchy:args=<app-id> [--yes]
# omarchy:examples=omarchycn app install wechat | omarchycn app install tencent-docs

set -euo pipefail

APPS_JSON="$OMARCHY_PATH/cn/apps.json"

app="${1:?usage: omarchycn app install <app-id> (see: omarchycn app list)}"
yes="${2:-}"

entry=$(jq -e --arg a "$app" '.apps[$a]' "$APPS_JSON") || {
  echo "Unknown app: $app (see: omarchycn app list)" >&2
  exit 1
}

name=$(jq -r '.name' <<<"$entry")
source_type=$(jq -r '.source' <<<"$entry")
license=$(jq -r '.license' <<<"$entry")

if [[ $license == "proprietary" && $yes != "--yes" ]]; then
  pkg=$(jq -r '.package' <<<"$entry")
  echo "$name 为专有软件，来源: AUR/$pkg（构建脚本公开，二进制来自厂商）"
  if [[ -t 0 ]]; then
    gum confirm "确认安装？" || exit 1
  else
    echo "非交互环境需显式加 --yes 确认专有软件安装" >&2
    exit 1
  fi
fi

case "$source_type" in
aur)
  omarchy-pkg-aur-add "$(jq -r '.package' <<<"$entry")"
  ;;
webapp)
  omarchy-webapp-install "$name" "$(jq -r '.url' <<<"$entry")" "$(jq -r '.icon' <<<"$entry")"
  ;;
*)
  echo "Unknown source type: $source_type" >&2
  exit 1
  ;;
esac

echo "$name 安装完成"
