#!/bin/sh

USAGE="\
Usage: $ <cmd...> | $(basename $0) [--ts-provided|--ts-local|--ts-server] <logstream-label>

Output lines of <cmd...> will be transmitted up to
WalT server and stored as log lines associated with
the given log stream label.

By default, or if option --ts-server is specified, timestamps
will be taken upon reception on server side.

If you know the node is well synchronized (e.g., PTP is set up
on this WALT image, or NTP and the node has booted several hours
ago) you may specify --ts-local instead. Timestamps will be
taken locally on the node by this script, using 'date +%s.%N'.

If you can get even higher precision timestamps (e.g. network
capture timestamps), you may instead use option --ts-provided
and stream lines of the following form:
<float_timestamp> <log_line>
with float_timestamp a unix timestamp (float number of
seconds since 1970), as obtained with 'date +%s.%N'.
"

# get walt-related env variables
. walt-env

# check possible option
ts_mode='server'   # --ts-server assumed by default

# note: "--timestamp" or "--ts" are obsolete aliases to "--ts-provided".
# It is mandatory to maintain these aliases because older experiment
# scripts in walt images and the walt-logs-daemon software of
# sub-package walt-common use them.
if [ "$1" = "--timestamp" -o "$1" = "--ts" -o "$1" = "--ts-provided" ]
then
    # timestamps provided in input
    ts_mode='provided'
    shift
elif [ "$1" = "--ts-local" ]
then
    # check if date command can provide sub-second granularity
    if [ "$(date +%N)" = '%N' ]
    then
        # cannot get sub-second granularity
        # we will let the server timestamp traces
        echo "WARNING: this date command cannot provide sub-second granularity." >&2
        echo "WARNING: falling back to server timestamping mode (--ts-server)." >&2
        ts_mode='server'
    else
        # OK for local timestamping
        ts_mode='local'
    fi
    shift
elif [ "$1" = "--ts-server" ]
then
    ts_mode='server'
    shift
fi

# get stream name as first arg
stream="$1"

if [ -z "$stream" ]
then
    echo "$USAGE" >&2
    exit 1
fi

# save stdout as fd 6
exec 6<&1

# read log lines, timestamp them
# and send them through network
{
    echo REQ_NEW_INCOMING_LOGS
    echo "$stream"
    if [ "$ts_mode" = "server" ]
    then
        # let the server know we will not send timestamps
        # (thus server should set timestamps itself)
        echo "NO_TIMESTAMPS"
        while read line
        do
            # send log line through pipe
            echo "$line"
            # also print it to stdout
            echo "$line" >&6
        done
    else
        # let the server know we will send timestamps
        # (as 1st column)
        echo "TIMESTAMPS_INSIDE"
        if [ "$ts_mode" = "provided" ]
        then
            while read ts line
            do
                # send log line through pipe
                echo "$ts $line"
                # also print it to stdout
                echo "$line" >&6
            done
        else    # ts_mode 'local'
            while read line
            do
                # send log line through pipe
                echo "$(date +%s.%N) $line"
                # also print it to stdout
                echo "$line" >&6
            done
        fi
    fi
    # this "CLOSE" message is not formatted as a
    # log line, thus the server will close the
    # connection, which will cause the nc process
    # below to disconnect.
    echo CLOSE
} | busybox nc $walt_server_ip $walt_server_logs_port

