#!/bin/sh

# 操作系统相关
_SYS_BIT_=`getconf LONG_BIT`
_SYS_MEM_=`expr \`cat /proc/meminfo | grep MemTotal | cut -f2 -d: | sed 's/ //g' | cut -f1 -dk\` / 1024`
_SYS_CPUS_=`cat /proc/cpuinfo | grep processor | wc -l`
_SYS_CHECK_LIST_AWK_='BEGIN {FS=","; RS=""; ORS=""; } {x=1; while(x<=NF) {print $x "\n"; x++}}'
_SYS_SHOW_INSTALL_DETAILS_=' >> '${_PATH_INIT_LOG_}' 2>&1'
_SYS_CPU_NUMS_=`cat /proc/cpuinfo | grep -c "model name"`
_SYS_MAKE_EXT_=
_INSTALL_STEP_=0
_CURRENT_STEP_=0
_ERROR_TAIL_NUMS_=10
_MEMCACHED_MAX_MEM_=64M

# 需要创建的路径列表
declare -a _MKDIR_=()

# @function 延迟初始化
function __DELAY_INIT__()
{
    if [ ${_IS_SHOW_COMPILATION_DETAILS_} -eq 1 ]; then
        _SYS_SHOW_INSTALL_DETAILS_=''
    fi
    if [ ${_IS_MULTI_THREAD_COMPILATION_} -eq 1 ]; then
        _SYS_MAKE_EXT_=" -j ${_SYS_CPU_NUMS_}"
    fi
    
    # 默认将公司的ip加入iptables白名单
    for i in ${_CORP_IPS_[*]}; do
        _IPTABLES_[${#_IPTABLES_[*]}]="-s ${i};"
    done
    
    # 默认将公司的ip和本地lo加入web白名单
    _extra=127.0.0.1
    if [ ${#_CORP_IPS_[*]} -gt 0 ]; then
        _extra=${_extra:+${_extra},}`echo ${_CORP_IPS_[*]} | sed -r 's/ /,/g'`
    fi
    for i in ${!_WEB_ACCESS_WHITELIST_[*]}; do
        if [ ! -z "${_WEB_ACCESS_WHITELIST_[${i}]}" ]; then
            _WEB_ACCESS_WHITELIST_[${i}]=${_extra},${_WEB_ACCESS_WHITELIST_[${i}]}
        fi
    done
    
    # 默认将一些地址加入到db授权账户下
    _DB_GAME_ACCOUNT_ALLOW_WHITELIST_[${#_DB_GAME_ACCOUNT_ALLOW_WHITELIST_[*]}]=127.0.0.1
    _DB_GAME_ACCOUNT_ALLOW_WHITELIST_[${#_DB_GAME_ACCOUNT_ALLOW_WHITELIST_[*]}]=localhost
    for i in $(echo "CPP PHP DB DBCTRL"); do
        if [ -z "${_HOSTS_[${i}]}" ]; then continue; fi
        _DB_GAME_ACCOUNT_ALLOW_WHITELIST_[${#_DB_GAME_ACCOUNT_ALLOW_WHITELIST_[*]}]=`GetInfo "${_HOSTS_[${i}]}" name`
    done
    
    # 将path推入需要mkdir的列表
    let iota=0
    [ ! -z "${_PATH_SOFTWARE_}" ]       && _MKDIR_[iota++]=${_PATH_SOFTWARE_}
    [ ! -z "${_PATH_COMPILATION_}" ]    && _MKDIR_[iota++]=${_PATH_COMPILATION_}
    [ ! -z "${_PATH_CONFIGS_}" ]        && _MKDIR_[iota++]=${_PATH_CONFIGS_}
    [ ! -z "${_PATH_CMD_}" ]            && _MKDIR_[iota++]=${_PATH_CMD_}
    [ ! -z "${_PATH_LOG_}" ]            && _MKDIR_[iota++]=${_PATH_LOG_},0777
    [ ! -z "${_PATH_WEB_}" ]            && _MKDIR_[iota++]=${_PATH_WEB_}
    [ ! -z "${_PATH_CPP_}" ]            && _MKDIR_[iota++]=${_PATH_CPP_}
    [ ! -z "${_PATH_DBAK_}" ]           && _MKDIR_[iota++]=${_PATH_DBAK_}
    [ ! -z "${_PATH_BACKUP_}" ]         && _MKDIR_[iota++]=${_PATH_BACKUP_}
    [ ! -z "${_PATH_PHP_SESS_}" ]       && _MKDIR_[iota++]=${_PATH_PHP_SESS_},0777
    [ ! -z "${_PATH_DB_DATA_}" ]        && _MKDIR_[iota++]=${_PATH_DB_DATA_}
    [ ! -z "${_PATH_REDIS_DATA_}" ]     && _MKDIR_[iota++]=${_PATH_REDIS_DATA_}
}


## -- FUNCTION -- ##
function GetInfo()
{
    local str_="$1" field_=$2 tmp_=
    eval declare -A tmp_=${str_}
    if [ -z "${tmp_[*]}" ]; then
        echo ""
        return
    fi
    echo ${tmp_[${field_}]}
}

# functions - show messages
# @function show result
function ShowResult()
{
    local msg=$1
    local nonewline=$2
    
    if [ ${nonewline} ]; then
        nonewline=" -n"
    fi
    
    echo ${nonewline} -e `ReplaceColor "${msg}"`
}

# @function ShowStatus()
function ShowStatus()
{
    local _status=$1
    local _hidden=$2
    
    # check status
    if [ ${_status} -eq 0 ]; then
        # if in hidden mode, return
        if [ ${_hidden} ] && [ ${_hidden} -eq 1 ]; then
            return
        fi
        ShowResult "[[green]DONE[/green]]"
    else
        ShowResult "[[red]FAILED[/red]]"
        # print error messages under background mode
        # first all, the log-file must exists (means: /dev/null as output destination will not be supported)
        if [ ${_IS_SHOW_COMPILATION_DETAILS_} -eq 0 ] && [ -f ${_PATH_INIT_LOG_} ]; then
            ShowResult "[red]------ 错误信息可能如下(非完整) ------[/red]"
            tail -${_ERROR_TAIL_NUMS_} ${_PATH_INIT_LOG_}
            ShowResult "[red]--------------------------------------[/red]"
        fi
        exit 1
    fi
}

# @function replace color
function ReplaceColor()
{
    local msg=$1
    local clr=
    local prev=
    local post='\033[0m'
    while [ "" != "$(echo $msg | perl -ne 'print if s/\[([\w]+)\](.*?)\[\/\1\]/\1/')" ]; do
        clr=$(echo $msg | sed 's/\(\[\([a-z]\{1,\}\)\]\).*/\1/' | sed 's/.*\[\([a-z]\{1,\}\)\]/\1/')
        prev='\033['`GetColor $clr`';40m'
        msg=$(echo $msg | perl -ne 'print if s/\[([\w]+)\](.*?)\[\/\1\]/'${prev//\\/\\\\}'\2'${post//\\/\\\\}'/')
    done
    
    echo $msg
}

# @function get color defined
function GetColor()
{
    local color=$1
    case ${color} in
        red)
        color=31
        ;;
        green)
        color=32
        ;;
        yellow)
        color=33
        ;;
        blue)
        color=34
        ;;
        purple)
        color=35
        ;;
        lightblue)
        color=36
        ;;
        default)
        color=30
        ;;
    esac
    
    echo ${color}
}

