#! /bin/bash
while getopts :e:c:s:v: ARGS  
do  
case $ARGS in   
    e)  
        get_endpoints="${OPTARG}"
        echo -e "${get_endpoints}"
        ;;   
    c)  
        get_clusterId=$OPTARG
        echo -e "$OPTARG"
        ;;  
    s)  
        get_secret=$OPTARG
        echo -e "$OPTARG"
        ;;  
    v)  
        get_edition=$OPTARG
        echo -e "$OPTARG"
        ;;
    *)  
        echo "未知选项：$ARGS"
        ;;
esac
done
red='\033[31m'
green='\033[32m'
yellow='\033[33m'
plain='\033[0m'
#获取版本号
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
wget -N https://dl.goedge.cn/edge-node/$get_edition/edge-node-linux-$arch_edition-plus-$get_edition.zip
unzip edge-node-linux-$arch_edition-plus-$get_edition.zip
cd edge-node/configs
echo -e "rpc:
  endpoints: [ "${get_endpoints}" ]
clusterId: "${get_clusterId}"
secret: "${get_secret}"" > cluster.yaml
cd ..
bin/edge-node start
count=`ps -ef | grep edge-node | 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-node/logs/run.log
fi
else
echo -e "${green}启动成功${plain}"
fi