#!/usr/bin/perl

#########################################
# 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
#
#########################################

use strict;
use warnings;

use Data::Dumper;
use Getopt::Std;
use File::Basename;

#other libraries

#########################################
# Script configuration defaults
# casual users should not need to change
# anything below this section
#########################################
my $description="Make DB table from CSV data";
my $WIN_MSEC_DFL=3600000;
my %options=();

#################################
# Script variable initialization
#################################
my $VERBOSE="N";
my $DO_HELP;
my $ARG_HEADER;
my $ARG_ID;
my $id=0;
my $fid="";

$DO_HELP="N";
$ARG_HEADER="";
$ARG_ID=0;

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

#################################
# name: printUsage
# description: print use message
# args: none
#################################
sub printUsage {
    print "\n";
    print " Description: $description\n";
    print "\n";
    printf " use: %s [options] file [file...]\n",basename($0);
    print "  Options:\n";
    print "   -H s    : header string\n";
    print "   -I n    : ID (<0 for empty)\n";
    print "   -v      : verbose output    [$VERBOSE]\n";
    print "   -h      : print use message\n";
    print "\n";
    print "\n";
}

########################################
# name: vout
# description: print verbose message to stderr
# args:
#     msg: message
########################################
sub vout {
    my $msg = shift;
    print STDERR $msg if ( $VERBOSE =~ /Y/ );
}

########################################
# name: exitError
# description: exit w/ message to stderr
# args:
#     msg   : error message
#     status: exit status to return
########################################
sub exitError {
    my $msg = shift;
    my $status = shift;
    my $bin=basename($0);
        print STDERR "\n";
        print STDERR "$bin: error - $msg\n";
    	print STDERR "\n";
	    exit $status;
}

########################################
# name: processCmdLine
# description: do command line processsing
# args:
#     args:       positional paramters
#     returnCode: none
########################################
sub processCmdLine {
    # declare the perl command line flags/options we want to allow
    getopts("H:hI:v", \%options);
    $DO_HELP="Y" if defined $options{h};
    $VERBOSE="Y" if defined $options{v};
    $ARG_HEADER=$options{H} if defined $options{H};
    $ARG_ID=$options{I} if defined $options{I};
}

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

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

# process command line args
#my $counter=0;
#foreach my $a(@ARGV) {
#    print "Arg # $counter : $a\n";
#    $counter++;
#}

#if ( ($#ARGV+1) == 0  ){
#    print "NO ARGS\n";
#    printUsage;
#    #	exit -1
#}else{
#    # this ensures that quoted whitespace is preserved by getopts
#    # note use of $@, in quotes
#    processCmdLine
#}
processCmdLine;


# other things found on the command line
if( $VERBOSE =~ /Y/ ) {
    # test for the existence of the options on the command line.
    # in a normal program you'd do more than just print these.
    print "opt: -h $options{h}\n" if defined $options{h};
    print "opt: -v $options{v}\n" if defined $options{v};
    print "opt: -H $options{H}\n" if defined $options{H};
    print "opt: -I $options{I}\n" if defined $options{I};
    print "additional args:\n" if $ARGV[0];
    foreach (@ARGV){ print "$_\n";}
}


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

# output help and exit
if ( $DO_HELP =~ /Y/ ){
    printUsage;
    exit 0;
}

print "$ARG_HEADER\n" if defined $options{H};

if(defined $options{I}){
    $id=$ARG_ID;
}else{
    $id="";
}

my $line;
$line="";
do{
    $line=readline(STDIN);
    if ($line =~ /^\s*$/ ){
    }else{
        if(defined $options{I}){
            if($ARG_ID >= 0){
                $fid="$id,";
            }else{
                $fid=",";
            }
        }else{
            $fid="";
        }
        print "$fid$line" ;
        $id+=1;
    }
}while ( $line );
