#!/bin/bash
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:$PATH

: '
Copyright (C) 2022 - now  HaoZi Technology Co., Ltd.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.
'

source <(curl -f -s --connect-timeout 10 --retry 3 https://dl.cdn.haozi.net/panel/public.sh)
if [ $? -ne 0 ]; then
    echo "下载 public.sh 失败,请检查网络或稍后重试。"
    echo "Download public.sh failed, please check the network or try again later."
    exit 1
fi

LOGO="+----------------------------------------------------\n| 耗子面板卸载脚本\n| Rat Panel uninstall script\n+----------------------------------------------------\n| Copyright © 2022-"$(date +%Y)" 耗子科技 All rights reserved.\n+----------------------------------------------------"

Prepare_System() {
    if [ $(whoami) != "root" ]; then
        error "请使用root用户运行卸载命令(Please run the uninstall command using the root user)"
    fi

    if [ ! -f "${setup_path}/panel/web" ]; then
        error "面板未安装,无需卸载(Panel is not installed, no need to uninstall)"
    fi

    if ! id -u "www" >/dev/null 2>&1; then
        groupadd www
        useradd -s /sbin/nologin -g www www
    fi
}

Remove_Swap() {
    swap_file="${setup_path}/swap"
    if [ -f "${swap_file}" ]; then
        swapoff ${swap_file}
        rm -f ${swap_file}
        sed -i '/swap/d' /etc/fstab
    fi

    mount -a
    if [ "$?" != "0" ]; then
        error "/etc/fstab 文件配置有误,请检查排除后重试,问题解决前勿重启系统(There is an error in the /etc/fstab file configuration, please check and try again after excluding, do not restart the system until the problem is resolved)"
    fi
}

Remove_Panel() {
    systemctl stop panel
    systemctl disable panel
    rm -f /etc/systemd/system/panel.service
    rm -f /usr/local/sbin/panel-cli
    rm -rf /usr/local/etc/panel
    rm -rf ${setup_path}
}

clear
echo -e "${LOGO}"

# 卸载确认
echo "高危操作,卸载面板前请务必备份好所有数据,提前卸载面板所有插件!"
echo "High-risk operation, please be sure to back up all data before uninstalling the panel, and uninstall all plugins in advance!"
echo -e $HR
echo "卸载面板后,所有数据(包括安装目录)将被清空,无法恢复!"
echo "After uninstalling the panel, all data (including the installation directory) will be cleared and cannot be recovered!"
echo -e $HR
echo "安全起见,请先等待 60 秒方可继续..."
echo "For safety reasons, please wait 60 seconds before proceeding..."
for i in {60..1}; do
    echo -ne "\r等待: $i 秒 (Waiting: $i seconds) "
    sleep 1
done

echo -e "\n"

read -r -p "输入 y 并回车以确认卸载面板 (Enter 'y' to confirm uninstallation): " uninstall
if [ "${uninstall}" != 'y' ]; then
    echo "输入不正确,已退出卸载。"
    exit
fi

echo -e "${LOGO}"
echo '正在卸载面板...'
echo 'Uninstalling the panel...'
echo -e $HR

Prepare_System
Remove_Swap
Remove_Panel

clear

echo -e "${LOGO}"
echo '面板卸载完成。'
echo 'Panel uninstalled successfully.'
echo '感谢您的使用,欢迎您再次使用面板。'
echo 'Thank you for using, welcome to use the panel again.'
echo -e $HR

rm -f uninstall.sh