

#!/bin/bash

#########################################
# Name: dgen.sh
#
# Summary: generate test data
#
# Description:
#
# Author: k. headley
#
# Copyright 2018 MBARI
#
#########################################

#########################################
# Script configuration defaults
# casual users should not need to change
# anything below this section
#########################################
description="Generate test data"
WIN_MSEC_DFL=3600000

#################################
# Script variable initialization
#################################
VERBOSE="N"
unset DO_HELP
unset ARG_BTIME
unset ARG_ETIME
unset ARG_EMSTIME
unset ARG_HEADER
unset ARG_ID
unset ARG_INDEX
unset ARG_DTIME
unset OFILE
let "PERIOD_MSEC=100"
RECORD="x"

#################################
# Function Definitions
#################################

#################################
# name: printUsage
# description: print use message
# args: none
#################################
printUsage(){
    echo
    echo " Description: $description"
    echo
    echo " use: `basename $0` [options] file [file...]"
    echo "  Options:"
    echo "   -b t    : window start"
    echo "   -e t    : window end"
	echo "   -E      : add epoch msec timestamp"
	echo "   -H s    : header string"
    echo "   -I n    : ID"
    echo "   -i n    : index"
    echo "   -p n    : data period (ms)"
    echo "   -r s    : record string"
    echo "   -o path : output file path"
    echo "   -v      : verbose output    [$VERBOSE]"
    echo "   -h      : print use message"
    echo ""
    echo
}

########################################
# name: vout
# description: print verbose message to stderr
# args:
#     msg: message
########################################
vout(){
    if [ "${VERBOSE}" == "Y" ] || [ "${VERBOSE}" == "TRUE" ]
    then
    	echo "$1" >&2
    fi
}

########################################
# name: imsg
# description: echos with indent
# args:
#     msg: message
#  indent: indent level
########################################
imsg(){
    OIFS=$IFS
    IFS=""
    msg_pad="                                        "
    let "msg_indent=${1}"
    shift
    all="${@}"
    printf "%s%s\n" ${msg_pad:0:${msg_indent}} ${all}
    IFS=$OIFS
}

########################################
# name: msg
# description: echos with default indent
# args:
#     msg: message
########################################
msg(){
	imsg $MSG_INDENT "${*}"
}

########################################
# name: exitError
# description: print use message to stderr
# args:
#     msg:        error message
#     returnCode: exit status to return
########################################
exitError(){
    echo >&2
    echo "`basename $0`: error - $1" >&2
    echo >&2
    exit $2
}

########################################
# name: processCmdLine
# description: do command line processsing
# args:
#     args:       positional paramters
#     returnCode: none
########################################
processCmdLine(){
    OPTIND=1
    vout "`basename $0` all args[$*]"

while getopts b:e:EhH:i:I:o:p:r:v Option
    do
        vout "processing $Option[$OPTARG]"
        case $Option in
            b ) ARG_BTIME=$OPTARG
            ;;
            e ) ARG_ETIME=$OPTARG
            ;;
            E ) ARG_EMSTIME="Y"
            ;;
            H ) ARG_HEADER=$OPTARG
            ;;
            i ) ARG_INDEX=$OPTARG
            ;;
            I ) ARG_ID=$OPTARG
            ;;
            o ) OFILE=$OPTARG
            ;;
            p ) let "PERIOD_MSEC=$OPTARG"
            ;;
            r ) RECORD="${OPTARG}"
            ;;
            v ) VERBOSE="Y"
            ;;
            h) DO_HELP="Y"
            ;;
            *) exit 0 # getopts outputs error message
            ;;
        esac
    done
}

##########################
# Script main entry point
##########################

# Argument processing
# Accepts arguments from command line
# or pipe/file redirect

# process command line args
if [ "$#" -eq 0 ]
then
    printUsage
    #	exit -1
else
    # this ensures that quoted whitespace is preserved by getopts
    # note use of $@, in quotes

    # originally was processCmdLine $*
    processCmdLine "$@"
    let "i=$OPTIND-1"
    while [ "${i}" -gt 0  ]
    do
        shift
        let "i-=1"
    done
fi

###########################
# generate time window boundaries
# normalized to epoch milliseconds

