#!/bin/bash

# install nginx
nginx -v
if [ $? -ne 0 ]; then
    tar xzvf nginx.tgz
    if [ $? -eq 0 ]; then
        yum localinstall --disablerepo='*' nginx/*.rpm -y
        systemctl enable nginx && systemctl start nginx
        if [ $? -ne 0 ]; then
            echo [E] install nginx failed: $? > log.log
        else
            echo [I] install nginx ok > log.log
        fi
    else
        echo [E] unzip nginx failed: $? > log.log
    fi
else
    echo [I] nginx exist > log.log
fi

# install mosquitto
mosquitto -h | grep version
if [ $? -ne 0 ]; then
    tar xzvf mosquitto.tgz
    if [ $? -eq 0 ]; then
        yum localinstall --disablerepo='*' mosquitto/*.rpm -y
        systemctl enable mosquitto && firewall-cmd --add-port=9883/tcp --add-port=9001/tcp --add-port=7000/tcp --permanent --zone=public && firewall-cmd --reload && mosquitto -h | grep version
        if [ $? -ne 0 ]; then
            echo [E] install mosquitto failed: $? >> log.log
        else
            echo [I] install mosquitto ok >> log.log
            cp -f mosquitto.conf /etc/mosquitto/mosquitto.conf
            systemctl restart mosquitto
        fi
    else
        echo [E] unzip mosquitto failed: $? >> log.log
    fi
else
    echo [I] mosquitto exist >> log.log
fi

# install jdk17
if [ ! -d "/opt/jdk-17.0.9+9" ]; then
    tar -xzvf OpenJDK17U-jdk_x64_linux_hotspot_17.0.9_9.tar.gz -C /opt
    if [ $? -eq 0 ]; then
        # grep JAVA_HOME /etc/environment
        # if [ $? -ne 0 ]; then
            # cp /etc/environment /etc/environment.bak
            # sed -i -e '/PATH/i\JAVA_HOME="/opt/jdk-17.0.9+9"' -e 's/"$/:$JAVA_HOME\/bin"/' /etc/environment
        # fi
        # source /etc/environment
        ln -s /opt/jdk-17.0.9+9/bin/java /usr/bin/java
        java -version
        if [ $? -ne 0 ]; then
            echo [E] install jdk17 failed: $? >> log.log
        else
            echo [I] install jdk17 ok >> log.log
        fi
    else
        echo [E] tar jdk17 failed: $? >> log.log
    fi
else
    echo [I] jdk17 exist >> log.log
fi

# install beepower
if [ ! -d "/usr/local/beepower" ]; then
    unzip beepower.zip -d /usr/local
    if [ $? -eq 0 ]; then
        chmod +x /usr/local/beepower/master/*.sh
        crontab -l|grep keepalive || crontab crontab.txt
        if [ $? -ne 0 ]; then
            echo [E] install beepower failed: $? >> log.log
        else
            echo [I] install beepower ok  >> log.log
        fi
    else
        echo [E] unzip beepower failed: $? >> log.log
    fi
else
    echo [I] beepower exist >> log.log
fi

# install youhaohudong
if [ ! -d "/usr/share/nginx/html/dist" ]; then
    unzip youhaohudong_1122.zip -d /usr/share/nginx/html
    if [ $? -eq 0 ]; then
        chmod -R 755 /usr/share/nginx/html/dist
        firewall-cmd --zone=public --add-service=http --permanent
        cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
        grep '/api/' /etc/nginx/nginx.conf || sed -i -e '/^\s*root/a\        location \/api\/ {\n            proxy_pass http:\/\/localhost:10083\/;\n        }' -e '/^\s*root/c\        root /usr/share/nginx/html/dist;' /etc/nginx/nginx.conf
        nginx -s reload
        if [ $? -ne 0 ]; then
            echo [E] install youhaohudong failed: $? >> log.log
        else
            echo [I] install youhaohudong ok >> log.log
        fi
    else
        echo [E] unzip youhaohudong failed: $? >> log.log
    fi
else
    echo [I] youhaohudong exist >> log.log
fi

# config static ip
# nmcli connection show
# echo -n "输入NAME名称: "
# read interface
# if [ -z "$interface" ]; then
    # nmcli device show | grep 'DEVICE\|CONNECTION'
    # echo -n "输入DEVICE名称: "
    # read device
    # if [ -z "$device" ]; then
        # echo [W] config static ip aborted >> log.log
    # else
        # nmcli connection add con-name '有线连接 1' ifname $device type ethernet ip4 192.168.8.8/24 gw4 192.168.8.1 ipv4.dns "192.168.8.1"
        # if [ $? -eq 0 ]; then
            # systemctl restart NetworkManager
            # echo [I] add static ip ok >> log.log
        # else
            # echo [E] add static ip failed >> log.log
        # fi
    # fi
# else
    # nmcli connection modify "$interface" ipv4.addresses 192.168.8.8/24 ipv4.gateway 192.168.8.1 ipv4.method manual ipv4.dns "192.168.8.1"
    # if [ $? -eq 0 ]; then
        # systemctl restart NetworkManager
        # echo [I] config static ip ok >> log.log
    # else
        # echo [E] config static ip failed >> log.log
    # fi
# fi


