#!/bin/sh
# 卸载防御：移除 Moo 管理的 kspeeder 引擎进程 + docker 加速钩子
# （正常路径 moo-server 退出时已自清理；此处兜底异常退出场景）

set -e

# 0) 卸载向导选择（0.6.320r3，官方模式参照 trim.photos/1panel）：
#    wizard_delete_data=true（用户在卸载向导选「删除应用数据」）→ 删数据目录；
#    CLI uninstall / 无向导字段 = 默认 false = 保留数据（平台默认行为，不变）。
#    安全：只删 glob 精确命中的 /volN/@app{data,conf,meta,home}/moo，
#    绝不碰用户自选「下载目录」（config.json download_dir，可能含其他文件）。
if [ "${wizard_delete_data:-false}" = "true" ]; then
    _ud() {
        echo "[uninstall-data] $*"
        [ -n "${TRIM_TEMP_LOGFILE:-}" ] && echo "[uninstall-data] $*" >> "${TRIM_TEMP_LOGFILE}" 2>/dev/null || true
    }
    _lp="$(cat "${TRIM_PKGVAR:-/var/apps/moo/var}/.logpath" 2>/dev/null | tr -d '[:space:]')"
    [ -n "${_lp}" ] && [ -f "${_lp}" ] && _ud "log -> ${_lp}" >/dev/null
    for p in /vol*/@appdata/moo /vol*/@appconf/moo /vol*/@appmeta/moo /vol*/@apphome/moo; do
        if [ -d "${p}" ]; then
            if rm -rf "${p}" 2>/dev/null; then
                _ud "removed ${p}"
            else
                _ud "WARN failed to remove ${p}"
            fi
        fi
    done
    _ud "data deletion done (download dir untouched)"
fi

# 1) 停掉 kspeeder 引擎（按 cachePath 特征匹配，避免误杀独立安装的 kspeeder 应用）
for pid in $(pgrep -f "iStoreEnhance .*--cachePath /vol1/@appdata/moo/kspeeder" 2>/dev/null || true); do
    kill "$pid" 2>/dev/null || true
done
sleep 1
for pid in $(pgrep -f "iStoreEnhance .*--cachePath /vol1/@appdata/moo/kspeeder" 2>/dev/null || true); do
    kill -9 "$pid" 2>/dev/null || true
done

# 2) /etc/hosts 标记行
grep -vF "# moo-kspeeder" /etc/hosts > /etc/hosts.moo-uninst 2>/dev/null \
    && cat /etc/hosts.moo-uninst > /etc/hosts 2>/dev/null || true
rm -f /etc/hosts.moo-uninst 2>/dev/null || true

# 3) daemon.json 中 Moo 添加的镜像条目（保留用户自配条目；fnOS 无 uci）
python3 - <<'PYEOF' 2>/dev/null || true
import json, os, time

path = "/etc/docker/daemon.json"
try:
    with open(path) as f:
        cfg = json.load(f)
    mirrors = cfg.get("registry-mirrors") or []
    ours = [m for m in mirrors if m.startswith("https://registry.linkease.net:")]
    if ours:
        kept = [m for m in mirrors if m not in ours]
        bak = path + ".moo-uninst-" + time.strftime("%Y%m%d_%H%M%S")
        try:
            with open(path) as f:
                orig = f.read()
            with open(bak, "w") as f:
                f.write(orig)
        except OSError:
            pass
        if kept:
            cfg["registry-mirrors"] = kept
        else:
            cfg.pop("registry-mirrors", None)
        with open(path, "w") as f:
            json.dump(cfg, f, indent=2)
        os.system("systemctl reload docker >/dev/null 2>&1")
except Exception:
    pass
PYEOF

echo "moo uninstall cleanup done"
exit 0
