# linux script framework server about functions encapsulation
# Longwei Lai
###################################################################

source "${____FWK_SCRIPT_PATH}/lnx_fwk_log.sh"
source "${____FWK_SCRIPT_PATH}/lnx_fwk_defs.sh"
source "${____FWK_SCRIPT_PATH}/lnx_fwk_path.sh"
source "${____FWK_SCRIPT_PATH}/lnx_fwk_progress.sh"

# control commands define
____FWK_SERVER_CTRL_START=start
____FWK_SERVER_CTRL_STOP=stop
____FWK_SERVER_CTRL_RESTART=restart
____FWK_SERVER_CTRL_IS_RUNNING=is_running
____FWK_SERVER_CTRL_LIST_STATUS=list_status

# ctrl specific server
# arguments:
#   arg1: server name
#   arg2: server control cmd
# returns:
#   echo style: 
#       N/A
#   return style:
#       0: success
#       1: failed
function ctrl_server()
{
    local svr_name="$1"
    local ctrl_cmd="$2"

    local args=($@)
    local svr_args=${args[@]:2}

    if [ "${ctrl_cmd}" = "${____FWK_SERVER_CTRL_START}" ]; then
        start_server "${svr_name}" ${svr_args}
    elif [ "${ctrl_cmd}" = "${____FWK_SERVER_CTRL_STOP}" ]; then
        stop_server "${svr_name}" ${svr_args}
    elif [ "${ctrl_cmd}" = "${____FWK_SERVER_CTRL_RESTART}" ]; then
        restart_server "${svr_name}" ${svr_args}
    elif [ "${ctrl_cmd}" = "${____FWK_SERVER_CTRL_IS_RUNNING}" ]; then
        is_server_running "${svr_name}" ${svr_args}
    elif [ "${ctrl_cmd}" = "${____FWK_SERVER_CTRL_LIST_STATUS}" ]; then
        list_server_status "${svr_name}" ${svr_args}
    else
        log_err "unknown ctrl command: ${ctrl_cmd}"
        return 1
    fi
}

# start specific server
# arguments:
#   arg1: server name
#   arg2: server entry
#   arg3-n: server start arguments
# returns:
#   echo style:
#       N/A
#   return style:
#       0: success
#       1: failed
#       2: already start, don't need start again
function start_server()
{
    local svr_name="$1"
    local svr_entry="$2"
    local show_name="$(get_server_show_name "${svr_name}")"
    local pid_file="$(make_server_pid_file_path "${svr_name}")"

    local args=($@)
    local svr_args="${args[@]:2}"
    
    # switch current directory to server path
    local old_path="`pwd`"
    cd "`dirname "${svr_path}"`"

    # makesure just running one instance
    if [ "$(is_server_running "${svr_name}")" = TRUE ]; then
        log_warn "${show_name} is running(pid: `cat "${pid_file}"`), don't need start!"
        cd "${old_path}"
        return 2
    fi

    # start server
    local nohup_file=""`dirname "${svr_path}"`"/."${svr_name}".${____FWK_NOHUP_SUFFIX}"
    nohup ${svr_entry} ${svr_args} 1>"${nohup_file}" 2>&1 &
    local svr_pid=$!
    if [ $? -ne 0 ]; then
        log_err "start ${show_name} failed, return code: $?, timeout?: ${check_timeout}"
        return 1
    fi
    echo -n "${svr_pid}" > "${pid_file}"

    # back to old path
    cd "${old_path}"

    log_info "start ${show_name} success, pid: ${svr_pid}"
}

# stop specific server
# arguments:
#   arg1: server name
# returns:
#   echo style:
#       N/A
#   return style:
#       N/A
function stop_server()
{
    local svr_name="$1"
    local pid_file="$(make_server_pid_file_path "${svr_name}")"

    local server_running=`is_server_running "${svr_name}"`
    if [ "${server_running}" = TRUE ]; then
        stop_pidfile "${pid_file}"
    fi

    if [ "${server_running}" = TRUE ]; then
        local nohup_file=""`dirname "${pid_file}"`"/."${svr_name}".${____FWK_NOHUP_SUFFIX}"
        if [ -f "${nohup_file}" ]; then
            log_info "Found ${svr_name} nohup file, backup it"
            local backup_nohup_file="${nohup_file}.`date "+%m%d%H%M%S"`"
            \cp -f "${nohup_file}" "${backup_nohup_file}"
        fi
    fi
}

# restart specific server
# arguments:
#   arg1: server name
# returns:
#   echo style:
#       N/A
#   return style:
#       N/A
function restart_server()
{
    stop_server $@
    start_server $@
}

# check specific server is running or not
# arguments:
#   arg1: server name
# returns:
#   echo style:
#       TRUE: server running
#       FALSE: server not running
#   return style:
#       N/A
function is_server_running()
{
    local svr_name="$1"
    local pid_file="$(make_server_pid_file_path "${svr_name}")"
    echo -n "$(is_pidfile_running "${pid_file}")"
}

# list specific server status
# arguments:
#   arg1: server name
# returns:
#   echo style:
#       N/A
#   return style:
#       N/A
function list_server_status()
{
    local svr_name="$1"

    echo -n -e "${svr_name} status: "
    if [ "$(is_server_running "${svr_name}")" = "TRUE" ]; then
        local pid_file="$(make_server_pid_file_path "${svr_name}")"
        echo -n "Running(pid: "
        echo "`cat "${pid_file}"`)"
    else
        echo "Stopped"
    fi
}

# get server debug option
# arguments:
#    arg1: server name
# returns:
#   echo style:
#       TRUE: build with debug
#       FALSE: build with release
#   return style:
#       N/A
function get_server_debug_option()
{
    # get debug option(from option directory)
    local dbg_opt_file="${____FWK_SVR_DEBUG_OPTION_FILE}"
    if [ ! -e "${dbg_opt_file}" ]; then
        echo -n FALSE
        return
    fi

    local dbg_opt="`cat "${dbg_opt_file}"`"
    if [ "${dbg_opt}" != TRUE ]; then
        echo -n FALSE
    else
        echo -n TRUE
    fi
}

# get server show name(appended all server options)
# arguments:
#   arg1: server name
# returns:
#   echo style:
#       <server show name>: the server show name, appended all server options
#   return style:
#       N/A
function get_server_show_name()
{
    local svr_name="$1"
    local show_name="${svr_name}("

    # append debug/release option
    local dbg_opt="$(get_server_debug_option "${svr_name}")"
    if [ "${dbg_opt}" = TRUE ]; then
        show_name+="debug"
    else
        show_name+="release"
    fi

    show_name+=")"
    echo -n "${show_name}"
}

