#!/bin/bash

uid=$(id -u)
if [[ "$uid" != 0 ]];then
    echo -ne "请使用root用户操作!\n"
    echo -ne "I need root account!\n"
    exit 1                                                                     
fi


# 获取系统类型信息
id_like=$(grep 'ID_LIKE' /etc/os-release)
os_id=$(grep '^ID=' /etc/os-release | awk -F '=' '{print $2}' | tr -d '\"')
is_huawei=$(grep "Huawei" /etc/os-release > /dev/null && echo "true"  || echo "false")
is_aliyun=$(grep "Alibaba" /etc/os-release > /dev/null && echo "true"  || echo "false")
is_euler=$(grep "openEuler" /etc/os-release > /dev/null && echo "true"  || echo "false")
is_anolis=$(grep "Anolis" /etc/os-release > /dev/null && echo "true"  || echo "false")

if [[ "$is_huawei" == "true" ]]; then
	dnf config-manager --add-repo https://repo.oepkgs.net/openeuler/rpm/openEuler-20.03-LTS-SP1/extras/x86_64/
    dnf clean all && dnf makecache
    dnf install -y git git-lfs --nogpgcheck
    rm -rf /etc/yum.repos.d/repo*.repo
    dnf clean all && dnf makecache
fi

# 根据系统类型设置os变量
if [[ "$id_like" =~ "rhel" || "$id_like" =~ "fedora" || "$id_like" =~ "centos" ]]; then
    os="centos"
elif [[ "$is_huawei" == "true" || "$is_aliyun" == "true" || "$is_euler" == "true" || "$is_anolis" == "true" ]]; then
    os="centos"
elif [[ "$os_id" == "debian" || "$id_like" =~ "debian" ]]; then
    os="debian"
else
    os="unknown"
fi

# 根据操作系统类型执行相应操作
if [[ "$os" == "centos" ]]; then
    # 在类CentOS系统上安装git并克隆、安装软件
    echo "在类CentOS8系统上安装MyFreeSWITCH..."
    if [ -e centos ];then
        echo -ne "请删除已经存在的centos目录\n"
        exit 1
    fi
    if [[ ! -e "centos/myfs.latest.so.centos8.bin"  ]];then
        yum install -y git git-lfs --nogpgcheck
        git clone https://cnb.cool/myfspbx/centos.git
    fi
    cd centos
    git lfs pull
    chmod 755 myfs.latest.so.centos8.bin && ./myfs.latest.so.centos8.bin install
elif [[ "$os" == "debian" ]]; then
    # 在Debian上安装git并克隆、安装软件
    echo "在类Debian系统上安装MyFreeSWITCH..."
    if [ -e debian ];then
        echo -ne "请删除已经存在的debian目录\n"
        exit 1
    fi

    if [[ ! -e "centos/myfs.latest.so.debian.bin"  ]];then
        apt update && apt install -y git git-lfs
        git clone https://cnb.cool/myfspbx/debian.git
    fi
    cd debian
    git lfs pull
    chmod 755 myfs.latest.so.debian.bin && ./myfs.latest.so.debian.bin install
else
    echo "未知的操作系统类型"
    exit 255
fi