#!/bin/bash

type=$1
version=$2
update=true

_start(){
        if [[ $type = "install" ]]
        then
            echo "开始安装佳石服务"
            _install
        elif  [[ $type = "reinstall" ]]
        then
            echo "开始安装佳石服务"
            _reinstall   
        else
            echo "参数1携带格式错误，只支持install和reinstall，标准格式为./install_ubuntu.sh install 1.7.65.202511130936 或者 ./install_ubuntu.sh reinstall 1.7.65.202511130936"
        fi
}

_version(){
        openeuler_version=$(source /etc/os-release; echo $VERSION_ID)
        echo "${openeuler_version}"
        if [ $openeuler_version = "24.03" ]
        then
                _openeuler_24.03
        elif [ $openeuler_version = "22.03" ]
        then
                _openeuler_22.03
        else
                update=false
        fi
}

_openeuler_22.03(){
        rm -rf jiasion.gpg-key*
        wget http://dev.jiasion.cn:9081/jiasion.gpg-key.gpg
# jiasion镜像源
echo "[jiasion]
name=Jiasion Mirror Repository
baseurl=http://dev.jiasion.cn:9081
gpgcheck=1
enabled=1
gpgkey=jiasion.gpg-key.gpg" > /etc/yum.repos.d/jiasion.repo          
}

_openeuler_24.03(){
        rm -rf jiasion.gpg-key*
        wget http://dev.jiasion.cn:9080/jiasion.gpg-key.gpg
# jiasion镜像源
echo "[jiasion]
name=Jiasion Mirror Repository
baseurl=http://dev.jiasion.cn:9080
gpgcheck=1
enabled=1
gpgkey=jiasion.gpg-key.gpg" > /etc/yum.repos.d/jiasion.repo          
}

_config(){
    if [ $openeuler_version = "24.03" -o $openeuler_version = "22.03" ]
    then        
        systemctl enable mongod.service
        systemctl start mongod
    fi
    #解决ntp报错
    dnf -y install ntp
    systemctl enable ntpd.service

    #解决中控时间同步问题
    systemctl stop chronyd
    systemctl disable chronyd
    sed -i 's/restrict default nomodify notrap nopeer noepeer noquery/restrict default nomodify notrap nopeer/' /etc/ntp.conf
    sed -i 's/restrict source nomodify notrap noepeer noquery/restrict source nomodify notrap/' /etc/ntp.conf
    if ! grep -q "server 127.127.1.0 prefer" /etc/ntp.conf; then
        echo "interface listen 0.0.0.0" >> /etc/ntp.conf
        echo "server 127.127.1.0 prefer" >> /etc/ntp.conf
        echo "fudge 127.127.1.0 stratum 10" >> /etc/ntp.conf
    fi

    systemctl start ntpd
    #mqtt服务启动失败问题
    #配置文件指定了 PID 文件路径 /run/mosquitto/mosquitto.pid，但是这个目录不存在或者权限不正确。
    mkdir -p /run/mosquitto
    chown mosquitto:mosquitto /run/mosquitto   
    chmod 755 /run/mosquitto
    #由于 /run 是 tmpfs，需要确保系统启动时创建目录
cat > /etc/tmpfiles.d/mosquitto.conf << 'EOF'
d /run/mosquitto 0755 mosquitto mosquitto -
EOF
    # 应用配置
    systemd-tmpfiles --create /etc/tmpfiles.d/mosquitto.conf
    #mqtt运行报错问题：无法打开日志文件 /var/log/mosquitto/mosquitto.log 进行写入
    mkdir -p /var/log/mosquitto
    chown -R mosquitto:mosquitto /var/log/mosquitto
    chmod 755 /var/log/mosquitto
    #重启服务
    systemctl restart mosquitto

    #解决"/etc/nginx/sites-enabled/jiasion.site.conf" failed (13: Permission denied)报错(SELinux 策略限制,/etc/nginx/sites-enabled/jiasion.site.conf软连接导致)
    systemctl stop nginx
    rm -rf /etc/nginx/sites-enabled/jiasion.site.conf 
    cp -Z /home/jsserver/jiasion.site.conf /etc/nginx/sites-enabled/
    # 应用上下文更改
    restorecon  -F -Rv /etc/nginx/
    systemctl enable nginx
    systemctl restart nginx
    systemctl restart jsserver
    echo "佳石服务器部署完成！"
}

_result(){
        if [ $? -eq 0 ]
        then
                echo "jsserver install successfull!"
                _config
        else
                echo "jsserver install failure!"
        fi
}

_install(){
        systemctl stop firewalld
        systemctl disable firewalld

        dnf update -y
        _version
        if [ $update = true ]
        then
        
# 设置换镜像源
sed -e 's|^metalink=|#metalink=|g' \
-e 's|^baseurl=http://repo.openeuler.org/|baseurl=https://mirrors.aliyun.com/openeuler/|g' \
-e 's|^gpgkey=http://repo.openeuler.org/|gpgkey=https://mirrors.aliyun.com/openeuler/|g' \
-i.bak /etc/yum.repos.d/openEuler.repo

# mongodb镜像源
echo "[mongodb-org-4.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/8/mongodb-org/4.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc" > /etc/yum.repos.d/mongodb-org-4.repo

                dnf clean all
                dnf makecache
                dnf -y install gtk2 mongodb-org-server-4.4.29 mongodb-org-shell-4.4.29 mongodb-database-tools cpio
                if [ "${version}" = "" ]
                then    
                        dnf -y install jsserver
                else
                        dnf -y install jsserver=${version}
                fi 
                _result 
                sudo cp /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime
                echo "时区配置完成！"
                systemctl restart jsserver

# 替换后可再次查看时间
        elif [ $update = false ]
        then
                echo -e "系统不兼容，不支持安装 \n***目前openEuler系统只支持openEuler22.03和openEuler24.03***"
        fi
}

_reinstall(){
    systemctl stop jsserver  || true
    dnf remove -y jsserver  || true
    _version
    if [ $update = true ]
    then
        echo "开始重新安装 jsserver..."
        dnf makecache
        if [ "${version}" = "" ]
        then    
            dnf -y install jsserver
        else
            dnf -y install jsserver=${version}
        fi
        _result
    elif [ $update = false ]
    then
        echo -e "系统不兼容，不支持安装 \n***目前openEuler系统只支持openEuler22.03和openEuler24.03***"
    fi
}

_start


