# linux script logic misc functions encapsulation
# Longwei Lai
###################################################################

source "${____LOGIC_SCRIPT_PATH}/lnx_logic_defs.sh"

function list_help()
{
    # foreground/background: 3:foreground 4:background
    # colors comments: 0:black 1:red 2:green 3:yellow 4:blue 5:purple 6:sky-blue 7:white
    # reset all: \e[0m

    clear
    echo -e "\e[1;35musage: ./run <command> [command-options...]\e[0m"
    echo -e "\e[1;37m-----------------------------------------------------------------------\e[0m"
    echo -e "  \e[1;32mserver build/tarball commands:\e[0m"
    echo -e "    \e[1;33mup\e[0m: update from git"
    echo -e "    \e[1;33mver_info\e[0m: show version info"
    echo -e "    \e[1;33mswitch_branch\e[0m: <\e[1;37mthe_new_branch\e[0m>: switch branch"
    echo -e "    \e[1;33mmake\e[0m/\e[1;33mbuild\e[0m <\e[1;37mdebug\e[0m/\e[1;37mrelease\e[0m> <\e[1;37mproduct\e[0m/\e[1;37minternal\e[0m/\e[1;37mpressure_test\e[0m\e[0m>: "
    echo "      build servers, default build release version servers"
    echo "      -- default use release setting to build"
    echo "      -- default use product setting to build"
    echo -e "    \e[1;33mtar\e[0m <\e[1;37mdebug\e[0m/\e[1;37mrelease\e[0m> <\e[1;37mproduct\e[0m/\e[1;37minternal\e[0m/\e[1;37mpressure_test\e[0m> <\e[1;37mfull\e[0m/\e[1;37mpure\e[0m>: tarball servers"
    echo "      tarball servers default build all versions servers"
    echo "      -- default use release setting to build"
    echo "      -- default use product setting to build"
    echo "      -- default use pure setting to puild"
    echo -e "\e[1;37m-----------------------------------------------------------------------\e[0m"
    echo -e "  \e[1;32mserver control commands:\e[0m"
    echo -e "    \e[1;33mstart_xxx\e[0m: start server"
    echo -e "      start specific server:"
    echo -e "    \e[1;33mstop_xxx\e[0m: stop server"
    echo -e "    \e[1;33msafestop_xxx\e[0m: safe stop server"
    echo -e "    \e[1;33mrestart_xxx\e[0m: restart server"
    echo -e "    \e[1;33mset_xxx_logic_status\e[0m: set server logic status(progress independent)"
    echo -e "    \e[1;32mserver types:\e[0m"
    echo -e "      * \e[1;36mgameserver:\e[0m game server, for get more informations about gameserver, run \e[1;37m./run help_gameserver\e[0m"
    echo -e "      * \e[1;36mloginserver:\e[0m login server, for get more informations about loginserver, run \e[1;37m./run help_loginserver\e[0m"
    echo -e "\e[1;37m-----------------------------------------------------------------------\e[0m"
    echo -e "  \e[1;32mfaketime commands:\e[0m"
    echo -e "    \e[1;33mread_faketime\e[0m: read now fake time setting"
    echo -e "    \e[1;33mclear_faketime\e[0m: clear fake time setting"
    echo -e "\e[1;37m-----------------------------------------------------------------------\e[0m"
    echo -e "  \e[1;32mtest commands:\e[0m"
    echo "    ...."
}

function list_help_gameserver()
{
    clear
    echo -e "\e[1;35mgameserver manaual page:\e[0m"
    echo -e "\e[1;37m-----------------------------------------------------------------------\e[0m"
    echo -e "  \e[1;33mstart_gameserver\e[0m: <\e[1;37mfakedate=xxxx-xx-xx\e[0\e[0m> <\e[1;37mfaketime=xx:xx:xx\e[0m> <\e[1;37mdontfaketime=YES/NO\e[0m>"
    echo -e "      \e[0;32mstart gameserver\e[0m"
    echo -e "        \e[1;37mfakedate=xxxx-xx-xx\e[0m: fake date start, eg: 2017-08-08"
    echo -e "        \e[1;37mfaketime=xx:xx:xx\e[0m: fake time start, eg: 08-30-59"
    echo -e "        \e[1;37mdontfaketime=YES/NO\e[0m: use fake time setting or not, default is NO"
    echo -e "  \e[1;33mstop_gameserver\e[0m:"
    echo -e "      \e[0;32mstop gameserver, for now, don't need any arguments to specific\e[0m"
    echo -e "  \e[1;33mrestart_gameserver\e[0m: <\e[1;37mfakedate=xxxx-xx-xx\e[0\e[0m> <\e[1;37mfaketime=xx:xx:xx\e[0m> <\e[1;37mdontfaketime=YES/NO\e[0m>"
    echo -e "      \e[0;32mrestart gameserver\e[0m"
    echo -e "        \e[1;37mfakedate=xxxx-xx-xx\e[0m: fake date start, eg: 2017-08-08"
    echo -e "        \e[1;37mfaketime=xx:xx:xx\e[0m: fake time start, eg: 08-30-59"
    echo -e "        \e[1;37mdontfaketime=YES/NO\e[0m: use fake time setting or not, default is NO"
    echo -e "  \e[1;33msafestop_gameserver\e[0m:"
    echo -e "      \e[0;32mlike stop_gameserver, but this command will safety stop gameserver(all game data will be saved before gameservers killed)\e[0m"
    echo -e "  \e[1;33mgameserver_status\e[0m:"
    echo -e "      \e[0;32mlist gameserver status\e[0m"
    echo -e "  \e[1;33mgameserver_logic_status\e[0m:"
    echo -e "      \e[0;32mlist gameserver logic status(progress independent)\e[0m"
    echo -e "  \e[1;33mset_gameserver_logic_status\e[0m:"
    echo -e "      \e[0;32mset gameserver logic status(progress independent)\e[0m"
}

