#!/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'，标准格式为./srv_ubuntu.sh install 1.5.25 或者 ./srv_ubuntu.sh reinstall 1.5.25"
        fi
}

version(){
        ubuntu_version=$(lsb_release -rs)
        if [ "$ubuntu_version" = "22.04" ]; then
                ubuntu_22.04
        else
                update=false
        fi
}

# 清华镜像源

ubuntu_22.04(){
        # 1. 阿里云源 (Ubuntu 22.04 Jammy) - 首选源，确保系统依赖兼容
        cat > /etc/apt/sources.list <<EOF
deb https://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
EOF
        
        # 2. 清华源 (Ubuntu 20.04 Focal) - 备选源 (保留原代码中的源作为备份)
        echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse" >> /etc/apt/sources.list
        echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse" >> /etc/apt/sources.list
        echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse" >> /etc/apt/sources.list
        echo "deb http://security.ubuntu.com/ubuntu/ focal-security main restricted universe multiverse" >> /etc/apt/sources.list

        # 3. MongoDB 4.4 源
        echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" >> /etc/apt/sources.list
}

config(){
    if [ $ubuntu_version = "22.04" ]
    then           
        systemctl enable mongod.service
        systemctl start mongod
    fi
    echo '佳石服务器部署完成！'
}

result(){
        if [ $? -eq 0 ]
        then
                echo 'jsserver install successfull!'
                config
        else
                echo 'jsserver install failure!'
        fi
}

install(){
        cp /etc/apt/sources.list /etc/apt/sources.list.back 
        version
        if [ $update = true ]
        then
                rm jiasion.gpg-key* -rf
                wget http://139.155.232.120/jiasion.gpg-key.asc && sudo apt-key add jiasion.gpg-key.asc
                curl -fsSL https://pgp.mongodb.com/server-4.4.asc | sudo apt-key add -
                # jiasion镜像源
                echo "deb http://139.155.232.120/mirror ./ " >>/etc/apt/sources.list
                touch /etc/apt/apt.conf.d/99verify-peer.conf
                echo > /etc/apt/apt.conf.d/99verify-peer.conf "Acquire { https::Verify-Peer false }"
                apt-get install --reinstall ca-certificates
                dpkg --configure -a
                apt-get update -y
                wget http://download.jiasion.cn/mirror/libssl1.1_1.1.1-1ubuntu2.1~18.04.23_amd64.deb
                sudo dpkg -i libssl1.1_1.1.1-1ubuntu2.1~18.04.23_amd64.deb 
                
                # 安装依赖
                apt-get install -y mosquitto ffmpeg gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-libav gstreamer1.0-x gstreamer1.0-alsa gstreamer1.0-gl gstreamer1.0-gtk3 gstreamer1.0-qt5 gstreamer1.0-pulseaudio python3-pip python3-websockify ntp pkg-config libopus-dev libopusfile-dev
                
                # sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3B4FE6ACC0B21F32
                if [ -z "${version}" ]
                then    
                        apt-get -y install jsserver
                        result
                else
                        apt-get -y install jsserver=${version}
                        result
                fi  
                sudo cp /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime
                echo '时区配置完成！'
                systemctl restart jsserver

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

reinstall(){
    if [ -z "${version}" ]
    then    
            apt-get -y install jsserver
            result
    else
            apt-get -y install jsserver=${version}
            result
    fi  
}

start
