#! /bin/bash
red='\033[31m'
green='\033[32m'
yellow='\033[33m'
plain='\033[0m'
#获取版本号
edition=$(wget --no-check-certificate -qO- -t2 -T3 https://api.github.com/repos/TeaOSLab/EdgeAdmin/releases/latest | grep "tag_name" | head -n 1 | awk -F ":" '{print $2}' | sed 's/\"//g;s/,//g;s/ //g')
echo -e "${green}#########################################"
echo -e "${green}#                                       #"
echo -e "${green}#         GoEdge一键部署脚本            #"
echo -e "${green}#           此脚本由HIM维护             #"
echo -e "${green}#           此脚本仅用于学习            #"
echo -e "${green}#                                       #"
echo -e "${green}#########################################${plain}"
if [[ -f /etc/redhat-release ]]; then
  release="centos"
elif cat /etc/issue | grep -Eqi "debian"; then
  release="debian"
elif cat /etc/issue | grep -Eqi "ubuntu"; then
  release="ubuntu"
elif cat /etc/issue | grep -Eqi "centos|red hat|redhat"; then
  release="centos"
elif cat /proc/version | grep -Eqi "debian"; then
  release="debian"
elif cat /proc/version | grep -Eqi "ubuntu"; then
  release="ubuntu"
elif cat /proc/version | grep -Eqi "centos|red hat|redhat"; then
  release="centos"
else
  echo -e "${red}未检测到系统版本，请联系脚本作者！${plain}\n" && exit 1
fi
if [ "$release" == "centos" ]; then
yum -y install wget curl git unzip
else
apt-get -y install wget curl git unzip
fi
#判断CPU架构
if arch | grep -Eqi "aarch64"; then
arch_edition="arm64"
elif arch | grep -Eqi "x86_64"; then
arch_edition="amd64"
fi
echo -e "${green}开始下载安装包...${plain}"
wget -N https://dl.goedge.cn/edge/$edition/edge-admin-linux-$arch_edition-plus-$edition.zip
echo -e "${green}解压安装包...${plain}"
unzip -o edge-admin-linux-$arch_edition-plus-$edition.zip
cd edge-admin/
echo -e "${green}正在启动GoEdge...${plain}"
bin/edge-admin start
count=`ps -ef | grep edge-admin | grep -v "grep" | grep -v "xxx_daemon.sh" | wc -l`
if [ 0 -eq $count ]
then
echo -e "${red}启动失败！${plain}"
read -p "是否输出日志 [Y/N]：" echo_logs
if [ "$echo_logs" == "Y" ] || [ "$echo_logs" == "y" ]; then
cat ./edge-admin/logs/run.log
fi
else
#获取本机ip
local_ip=$(curl "ifconfig.me")
echo -e "${yellow}请访问 ${red}http://${local_ip}:7788 ${yellow}访问管理平台
如果你的服务器上已经设置了防火墙，需要在防火墙设置 7788 这个端口是通过的${plain}"
fi