#!/bin/bash

OS=`grep "^ID=" /etc/os-release | cut -d '=' -f 2- | sed 's|"||g'`
function install_centos_dependency(){
    yum install -y jq
    yum install -y ntpdate 
}
function install_ubuntu_dependency(){
    apt install -y jq
    apt install -y ntpdate
}

function init_install(){
    
    wget http://download.openfogos.com/new.zip -O /tmp/new.zip
    unzip  /tmp/new.zip -d /root/
    mkdir -p /root/pear/
    cp -r /root/new/* /root/pear/ 
    chmod -R 755 /root/pear
    
    # cron 部分
    if [ -z "$(cat /etc/crontab | grep pear_cron.sh | grep -v '#')" ]; then 
        echo "* * * * * root /root/pear/cron/pear_cron.sh -1m" >> /etc/crontab
        echo "*/5 * * * * root /root/pear/cron/pear_cron.sh -5m" >> /etc/crontab
        echo "*/30 * * * * root /root/pear/cron/pear_cron.sh -30m" >> /etc/crontab
        echo "15 4 * * * root /root/pear/cronpear_cron.sh -1d" >> /etc/crontab
    fi

    # 部署网口状态监控脚本
    if [ "${OS}" = "centos" ]; then
        cp /root/pear/network/centos/ifup-local.sh /etc/NetworkManager/dispatcher.d/
    elif [ "${OS}" = "ubuntu" ]; then
        cp /root/pear/network/ubuntu/50-ifup-hooks.sh /etc/networkd-dispatcher/routable.d/
    else
        echo "未知系统"
    fi

    # 部署开机自启动
    cp /root/pear/init/pear_init /etc/init.d/pear_init
    cp /root/pear/service/pear.service /lib/systemd/system/pear.service
    systemctl daemon-reload 
    systemctl enable pear.service

    # 安装系统依赖
    if [ "${OS}" = "centos" ]; then
        install_centos_dependency
    elif [ "${OS}" = "ubuntu" ]; then
        install_ubuntu_dependency
    else
        echo "未知系统"
    fi
    echo ""
    echo "install finish!"
    echo ""
    read -s -n1 -p "Press any key to continue..."
}

function configure_network(){
    while true :
    do
        clear
        echo -e "\n------------------------------------------------------"
        echo -e "level: 3"
        echo "set ip rule:"
        echo ""
        echo -e "The current network connection type is \033[31m $(cat /root/pear/var/network_connection_type) \033[0m"
        echo ""
        echo -e "Type of network connection: "
        echo -e "\t 1) pppoe"
        echo -e "\t 2) static"
        echo -e "\t 3) dhcp"
        echo -e "\t 0) quit"
        read -p "Enter the operation Number (for example: 0) " choose

        case $choose in
            1 | pppoe )
                echo "pppoe" > /root/pear/var/network_connection_type
            ;;
            2 | static )
                echo "static" > /root/pear/var/network_connection_type
            ;;
            3 | dhcp )
                echo "dhcp" > /root/pear/var/network_connection_type
            ;;
            0 | quit | q)
                
            ;;
            ?)
                continue
            ;;
        esac
        echo ""
        echo -e "The current network connection type is \033[31m $(cat /root/pear/var/network_connection_type) \033[0m"
        echo ""
        read -s -n1 -p "Press any key to continue..."
        break
    done
}
function configure_storage(){
    clear
    echo -e "\n------------------------------------------------------"
    echo -e "level: 3"
    echo "configure storage:"
    /root/pear/storage/part.sh
    read -p "Please enter the argument:" argument
    /root/pear/storage/part.sh $argument

    echo ""
    echo "finish"
    echo ""
    echo "lsblk | grep -v loop | grep T "
    lsblk | grep -v loop | grep T
    echo ""
    read -s -n1 -p "Press any key to continue..."
}

