#!/bin/bash



# 安装telnet（用来连接虚机）
function update_telnet() {
    # wget http://tw15b0178.onething.net:9090/tool/superV/byte_file/telnet-0.17-66.el7.x86_64.rpm -O /tmp/telnet-0.17-66.el7.x86_64.rpm && rpm -i /tmp/telnet-0.17-66.el7.x86_64.rpm
    URL="http://tw15b0178.onething.net:9090/tool/superV/byte_file/telnet-0.17-66.el7.x86_64.rpm"
    DEST="/tmp/telnet-0.17-66.el7.x86_64.rpm"
    RETRIES=5
    
    if ! command -v telnet &>/dev/null;then     # 判断命令是否存在，不存在则为真
        for i in $(seq 1 "$RETRIES");           # 循环5次
        do
            wget -q --timeout=15 -O "$DEST" "$URL"
            if [ -s "$DEST" ];then              # 判断下载的文件是否为空，不为空则为真
                rpm -U "$DEST" >/dev/null 2>&1
                break                           # 完成安装退出
            fi
        done
    fi
    
    # 无限循环版本
    # until rm -f "$DEST" \
    #   && wget -q --timeout=15 -O "$DEST" "$URL" \
    #   && [ -s "$DEST" ] \
    #   && rpm -qp "$DEST" >/dev/null 2>&1; 
    # do
    #   sleep 2
    # done
    # 
    # rpm -U "$DEST" >/dev/null 2>&1
}

# 安装jq（后续判断要用，采用无限循环安装）
function update_jq() {
    URL="http://tw15b0178.onething.net:9090/tool/superV/byte_file/jq-linux64"
    DEST="/tmp/jq-linux64"
    RETRIES=5
    
    if ! command -v jq &>/dev/null;then                 # 判断命令是否存在，不存在则为真
        while true;                                     # 无限循环，关乎后续升级判断
        do
            wget -q --timeout=15 -O "$DEST" "$URL"
            if [ -s "$DEST" ];then                      # 判断下载的文件是否为空，不为空则为真，为空直接进入下一次循环
                chmod +x /tmp/jq-linux64
                cp -a /tmp/jq-linux64 /usr/local/bin/jq
                break                                   # 完成安装退出
            fi
        done
    fi
}

# 升级高版本拨号插件（低版本虚机会网络不通）
function update_plugin() {
    # xycall installurl "http://tw06d0006.onething.net/czx/virtualrouter/plugin-virtualrouter_5.1.3_xybk.ipk"
    # xycall installurl "http://tw06d0006.onething.net/czx/virtualrouter/plugin-virtualrouter_5.1.3_xykj.ipk"
    local_version=$(xycall info plugin-virtualrouter | jq -r ."local_version" | cut -d. -f1)
    
    if [ $local_version -lt 5 ];then
        if [ "$TAG" == "xybk" ];then
            xycall installurl "http://tw06d0006.onething.net/czx/virtualrouter/plugin-virtualrouter_5.1.3_xybk.ipk"
        else
            xycall installurl "http://tw06d0006.onething.net/czx/virtualrouter/plugin-virtualrouter_5.1.3_xykj.ipk"
        fi
    fi
}

# 更新xt_MASQUERADE内核模块（内核全锥模块）
function update_xt_MASQUERADE() {
    URL="http://tw15b0178.onething.net:9090/tool/superV/byte_file/xt_MASQUERADE.ko.xz"
    DEST="/tmp/xt_MASQUERADE.ko.xz"
    
    if [ $(modinfo xt_MASQUERADE | grep "Zhixing Chen" | wc -l) -eq 0 ];then
        while true;
        do
            wget -q --timeout=15 -O "$DEST" "$URL"
            if [ -s "$DEST" ];then
                mv -f "$DEST" /lib/modules/5.10.25-1.3.1/kernel/net/netfilter/xt_MASQUERADE.ko.xz && sync; iptables -t nat -F; ip6tables -t nat -F; rmmod xt_MASQUERADE; modprobe xt_MASQUERADE
                if [ $(modinfo xt_MASQUERADE | grep "Zhixing Chen" | wc -l) -ne 0 ];then
                    break
                fi
            fi
        done
    fi
}

function main() {
    # 基础工具安装
    update_telnet
    update_jq

    # 检查操作系统
    if [ -f /etc/xyapp/.sysname ];then
        TAG=$(cat /etc/xyapp/.sysname)
    else
        TAG=xybk
    fi
    
    if [ "${TAG}" == "xr" ];then
        TAG=xykj
    elif [ "${TAG}" == "XRVD" ];then
        TAG=xykj
    elif [ "${TAG}" != "xybk" ];then
        TAG=xykj
    fi
    
    # 升级插件（）
    update_plugin
    update_xt_MASQUERADE
    echo -n "ok"
}

main