#!/system/bin/sh
set -eu

try_run() {
    set +e
    "$@" >/dev/null 2>&1
    code=$?
    set -e
    return $code
}

clear_http_proxy() {
    # 方法 1: su2 heredoc (光速虚拟机，HarukiProxy 同款)
    if command -v su2 >/dev/null 2>&1; then
        set +e
        su2 <<PROXY_CLEAR
settings put global http_proxy :0
settings put global https_proxy :0
PROXY_CLEAR
        code=$?
        set -e
        if [ $code -eq 0 ]; then return 0; fi
    fi

    # 方法 2: /system/bin/settings
    if [ -x /system/bin/settings ]; then
        if try_run /system/bin/settings put global http_proxy :0; then
            return 0
        fi

        try_run /system/bin/settings delete global http_proxy || true
        try_run /system/bin/settings delete global global_http_proxy_host || true
        try_run /system/bin/settings delete global global_http_proxy_port || true
        try_run /system/bin/settings delete global global_http_proxy_exclusion_list || true
        return 0
    fi

    # 方法 3: /system/bin/cmd
    if [ -x /system/bin/cmd ]; then
        if try_run /system/bin/cmd settings put global http_proxy :0; then
            return 0
        fi
    fi

    # 方法 4: /system/bin/content
    if [ -x /system/bin/content ]; then
        try_run /system/bin/content delete --uri content://settings/global --where "name='http_proxy'" || true
        try_run /system/bin/content delete --uri content://settings/global --where "name='global_http_proxy_host'" || true
        try_run /system/bin/content delete --uri content://settings/global --where "name='global_http_proxy_port'" || true
        try_run /system/bin/content delete --uri content://settings/global --where "name='global_http_proxy_exclusion_list'" || true
        return 0
    fi

    return 1
}

# ---- Root 提权 ----
# 优先尝试 su2 (光速虚拟机专用)，再回退到 su
if [ "$(id -u)" -ne 0 ]; then
    if command -v su2 >/dev/null 2>&1; then
        exec su2 -c "$0 $*"
    fi
    if command -v su >/dev/null 2>&1; then
        exec su -c "$0 $*"
    fi
    echo "需要 Root 权限才能停止 mysekaiproxy"
    exit 1
fi

ROOT_DIR="/data/local/tmp/mysekaiproxy"
PID_PATH="$ROOT_DIR/mysekaiproxy.pid"
BIN_PATH="$ROOT_DIR/mysekaiproxy-android-arm64"
CONFIG_PATH="$ROOT_DIR/config-android.yaml"
SYSTEM_CACERTS_DIR="/system/etc/security/cacerts"

if clear_http_proxy; then
    echo "[1/3] 清理系统 HTTP 代理 ... OK"
else
    echo "[1/3] 自动清理系统 HTTP 代理失败，请手动检查代理设置"
fi

if [ -f "$BIN_PATH" ] && [ -f "$CONFIG_PATH" ]; then
    HASH_NAME="$($BIN_PATH ca-install-info --config "$CONFIG_PATH" 2>/dev/null || true)"
    if [ -n "$HASH_NAME" ] && [ -f "$SYSTEM_CACERTS_DIR/$HASH_NAME" ]; then
        mount -o remount,rw /system >/dev/null 2>&1 || true
        rm -f "$SYSTEM_CACERTS_DIR/$HASH_NAME"
    fi
fi

# 先通过 pkill 停止进程（与 HarukiProxy 行为一致）
if command -v pkill >/dev/null 2>&1; then
    pkill -l 2 "mysekaiproxy" 2>/dev/null || true
fi

# 再通过 PID 文件停止（兼容后台模式残留）
if [ -f "$PID_PATH" ]; then
    PID="$(cat "$PID_PATH")"
    if [ -n "$PID" ]; then
        kill "$PID" >/dev/null 2>&1 || true
    fi
    rm -f "$PID_PATH"
fi

echo "[2/3] 停止 MySekaiProxy-Android 进程 ... OK"
echo "[3/3] 完成"
