#!/bin/sh
# Official CLI/service removal. Saved account, identity and settings are retained.
set -eu
[ "$#" -eq 0 ] || { echo 'Usage: uninstall.sh (saved settings are retained)' >&2; exit 1; }
if [ "$(id -u)" -ne 0 ]; then
    # With `curl ... | sh`, $0 names the interpreter, not this script.
    case "$0" in
        sh|*/sh|dash|*/dash|bash|*/bash|-sh|-bash) ;;
        *) [ ! -f "$0" ] || exec sudo /bin/sh "$0";;
    esac
    command -v curl >/dev/null || { echo 'Install curl and CA certificates first.' >&2; exit 1; }
    umask 077
    work=$(mktemp -d /tmp/wangyunlian-uninstall.XXXXXX)
    trap 'rm -rf "$work"' EXIT
    trap 'exit 1' HUP INT TERM
    curl --fail --show-error --location --proto '=https' --tlsv1.2 \
        https://wangyunlian.com/uninstall.sh -o "$work/uninstall.sh"
    sudo /bin/sh "$work/uninstall.sh"
    exit $?
fi

remove_linux_service() {
    unit=$1
    binary=$2
    file=/etc/systemd/system/$unit.service
    if [ -f "$file" ]; then
        grep -Eq "^ExecStart=$binary([[:space:]]|$)" "$file" || {
            echo "Custom service: $file. Review its paths before manual removal." >&2; exit 1;
        }
        systemctl disable --now "$unit.service"
        rm -f "$file"
    elif [ "$(systemctl show "$unit.service" -p LoadState --value 2>/dev/null || :)" = loaded ]; then
        echo "Custom service location for $unit. Use the manual uninstall guide." >&2; exit 1
    fi
}

case "$(uname -s)" in
    Darwin)
        lock=/var/run/wangyunlian-install.lock
        mkdir "$lock" 2>/dev/null || { echo 'Another installation is in progress.' >&2; exit 1; }
        trap 'rmdir "$lock"' EXIT
        for label in com.too1.wangyunlian.agent com.too1.onevpn.agent; do
            if launchctl print "system/$label" >/dev/null 2>&1; then launchctl bootout "system/$label"; fi
            rm -f "/Library/LaunchDaemons/$label.plist"
        done
        rm -f /usr/local/bin/wangyunlian /usr/local/bin/wangyunlian-agent /usr/local/bin/onevpnctl
        rm -f '/Library/Application Support/WangYunLian/bin/wangyunlian' \
            '/Library/Application Support/WangYunLian/bin/wangyunlian-agent' \
            '/Library/Application Support/OneVPN/bin/onevpn-agent' \
            '/Library/Application Support/OneVPN/bin/run-agent.sh'
        ;;
    Linux)
        if [ -f /etc/openwrt_release ]; then
            for name in wangyunlian onevpn; do
                if [ -f "/etc/init.d/$name" ]; then
                    grep -Eq '/usr/sbin/(wangyunlian-agent|onevpn-agent)' "/etc/init.d/$name" || {
                        echo "Custom service: /etc/init.d/$name. Review it before removal." >&2; exit 1;
                    }
                    "/etc/init.d/$name" stop
                    "/etc/init.d/$name" disable
                fi
            done
            if command -v opkg >/dev/null 2>&1; then
                umask 077
                work=$(mktemp -d /tmp/wangyunlian-uninstall-config.XXXXXX)
                trap 'if [ -f "$work/config" ]; then cp -p "$work/config" /etc/config/onevpn || { echo "Config backup retained: $work/config" >&2; exit 1; }; fi; rm -rf "$work"' EXIT
                trap 'exit 1' HUP INT TERM
                if [ -f /etc/config/onevpn ]; then cp -p /etc/config/onevpn "$work/config"; fi
                packages=$(opkg list-installed)
                for name in wangyunlian onevpn; do
                    if printf '%s\n' "$packages" | grep -q "^$name "; then opkg remove "$name"; fi
                done
                if [ -f "$work/config" ]; then cp -p "$work/config" /etc/config/onevpn; fi
            fi
            rm -f /etc/init.d/wangyunlian /etc/init.d/onevpn
        else
            command -v systemctl >/dev/null 2>&1 || { echo 'Use the manual guide for non-systemd Linux.' >&2; exit 1; }
            remove_linux_service wangyunlian-agent /usr/sbin/wangyunlian-agent
            remove_linux_service onevpn-agent /usr/sbin/onevpn-agent
            systemctl daemon-reload
        fi
        rm -f /usr/sbin/wangyunlian /usr/sbin/wangyunlian-agent /usr/sbin/onevpnctl /usr/sbin/onevpn-agent
        ;;
    *) echo 'Supported systems: macOS, Linux and OpenWrt.' >&2; exit 1;;
esac
echo '网云连命令行与后台服务已移除，账号、设备身份和配置已保留。'
echo '不再使用的设备请在控制台逐网移除：https://wangyunlian.com/console/'
