#!/bin/bash

wiwo_config="$BASEDIR/configs/wiwo"

_install() {

    rm -rf /root/wiwo
    if [[ $ARCH == 'x86_64' ]];then
        # cp wiwo_conf.tgz (wiwo/bin/*, wiwo/conf/*) to /root/wiwo
        tar xzvf $wiwo_config/wiwo_conf.tgz -C /root/ > /dev/null 2>&1

        # install wiwo libmessage
        local wiwo_dir='/root/wiwo/libs'
        if [ ! -d $wiwo_dir ]; then
            mkdir -p $wiwo_dir
        fi

        tar xvf $wiwo_config/wiwo_lib/libmessage.tar.gz -C $wiwo_dir/ >/dev/null
        tar xvf $wiwo_config/wiwo_lib/libwiwo.tar.gz -C $wiwo_dir/ >/dev/null
        libmessage_file=`ls $wiwo_dir/libmessage.so.1.*|xargs basename`
        umessage_file=`ls $wiwo_dir/umessage.desc.1.*|xargs basename`
        libwiwo_file=`ls $wiwo_dir/libwiwo_common.so.1.*|xargs basename`

        ln -s $libmessage_file /root/wiwo/libs/libmessage.so
        ln -s $umessage_file /root/wiwo/libs/umessage.desc
        ln -s $libwiwo_file /root/wiwo/libs/libwiwo_common.so
        ln -s $libwiwo_file /root/wiwo/libs/libwiwo_common.so.1
    elif [[ $ARCH = 'aarch64' ]];then
        wget -P $BASEDIR -c http://softlib.service.ucloud.cn/usa/ucloud-basic-init/wiwo.aarch64.tar.gz > /dev/null 2>&1
        tar xf $BASEDIR/wiwo.aarch64.tar.gz -C /root/
    fi

    # 一些配置文件和脚本
    \cp -f $wiwo_config/ucloud.conf /etc/ld.so.conf.d/ucloud.conf
    \cp -f $wiwo_config/wiwolog_hourly_logrotate /etc/cron.hourly/
    \cp -f $wiwo_config/hourly_logrotate_wiwolog.conf /etc/
    \cp -f $wiwo_config/logrotate_wiwolog_rename.sh  /etc/
    \cp -f $wiwo_config/inst.sh  /root/

    # 安装依赖
    if [[ $OS == "CentOS6" ]];then

        # 引起依赖问题，导致 yum安装包失败
        rpm -e compat-mysql51-5.1.54-1.el6.remi.x86_64 mysql-5.5.35-1.el6.remi.x86_64 mysql-libs-5.5.35-1.el6.remi.x86_64 postfix > /dev/null 2>&1

        yum -q -y install mysql mysql-devel mysql-libs > /dev/null 2>&1
        tar xvf $wiwo_config/wiwo_lib/libmysqlclient.tar.gz -C /usr/lib64/mysql/ > /dev/null 2>&1

    elif [[ $OS == "CentOS7" ]];then
        yum -q -y install mariadb mariadb-devel mariadb-libs > /dev/null 2>&1
    fi

    # protobuf
    if [[ $ARCH == 'x86_64' ]];then
        yum -y -q install protobuf-2.4.1 protobuf-python > /dev/null 2>&1
        #    pip install -q $BASEDIR/packages/protobuf-2.5.0.tar.gz > /dev/null 2>&1
    elif [[ $ARCH = 'aarch64' ]];then
        yum -y -q install protobuf protobuf-python > /dev/null 2>&1
        wget -P $BASEDIR -c http://softlib.service.ucloud.cn/usa/ucloud-basic-init/libprotobuf.so.7.0.0.aarch64.tar.gz > /dev/null 2>&1
        tar xf $BASEDIR/libprotobuf.so.7.0.0.aarch64.tar.gz -C /usr/lib/
        rm -f /usr/lib/libprotobuf.so.7
        ln -s libprotobuf.so.7.0.0 /usr/lib/libprotobuf.so.7
    fi

    ldconfig > /dev/null 2>&1

    # 添加启动项
    cp -f $wiwo_config/wiwo /etc/init.d/wiwo
    if [[ $VMFLAG = 0 && $ARCH = 'x86_64' ]];then
        if [[ $Usage = udb ]];then
            sed -i "s&#UsageEnv&/root/udb/script/common/init_env.sh&" /etc/init.d/wiwo
        elif [[ $Usage = umem ]]; then
            sed -i "s&#UsageEnv&/root/umem/script/init_env.sh&" /etc/init.d/wiwo
        elif [[ $Usage = uhost ]]; then
            sed -i "s&#UsageEnv&/usr/bin/pm2 resurrect&" /etc/init.d/wiwo
        elif [[ $Usage = ufile && $SecUsage = s3-database ]]; then
            sed -i "s&#UsageEnv&/root/udb/script/common/init_env.sh&" /etc/init.d/wiwo
        elif [[ $Usage = unet && $SecUsage = ulb ]];then
            sed -i "s&#UsageEnv&/root/init_ulb.sh&" /etc/init.d/wiwo
        elif [[ $Usage = unet && $SecUsage = sdngateway ]]; then
            sed -i "s&#UsageEnv&/root/init_ulb.sh&" /etc/init.d/wiwo
        fi
    fi

    chmod +x /etc/init.d/wiwo
    chkconfig --add wiwo
    # 不起wiwo，防止被rc.local里的死循环卡住起不来，反正重启后要交付检测
    #service wiwo start > /dev/null 2>&1
    chkconfig wiwo on > /dev/null 2>&1

}

_check() {
    _check="PASS"
    if [[ ! -d /root/wiwo ]];then
        _check="FAIL"
        PRINT_ERR "[ERROR] Dir:/root/wiwo Not Found."
    fi
    if [[ $VMFLAG = 0 ]];then
        if [[ $(ps -ef |grep wiwo | grep -v grep | wc -l) -eq 0 ]]; then
            _check="FAIL"
            PRINT_ERR "[ERROR] wiwo Not Running."
        fi
    fi

    if [[ $(chkconfig 2>/dev/null|grep wiwo| grep -c on) -eq 0 ]]; then
        _check="FAIL"
        PRINT_ERR "[ERROR] wiwo Not Enabled In Chkconfig List"
    fi
}