function install_biz(){
    while true :
    do
        clear
        echo -e "\n------------------------------------------------------"
        echo -e "level: 3"
        echo "install_biz:"
        echo -e "The current service list: "
        echo -e "\t 1) qny"
        echo -e "\t 2) qnks"
        echo -e "\t 3) ks"
        echo -e "\t 4) yf"
        echo -e "\t 5) tencent_sl"
        echo -e "\t 0) quit"
        read -p "Enter the operation Number (for example: 0) " choose
        url=""
        case $choose in
            1 | qny )
                
            ;;
            2 | qnks )
                url="http://download.openfogos.com/test/fogvdn_PEAR_7002_X64_LINUX_579.tar.gz"
            ;;
            3 | ks )
                url="http://download.openfogos.com/test/fogvdn_PEAR_8001_X64_LINUX_575.tar.gz"
            ;;
            4 | yf )
                url="http://download.openfogos.com/test/fogvdn_PEAR_8002_X64_LINUX_570.tar.gz"
            ;;
            5 | tencent_sl )
                url="http://download.openfogos.com/test/fogvdn_PEAR_2608_X64_LINUX_568.tar.gz "
            ;;
            0 | quit | q)
                break;
            ;;
            ?)
                continue
            ;;
        esac

        # 可能已经存在
        /etc/init.d/xc_cdn.sh stop
        # 清理
        rm -rf /etc/pear/pear_monitor/*


        # 安装
        wget $url -O /tmp/fogvdn.tar.gz
        tar -C / -zxvf /tmp/fogvdn.tar.gz

        cp /root/pear/service/xc_cdn.service /lib/systemd/system/xc_cdn.service
        systemctl daemon-reload 
        systemctl enable xc_cdn.service # 开机自启动

        echo "install biz finish!"
        echo ""
        read -s -n1 -p "Press any key to continue..."
        echo ""
        break;
    done
}

function configure_biz_mac(){
    while :
    do
        clear
        echo -e "-------------------------------"
        echo -e "level: 4"
        echo -e "configure biz mac:"
        echo ""
        echo -e "The current conf:\033[31m $(cat /etc/pear/pear_monitor/mac.conf) \033[0m"
        ip -o a | grep -v "inet6" |grep -vE "(1:|docker0|virbr0|br-lan)" -m 10 | awk '{print $2 " " $4}'
        echo -e "estimate quantity:" $(ip -o a | grep -v "inet6" |grep -vE "(1:|docker0|virbr0|br-lan)" | wc -l)

        echo -e "-----------------------"
        echo ""
        echo -e "\t 1) automatic generated"
        echo -e "\t 2) manual"
        echo -e "\t 0) quit"
        echo ""
        read -p "Enter the operation Number (for example: 0) " choose
        echo ""
        case $choose in 
        1 )
            read -p "Please enter the number of generation:" number
            pear_monitor -d --auto_mac=$number
        ;;
        2 )
            read -p "Please enter the macs:" macs
            echo $macs > /etc/pear/pear_monitor/mac.conf
        ;;
        0 | q | quit)
            break;
        ;;
        ?)
            continue
        ;;
        esac
        echo ""
        echo -e "The current conf:\033[31m $(cat /etc/pear/pear_monitor/mac.conf) \033[0m"
        echo ""
        read -s -n1 -p "Press any key to continue..."
        break;
    done
}
function configure_biz_storage(){
    while :
    do
        clear
        echo -e "-------------------------------"
        echo -e "level: 4"
        echo -e "configure biz stroage:"
        echo ""
        echo -e "The current conf:\033[31m $(cat /etc/pear/pear_monitor/dev.conf)\033[0m"
        lsblk | grep -v loop | grep T
        echo -e "estimate quantity:" $(lsblk | grep -v loop | grep T | wc -l)
        
        echo -e "-----------------------"
        echo ""
        echo -e "\t 1) automatic generated"
        echo -e "\t 2) manual"
        echo -e "\t 0) quit"
        echo ""
        read -p "Enter the operation Number (for example: 0) " choose
        echo ""
        case $choose in 
        1 )
            read -p "Please enter the prefix:" prefix
            read -p "Please enter the start of range:" start
            read -p "Please enter the end of range:" end
            pear_monitor -d --auto_disk=$prefix,$start,$end
        ;;
        2 )
            read -p "Please enter the storage path:" storagePath
            echo $storagePath > /etc/pear/pear_monitor/dev.conf
        ;;
        0 | q | quit )
            break
        ;;
        ?)
            continue
        ;;
        esac
        echo ""
        echo -e "The current conf:\033[31m $(cat /etc/pear/pear_monitor/dev.conf)\033[0m"
        echo ""
        read -s -n1 -p "Press any key to continue..."
        break;
    done
}
function configure_biz_network(){
    while :
    do
        clear
        echo -e "-------------------------------"
        echo -e "level: 4"
        echo -e "configure biz nic:"
        echo ""
        echo -e "The current conf:\033[31m $(cat /etc/pear/pear_monitor/nic.conf) \033[0m"
        ip -o a | grep -v "inet6" |grep -vE "(1:|docker0|virbr0|br-lan)" -m 10 | awk '{print $2 " " $4}'
        echo -e "estimate quantity:" $(ip -o a | grep -v "inet6" |grep -vE "(1:|docker0|virbr0|br-lan)" | wc -l)

        echo -e "-----------------------"
        echo ""
        echo -e "\t 1) automatic generated"
        echo -e "\t 2) manual"
        echo -e "\t 0) quit"
        echo ""
        read -p "Enter the operation Number (for example: 0) " choose
        echo ""
        case $choose in 
        1 )
            read -p "Please enter the prefix:" prefix
            read -p "Please enter the start of range:" start
            read -p "Please enter the end of range:" end
        ;;
        2 )
            read -p "Please enter the nic:" nics
            echo $nics > /etc/pear/pear_monitor/nic.conf
        ;;
        0 | q | quit)
            break
        ;;
        ?)
            continue
        ;;
        esac
        echo ""
        echo -e "The current conf:\033[31m $(cat /etc/pear/pear_monitor/nic.conf) \033[0m"
        echo ""
        read -s -n1 -p "Press any key to continue..."
    done
}
# 配置业务
function configure_biz(){
    while true :
    do
        clear
        echo -e "\n------------------------------------------------------"
        echo -e "level: 3"
        echo "configure_biz:"
        echo ""
        echo -e "\t 1) configure mac"
        echo -e "\t 2) configure storage"
        echo -e "\t 3) configure network"
        echo -e "\t 0) quit"
        echo ""
        read -p "Enter the operation Number (for example: 0) " choose

        case $choose in
            1 )
                configure_biz_mac
            ;;
            2  )
                configure_biz_storage
            ;;
            3  )
               configure_biz_network
            ;;
            0 | quit | q)
                break;
            ;;
            ?)
                continue
            ;;
        esac
        # 安装
    done
}

function init(){
    
    while : 
    do
        clear
        echo -e "\n------------------------------------------------------"
        echo -e "level: 2"
        echo "init:"
        echo ""
        if [ ! -f "/root/pear/var/init" ]; then 
            echo -e "The current system is initialized"
        fi 
        if [ ! -f "/etc/pear/pear_scripts/scripts.sh" ];then
            echo -e "The current biz is $(cat /etc/pear/pear_scripts/scripts.sh | grep PLATFORM= | sed -e s/PLATFORM=//g)"
        fi
        
        echo ""
        echo -e "\t 1) init"
        echo -e "\t 2) install biz"
        echo -e "\t 0) quit"
        echo ""
        read -p "Enter the operation Number (for example: 0) " choose
        
        case $choose in 
        1)
            init_install
        ;;
        2)
            install_biz
        ;;
        0 | q | quit)
            return;
        ;;
        esac
    done
    echo ""
    read -s -n1 -p "Press any key to continue... "

}

function configure(){
    
    while : 
    do
        clear
        echo -e "\n------------------------------------------------------"
        echo -e "level: 2"
        echo "configure:"
        echo ""
        echo -e "\t 1) configure network"
        echo -e "\t 2) configure storage"
        echo -e "\t 3) configure biz"
        echo -e "\t 0) quit"
        echo ""
        read -p "Enter the operation Number (for example: 0) " choose
        
        case $choose in 
        1)
            configure_network
        ;;
        2)
            configure_storage
        ;;
        3)
            configure_biz
        ;;
        0 | q | quit)
            return;
        ;;
        esac
    done

}

function check(){
    
    clear
    echo -e "\n------------------------------------------------------"
    echo -e "level: 2"
    echo "check:"
    if [ -f /etc/pear/pear_monitor/info.yml ]; then 
        echo ""
        cat /etc/pear/pear_monitor/info.yml
        echo ""
    else
        echo ""
        echo -e "mac:\033[31m $(cat /etc/pear/pear_monitor/mac.conf)\033[0m"
        echo -e "dev:\033[31m $(cat /etc/pear/pear_monitor/dev.conf)\033[0m"
        echo -e "nic:\033[31m $(cat /etc/pear/pear_monitor/nic.conf)\033[0m"
        echo -e "pear_id:\033[31m $(cat/etc/pear/pear_monitor/pear_user_info.json | jq -r .i)\033[0m"
        echo ""
    fi

    read -s -n1 -p "Press any key to continue... "
}
mkdir -p /root/pear/var/

# 网络
while true :
do
    clear
    echo -e "\n--------------------------------------------"
    echo -e "level: 1"
    echo -e "Menu: "
    echo ""
    echo -e "\t 1) init"
    echo -e "\t 2) configure"
    echo -e "\t 3) check"
    echo -e "\t 4) restart biz"
    echo -e "\t 0) quit"
    echo ""
    read -p "Please enter the serial number:" choose
    case $choose in
        1 )
            # 配置 ip rule
            # TODO 改为配置network
            init
        ;;
        2 )
            # 配置网络
            configure
        ;;
        3 )
            # 配置业务
            check
        ;;
        4 ) 
            /etc/init.d/xc_cdn.sh restart 
        ;;
        0 | q | quit )
            exit 0;
        ;;
        ?)
        ;;
    esac
done 
