

#!/bin/bash

#########################################
# Name: mktable.sh
#
# Summary: make DB table from raw csv
#
# Description:
# Add ID field to (and optional header)
# to existing CSV data
#
# Author: k. headley
#
# Copyright 2018 MBARI
#
#########################################

#########################################
# Script configuration defaults
# casual users should not need to change
# anything below this section
#########################################
description="Make DB table from CSV data"

#################################
# Script variable initialization
#################################
VERBOSE="N"
unset DO_HELP
unset ARG_HEADER
unset ARG_ID
unset ARG_GNUDATE
MT_CDATE=""

#################################
# 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 "   -H s    : header string"
	echo "   -I n    : ID (<0 for empty)"
#	echo "   -g      : GNU date (use for Cygwin)"
    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: convertDate
# description: convert date format
# args:
#     $1:        date  [note 1]
#     $2:        fromFormat
#     $3:        toFormat
# returns value in MT_CDATE
# [1] prefix w/ '@' if epoch sec, e.g. @1533106800
########################################
convertDate(){

	local date_in=$1
	local fromFormat=$2
	local toFormat=$3
	MT_CDATE=""

	echo "converting $date_in from $fromFormat to $toFormat"
	nmatch_len=`expr "${date_in}" : '[0-9]*'`
	if [ $ARG_GNUDATE ]
	then
		if [ ${fromFormat} == "%s" ]
		then
			date_in="@${date_in}"
		fi
		MT_CDATE=$(date --date ${date_in} ${toFormat})
	else
		MT_CDATE=$(date -jf ${fromFormat} ${date_in} ${toFormat})
	fi
}

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

	while getopts ghH:I:v Option
    do
        vout "processing $Option[$OPTARG]"
        case $Option in
            g) ARG_GNUDATE=$OPTARG
            ;;
            H ) ARG_HEADER=$OPTARG
            ;;
            I ) ARG_ID=$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

# GNU utils date conversion
# (does not have BSD -j option)
# $ date --date="2018-08-01T00:00:00" +%s
# 1533106800
#
# $ date --date="@1533106800" +%Y-%m-%dT%H:%M:%S
# 2018-08-01T00:00:00

#NOW_ISO=$(date  +%Y-%m-%dT%H:%M:%S)
#convertDate ${NOW_ISO} '%Y-%m-%dT%H:%M:%S' '+%s'
#let "NOW_EPS=${MT_CDATE}"
#vout "NOW_EPS=${NOW_EPS}"
#
#convertDate ${NOW_EPS} '%s' '+%Y-%m-%dT%H:%M:%S'
#NOW_ISO=${MT_CDATE}
#vout "NOW_ISO=${NOW_ISO}"


###########################
# debug output
vout "VERBOSE     : [$VERBOSE]"
vout "HEADER      : [$ARG_HEADER]"
vout "ID          : [$ARG_ID]"

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

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

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


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

while IFS= read line
do
    if [ ${#line} -gt 24 ]
    then
        if [ ${ARG_ID} ]
        then
            if [ ${ARG_ID} -ge 0 ]
            then
                fid="${id},"
            else
                fid=","
            fi
        else
            fid=""
        fi

        echo "${fid}${line}"

        # update counters
        let "id+=1"
    fi
done