#!/bin/bash
#
# Version: 0.5
# Auther : zhuanglei.chen@lingycloud.com

set -e

ensure_bash() {
    if [ "$BASH_VERSION" = "" ]; then
        echo "Should be run by /bin/bash"
        exit 1
    fi
}
ensure_bash


red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
plain='\033[0m'

upgrade=0
now=`date +"%Y%m%d%H%M%S"`
wd=`pwd`
hostname=`hostname`
download_ip=
version=


get_download_ip() {
	download_ip="inside-init-server.lingycloud.com"
    # local ips=(115.231.111.151 115.233.223.27 10.4.13.157)
    # #local ips=(115.231.112.254 115.233.222.254 10.4.13.254)
    # local ip

    # for ip in ${ips[@]}; do
        # local ret=0
        # ping -c 2 -i 0.5 -W 1 -q $ip >/dev/null || ret=$?

        # if [ $ret = 0 ]; then
            # download_ip=$ip
            # return 0
        # fi
    # done

    # echo -e "${red}${ips[@]} not reachable${plain}"
    # return 1
}


install_packages() {
    yum install -y lrzsz ipset dstat tree dnsmasq openssl
}


install_lingy_packages() {
    if [ ! -f "/opt/sdn_relay/conf/sdn_relay.conf" ]
    then
        mkdir -p /data/logs/sdn_relay

        cd /opt
        curl -k -vo sdn_relay_cloud.tgz "https://${download_ip}/static/cbox/sdn_relay_cloud.tgz"
        tar zxvf sdn_relay_cloud.tgz



        sed -i "s/sn-hostname/$hostname/" /opt/sdn_relay/conf/sdn_relay.conf

        cd $wd
    fi

    if [ ! -f "/opt/ngxstrm_v2/bin/nginx" ]
    then
        cd /opt
        curl -k -vo ngxstrm_v2_cloud.tgz "https://${download_ip}/static/cbox/ngxstrm_v2_cloud.tgz"
        tar zxvf ngxstrm_v2_cloud.tgz
        cd $wd
    fi
}


choose_version() {
    local versions=($(curl "https://${download_ip}/static/cbox/" -k -s -m 20 | grep '.tgz' | grep 'cloud-agent-' | cut -d'-' -f3 | cut -d'.' -f1,2,3 | sort -V))

    [ ${#versions[@]} -eq 0 ] && echo -e "${red}Error:${plain} get latest cloud-agent version failed." && return 1

    echo -e "${yellow}Availabe versions:${plain}"
    local ver
    for ver in ${versions[@]}; do
        echo -e ${green}${ver}${plain}
    done

    echo
    echo -e -n "Choose the version to install (default: ${green}${versions[-1]}${plain}): "
    read version

    if [ "$version" = "" ]; then
        version=${versions[-1]}
    fi

    local ok=0
    for ver in ${versions[@]}; do
        if [ $version = $ver ]; then
            ok=1
        fi
    done

    if [ $ok = 0 ]; then
        echo -e "${red}Wrong version \"$version\"${plain}"
        return 1
    fi
}


install_agent() {
    local A

    if [ -f /opt/cloud-agent/conf/cbox.conf ]; then
        echo -e -n "${yellow}cloud-agent already installed, upgrade? (Y/n):${plain} "
        read A
        case $A in
            Y) echo -e "${green}cloud-agent will be upgraded.${plain}"; upgrade=1
                ;;
            n) echo "Nothing to do, exit"; exit 0
                ;;
            *) echo -e "${red}Wrong choice \"$A\", exit${plain}"; exit 1
                ;;
        esac
    fi

    if [ $upgrade = 1 ]; then
        echo -e "${green}Stop the cloud-agent if running${plain} ..."
        /opt/cloud-agent/agent -s stop > /dev/null 2>&1 || true
        echo -e "${green}Move /opt/cloud-agent to /tmp/cloud-agent-${now}${plain} ..."
        mv /opt/cloud-agent /tmp/cloud-agent-$now
    fi

    cd /opt

    if [ ! -d /opt/python373 ]; then
        echo -e "${green}Download python373.tgz${plain} ..."
        echo
        curl "https://${download_ip}/static/cbox/python373.tgz" -k -o python373.tgz

        tar zxf python373.tgz
    fi

    echo -e "${green}Download cloud-agent-${version}.tgz${plain} ..."
    echo
    curl "https://inside-init-server.lingycloud.com/static/cbox/cloud-agent-1.tgz" -k -o cloud-agent-1.tgz

    tar zxf cloud-agent-1.tgz

    sed -i "/^sn =/c sn = $hostname" /opt/cloud-agent/conf/cbox.conf

    if [ $upgrade = 1 ]; then
        mv /opt/cloud-agent/conf/cbox.conf /opt/cloud-agent/conf/cbox.conf.default
        cp -f /tmp/cloud-agent-${now}/conf/cbox.conf /opt/cloud-agent/conf/cbox.conf
        cp -f /tmp/cloud-agent-${now}/logs/* /opt/cloud-agent/logs/ >/dev/null 2>&1 || true
    fi

    echo -e -n "${yellow}Edit cbox.conf? (y/n):${plain} "
    read A
    if [ "$A" = "y" ]; then
        vi /opt/cloud-agent/conf/cbox.conf
    fi

    echo -e -n "${yellow}Start the agent? (y/n):${plain} "
    read A
    if [ "$A" = "y" ]; then
        /opt/cloud-agent/agent -d
        echo -e "${yellow}Check the log to see if there are any thing wrong.${plain}"
        echo -e "${yellow}Press \"Ctrl + C\" to finish.${plain}"
        tail -f /opt/cloud-agent/logs/cbox.log | grep -v DEBUG
    fi
}


get_download_ip
install_packages
install_lingy_packages
choose_version
install_agent
