

# It is only an estimation. And that the value might be wrong when the mq current.tsv suddenly move the mq.lastidx.
# It is preserved as an observer facility, to ensure how many msg waiting. If the value is more than 100, there is a way to handle the message in one time.

# Should be invoked inside   "$___X_CMD_TAILER_HANDLE" "$line"

___x_cmd_tailer_pending(){
    [ $# -gt 0 ]    ||  set -- --help

    while [ $# -gt 0 ]; do
        case "$1" in
            -h|--help)      ___x_cmd help -m tailer pending "$@" ; return 0 ;;
            *)              break ;;
        esac
    done

    local mq_folder="$1"
    [ -n "$mq_folder" ] || N=tailer M="Please provide folder for mq." log:ret:64

    local mq_fp_previous="$mq_folder/MQ.PREVIOUS"
    local mq_fp_previous_data="$mq_fp_previous/data.tsv"
    local mq_fp_previous_index="$mq_fp_previous/index.txt"

    local mq_fp_current="$mq_folder/MQ.CURRENT"
    local mq_fp_current_data="$mq_fp_current/data.tsv"
    local mq_fp_current_index="$mq_fp_current/index.txt"

    local pending_count=0
    local index=0
    local line_count=0

    # First, check PREVIOUS queue
    if [ -e "$mq_fp_previous_index" ]; then
        read -r index <"$mq_fp_previous_index" 2>/dev/null || index=0
        line_count=0
        [ ! -e "$mq_fp_previous_data" ] || {
            line_count="$(___x_cmd_cmds wc -l <"$mq_fp_previous_data" 2>/dev/null)" || line_count=0
        }
        pending_count=$(( pending_count + line_count - index ))
    fi

    # Then, check CURRENT queue
    if [ -e "$mq_fp_current_index" ]; then
        read -r index <"$mq_fp_current_index" 2>/dev/null || index=0
        line_count=0
        [ ! -e "$mq_fp_current_data" ] || {
            line_count="$(___x_cmd_cmds wc -l <"$mq_fp_current_data" 2>/dev/null)" || line_count=0
        }
        pending_count=$(( pending_count + line_count - index ))
    fi

    printf "%s\n" "$pending_count"
}
