# fake time about functions encapsulation
# Longwei Lai
###################################################################

source "${____FWK_SCRIPT_PATH}/lnx_fwk_defs.sh"

# read current fake time setting
# arguments:
#   N/A
# returns:
#   echo style:
#     faketime or "no faketime"
#   return style:
#     N/A
function read_faketime()
{
    local faketime=
    if [ -f "${____FWK_SVR_FAKETIME_OPTION_FILE}" ]; then
        faketime="`cat "${____FWK_SVR_FAKETIME_OPTION_FILE}"`"
    fi

    if [ -z "${faketime}" ]; then
        echo -n "no faketime"
        return
    fi

    local now_time="`date "+%Y-%m-%d %H:%M:%S"`"
    local faketime_begin=
    if [ -f "${____FWK_SVR_FAKETIME_BEGIN_OPTION_FILE}" ]; then
        faketime_begin="`cat "${____FWK_SVR_FAKETIME_BEGIN_OPTION_FILE}"`"
    else
        faketime_begin="${now_time}"
        echo -n "${faketime_begin}" > "${____FWK_SVR_FAKETIME_BEGIN_OPTION_FILE}"
    fi

    local now_ts="`date -d "${now_time}" '+%s'`"
    local faketime_begin_ts="`date -d "${faketime_begin}" '+%s'`"
    local diff_time=$((${now_ts} - ${faketime_begin_ts}))

    local faketime_ts="`date -d "${faketime}" '+%s'`"
    local now_faketime_ts=$((${faketime_ts} + ${diff_time}))
    local real_faketime="`date -d "@${now_faketime_ts}" '+%Y-%m-%d %H:%M:%S'`"

    echo -n "${real_faketime}"
}

# clear fake time setting
# arguments:
#   N/A
# returns:
#   echo style:
#     N/A
#   return style:
#     N/A
function clear_faketime()
{
    if [ -f "${____FWK_SVR_FAKETIME_OPTION_FILE}" ]; then
        echo -n "" > "${____FWK_SVR_FAKETIME_OPTION_FILE}"
    fi
}
