# linux script logic server build(included tarball) about functions encapsulation
# HongWei Zheng
###################################################################

source "${____LOGIC_SCRIPT_PATH}/lnx_fwk_defs.sh"
source "${____LOGIC_SCRIPT_PATH}/lnx_logic_defs.sh"


############################################################
# 解压热更包
function _unzip_hotfix()
{
    echo "解压热更包... [${SVR_PATH}/${HF_ZIP_NAME}]"
    cd ${SVR_PATH} && unzip -d ./ -o ./${HF_ZIP_NAME}
}

function _exec_debug_cmd()
{
    local url='http://localhost:3001/'
    local cmd=${1}
    local ret=`curl ${url}${cmd}|grep 'CMD OK'`
    if [ "${ret}" = "<CMD OK>" ]; then
        return 0
    fi
    return 1
}

# 清除代码缓存
function _clear_code_cache()
{
    echo "清理代码缓存..."
    _exec_debug_cmd 'clearcache'
    return $?
}

# 检查外部参数
function _check_hotfix_args()
{
    if [ -z "${HF_ZIP_NAME}" ]; then
        echo "热更包名错误 [${HF_ZIP_NAME}]"
        return 1
    fi

    local game=`ls ${SVR_PATH}|grep 'game'`
    if [ "${game}" != "game" ]; then
        echo "源码目录错误 [${SVR_PATH} ${game}]"
        return 1
    fi

    if [ ! -s "${SVR_PATH}/${HF_ZIP_NAME}" ]; then
        echo "热更包不存在 [${SVR_PATH}/${HF_ZIP_NAME}]"
        return 1
    fi

    return 0
}

function hotfix_zip_gs()
{
    HF_ZIP_NAME=${1} #热更包名

    _check_hotfix_args
    if [ $? -ne 0 ]; then
        echo "参数错误"
        exit 1
    fi

    _exec_debug_cmd 'help'
    if [ $? -ne 0 ]; then
        echo "连接debug_control失败"
        exit 1
    fi

    _unzip_hotfix
    if [ $? -ne 0 ]; then
        echo "解压热更包失败"
        exit 1
    fi

    _clear_code_cache
    if [ $? -ne 0 ]; then
        echo "清除代码缓存失败"
        exit 1
    fi

    echo "热更完成" ${HF_ZIP_NAME}
    echo "热更完成" ${HF_ZIP_NAME} >> ./hotfixPkgLog.txt
}