NOW_ISO=$(date  +%Y-%m-%dT%H:%M:%S)
let "NOW_EPS=$(date -jf '%Y-%m-%dT%H:%M:%S' $NOW_ISO +%s)"

if [ ${ARG_BTIME} ]
then
	BTIME_ISO=${ARG_BTIME:0:19}
else
	BTIME_ISO=${NOW_ISO}
fi
BTIME_EPS=$(date -jf "%Y-%m-%dT%H:%M:%S" $BTIME_ISO +%s)

if [ ${ARG_ETIME} ]
then
	ETIME_ISO=${ARG_ETIME:0:19}
else
    let "ETIME_EPS=$BTIME_EPS+$WIN_MSEC_DFL/1000"
    ETIME_ISO=$(date -jf "%s" $BTIME_EPS +%Y-%m-%dT%H:%M:%S)
fi
ETIME_EPS=$(date -jf "%Y-%m-%dT%H:%M:%S" $ETIME_ISO +%s)


if [ ${ARG_BTIME:20:3} ]
then
	BTIME_MS=${ARG_BTIME:20:3}
    BTIME_MS=${BTIME_MS#0}
    BTIME_MS=${BTIME_MS#0}
else
	let "BTIME_MS=0"
fi

if [ ${ARG_ETIME:20:3} ]
then
    ETIME_MS=${ARG_ETIME:20:3}
    ETIME_MS=${ETIME_MS#0}
    ETIME_MS=${ETIME_MS#0}
else
	let "ETIME_MS=0"
fi

let "BTIME_EMS=$BTIME_EPS*1000+$BTIME_MS"
let "ETIME_EMS=$ETIME_EPS*1000+$ETIME_MS"

###########################
# debug output
vout "VERBOSE     : [$VERBOSE]"
vout "BTIME       : [$ARG_BTIME]"
vout "ETIME       : [$ARG_ETIME]"
vout "EMSTIME     : [$ARG_EMSTIME]"
vout "HEADER      : [$ARG_HEADER]"
vout "INDEX       : [$ARG_INDEX]"
vout "ID          : [$ARG_ID]"
vout "PERIOD_MSEC : [$PERIOD_MSEC]"
vout "OFILE       : [$OFILE]"
vout "BTIME_EPS   : [$BTIME_EPS]"
vout "BTIME_EMS   : [$BTIME_EMS]"
vout "ETIME_EPS   : [$ETIME_EPS]"
vout "ETIME_EMS   : [$ETIME_EMS]"

###########################
# Run

# output help and exit
if [ ${DO_HELP} ] && [ ${DO_HELP} == "Y" ]
then
    printUsage
    exit 0
fi

if [ ${ARG_HEADER} ]
then
echo ${ARG_HEADER}
fi

let "t=$BTIME_EMS"
if [ ${ARG_INDEX} ]
then
let "idx=${ARG_INDEX}"
else
let "idx=0"
fi

if [ ${ARG_ID} ]
then
let "id=${ARG_ID}"
else
let "id=0"
fi

while [ $t -lt $ETIME_EMS ]
do

# compute ISO1806 timestamp
    let "t_eps=$t/1000"
    let "t_msec=$t%1000"
    t_iso=$(date -jf '%s' $t_eps +%Y-%m-%dT%H:%M:%S)

# compute appropriate leading zeros
# for milliseconds
    if [ $t_msec -lt 100 ]
    then
        if [ $t_msec -ge 10 ]
        then
            lz="0"
        else
            lz="00"
        fi
    else
    	unset lz
    fi

# output faux data record to stdout
# iso1806-time,record,index
if [ ${ARG_INDEX} ]
then
    fidx=",${idx}"
else
	fidx=""
fi

if [ ${ARG_ID} ]
then
	fid="${id},"
else
	fid=""
fi

if [ ${ARG_EMSTIME} ]
then
	printf -v fems "%d.%03d," ${t_eps} ${t_msec}
else
	fems=""
fi

echo "${fid}${fems}$t_iso.${lz}${t_msec}Z,${RECORD}${fidx}"

# update counters
    let "t+=$PERIOD_MSEC"
    let "idx+=1"
    let "id+=1"

done