#!/bin/sh
# @function install QConf-Agent
function InstallQConfAgent()
{
    ShowResult "[green]正在安装QConf配置管理Agent... [/green]" 1

    local info_=${_HOSTS_[SCM]:-${_HOSTS_[DBCTRL]}}
    
    if [ -z "${info_}" ]; then
        return
    fi
    local hostip_=`GetInfo "${info_}" ip` hostfull_= sn_=qconf-agent prefix_=/usr/local/qconf
    hostfull_=${hostip_}:2181
    
    # install libevent
    cd ${_PATH_COMPILATION_}
    eval rm -rf ./qconf-* ${_SYS_SHOW_INSTALL_DETAILS_}
    eval tar zxvf ${_PATH_SOFTWARE_}/qconf-* ${_SYS_SHOW_INSTALL_DETAILS_}
    eval cd QConf-* ${_SYS_SHOW_INSTALL_DETAILS_}
    eval cmake . ${_SYS_SHOW_INSTALL_DETAILS_}
    eval make ${_SYS_MAKE_EXT_} ${_SYS_SHOW_INSTALL_DETAILS_}
    eval make install ${_SYS_SHOW_INSTALL_DETAILS_}
    ShowStatus $? 1
    
    eval chmod +x ${prefix_}/bin/* ${_SYS_SHOW_INSTALL_DETAILS_}
    sed -ri "/^zookeeper.test/c \zookeeper\.test=${hostfull_}" ${prefix_}/conf/idc.conf
    ShowStatus $? 1
    
    if [ ! -f /etc/init.d/${sn_} ]; then
        eval \cp -f ${prefix_}/bin/agent-cmd.sh /etc/init.d/${sn_} ${_SYS_SHOW_INSTALL_DETAILS_}
        sed -ri '1i# chkconfig: 345 60 60\n# description: Activates/Deactivates QConf-Agent' /etc/init.d/${sn_}
        eval chkconfig --add ${sn_} ${_SYS_SHOW_INSTALL_DETAILS_}
        ShowStatus $?
        eval service ${sn_} start ${_SYS_SHOW_INSTALL_DETAILS_}
    else
        ShowStatus 0
    fi
}

function InstallGameQconf()
{
    local game_qconf=/etc/init/game-qconf.conf

    if [ ! -f ${game_qconf} ];then
        cat > ${game_qconf} << 'EOF'
description "My game-zk watch script."
author "LMH"
start on runlevel [234]
stop on runlevel [0156]

chdir /data/cmd                        #脚本路径
exec /data/cmd/game-qconf.py           #需要给x权限
respawn
EOF
    initctl reload-configuration
    fi

    if [ -d  /usr/lib/systemd/system ];then
cat > /usr/lib/systemd/system/game-qconf.service << 'EOF'
[Unit]
Description=/data/cmd/game-qconf.py

[Service]
ExecStart=/data/cmd/game-qconf.py
ExecReload=/bin/kill -HUP $MAINPID
RestartSec=42s

[Install]
WantedBy=multi-user.target
EOF
    systemctl daemon-reload
    fi

    if [ ! -f /etc/init.d/game-qconf ];then
        cat > /etc/init.d/game-qconf << 'SH'
#/bin/sh
#chkconfig: 2345 20 80
#description:  My game-zk watch script.
case $1 in
        start)
            start game-qconf
            ;;
        stop)
            stop game-qconf
            ;;
        status)
            status game-qconf
            ;;
        restart)
            restart game-qconf
            ;;
        *)  echo "require start|stop|status|restart";;
esac
SH
    
    chmod +x /etc/init.d/game-qconf
    chmod +x /data/cmd/*.py
    fi
}