#!/bin/bash
# 判断本机的CPU架构和操作系统版本
cpu_arch=$(uname -m)
os_version=$(cat /etc/os-release | grep PRETTY_NAME | cut -d '"' -f 2)
type=$(curl http://metadata.tencentyun.com/latest/meta-data/instance/instance-type)
NAIXI_VERSION="2024-09-28"
echo -e '## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##'
echo -e '#              Naixi-Uninstall-QCloud-Agent            #'
echo -e '#                     '$NAIXI_VERSION'                     #'
echo -e '#     https://dl.naixi.net/tools/qcloud/uninstall.sh   #'
echo -e '## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##'
echo -e
echo -e 'If you are using OrcaTerm as lighthouse user, please try <sudo -i> and run again!!!'
echo -e 'Do not worry, cloud-init still in your machine, this script can not effect to QCloud IPv6 function.'
echo -e 'Uninstall QCloud YunJing and monitor services...'
bash /usr/local/qcloud/stargate/admin/uninstall.sh
bash /usr/local/qcloud/YunJing/uninst.sh
bash /usr/local/qcloud/monitor/barad/admin/uninstall.sh
echo -e 'Uninstall QCloud services done.'
echo -e '## ## ## ## Check QCloud process ## ## ## ##'
ps -A | grep agent
echo -e '## ## ## ## ## ## ## ## ## ## ## #### ## ## #### ## ## ##'
# update apt and yum
if [[ "$os_version" == *"Ubuntu"* || "$os_version" == *"Debian"* ]]; then
    echo "Updating for Debian/Ubuntu..."
    apt-get update -y
    apt-get install unzip curl wget -y
elif [[ "$os_version" == *"CentOS"* || "$os_version" == *"Red Hat"* || "$os_version" == *"Fedora"* ]]; then
    echo "Updating for CentOS/Red Hat/Fedora..."
    yum update -y
    yum install unzip curl wget -y
else
    echo "System is not support!"
    exit 1
fi
echo -e 'Clean QCloud process...'
curl -sL https://dl.naixi.net/tools/qcloud/kill-process.sh | bash
echo -e 'Check again...'
echo -e '## ## ## ## make sure there nothing to input ## ## ## ##'
ps -A | grep agent
echo -e '## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##'
echo -e "# Type：$type"
echo -e "# CPU Arch：$cpu_arch"
echo -e "# System：$os_version"
echo -e '## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##'
echo -e 'This script will not remove your TAT automation assistant.'
echo -e 'If you need to completely remove it, please use uninstall-all.sh instead of uninstall.sh'
echo -e 'Done!'