function list_help_loginserver()
{
    clear
    echo -e "\e[1;35mloginserver manaual page:\e[0m"
    echo -e "\e[1;37m-----------------------------------------------------------------------\e[0m"
    echo -e "  \e[1;33mstart_loginserver\e[0m: <\e[1;37mfakedate=xxxx-xx-xx\e[0\e[0m> <\e[1;37mfaketime=xx:xx:xx\e[0m> <\e[1;37mdontfaketime=YES/NO\e[0m>"
    echo -e "      \e[0;32mstart loginserver\e[0m"
    echo -e "        \e[1;37mfakedate=xxxx-xx-xx\e[0m: fake date start, eg: 2017-08-08"
    echo -e "        \e[1;37mfaketime=xx:xx:xx\e[0m: fake time start, eg: 08-30-59"
    echo -e "        \e[1;37mdontfaketime=YES/NO\e[0m: use fake time setting or not, default is NO"
    echo -e "  \e[1;33mstop_loginserver\e[0m:"
    echo -e "      \e[0;32mstop loginserver, for now, don't need any arguments to specific\e[0m"
    echo -e "  \e[1;33mrestart_loginserver\e[0m: <\e[1;37mfakedate=xxxx-xx-xx\e[0\e[0m> <\e[1;37mfaketime=xx:xx:xx\e[0m> <\e[1;37mdontfaketime=YES/NO\e[0m>"
    echo -e "      \e[0;32mrestart loginserver\e[0m"
    echo -e "        \e[1;37mfakedate=xxxx-xx-xx\e[0m: fake date start, eg: 2017-08-08"
    echo -e "        \e[1;37mfaketime=xx:xx:xx\e[0m: fake time start, eg: 08-30-59"
    echo -e "        \e[1;37mdontfaketime=YES/NO\e[0m: use fake time setting or not, default is NO"
    echo -e "  \e[1;33msafestop_loginserver\e[0m:"
    echo -e "      \e[0;32mlike stop_loginserver, but this command will safety stop loginserver(all game data will be saved before loginservers killed)\e[0m"
    echo -e "  \e[1;33mloginserver_status\e[0m:"
    echo -e "      \e[0;32mlist loginserver status\e[0m"
    echo -e "  \e[1;33mloginserver_logic_status\e[0m:"
    echo -e "      \e[0;32mlist loginserver logic status(progress independent)\e[0m"
    echo -e "  \e[1;33mset_loginserver_logic_status\e[0m:"
    echo -e "      \e[0;32mset loginserver logic status(progress independent)\e[0m"
}


function env_ensure()
{
    # force source /etc/profile
    source /etc/profile

    # add SVR_PATH to LD_LIBRARY_PATH variable and export it
    export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${SVR_PATH}"

    # setup linux sys listen backlog size(dynamic change) to ${willsetbacklog}
    local willsetbacklog=4096
    local smcfile=/proc/sys/net/core/somaxconn
    if [ -w "${smcfile}" ]; then
        local oldbacklog=`cat "${smcfile}"`
        if [ "$oldbacklog" -lt "${willsetbacklog}" ]; then
    	log_warn "system listen backlog config[${oldbacklog}] too small, try to config to 4096..."
            echo -n ${willsetbacklog} > "${smcfile}"
            local retcode=$?
            if [ "${retcode}" -ne 0 ]; then
                log_err "modify listen backklog size failed, permission deny? return code: ${retcode}"
                exit $retcode
            fi  
        fi
    fi

    # setup max open file limits to 65535
    ulimit -n 65535
    retcode=$?
    if [ "${retcode}" -ne 0 ]; then
        echo "mofify max open file size failed, return code: ${retcode}"
        exit $retcode
    fi  

    # setup coredump file limits to ulimited
    ulimit -c unlimited
    retcode=$?
    if [ "${retcode}" -ne 0 ]; then
        echo "mofify coredump file limits to ulimited failed, return code: ${retcode}"
        exit $retcode
    fi

    export SKYNET_THREAD=`cat /proc/cpuinfo | grep "processor" |wc -l`
    echo "SKYNET_THREAD is" ${SKYNET_THREAD}
}

