///
/// @file raw2x.c
/// @authors k. headley
/// @date 03 mar 2020

/// Carbon Waveglider (CWG) licor raw data transform utility

/// @sa doxygen-examples.c for more examples of Doxygen markup


/////////////////////////
// Terms of use
/////////////////////////
/*
 Copyright Information

 Copyright 2002-YYYY MBARI
 Monterey Bay Aquarium Research Institute, all rights reserved.

 Terms of Use

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 3 of the License, or
 (at your option) any later version. You can access the GPLv3 license at
 http://www.gnu.org/licenses/gpl-3.0.html

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details
 (http://www.gnu.org/licenses/gpl-3.0.html)

 MBARI provides the documentation and software code "as is", with no warranty,
 express or implied, as to the software, title, non-infringement of third party
 rights, merchantability, or fitness for any particular purpose, the accuracy of
 the code, or the performance or results which you may obtain from its use. You
 assume the entire risk associated with use of the code, and you agree to be
 responsible for the entire cost of repair or servicing of the program with
 which you are using the code.

 In no event shall MBARI be liable for any damages, whether general, special,
 incidental or consequential damages, arising out of your use of the software,
 including, but not limited to, the loss or corruption of your data or damages
 of any kind resulting from use of the software, any prohibited use, or your
 inability to use the software. You agree to defend, indemnify and hold harmless
 MBARI and its officers, directors, and employees against any claim, loss,
 liability or expense, including attorneys' fees, resulting from loss of or
 damage to property or the injury to or death of any person arising out of the
 use of the software.

 The MBARI software is provided without obligation on the part of the
 Monterey Bay Aquarium Research Institute to assist in its use, correction,
 modification, or enhancement.

 MBARI assumes no responsibility or liability for any third party and/or
 commercial software required for the database or applications. Licensee agrees
 to obtain and maintain valid licenses for any additional third party software
 required.
 */

/////////////////////////
// Headers
/////////////////////////
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <getopt.h>
#include <signal.h>
#include <errno.h>
#include <fcntl.h>
#include <time.h>
#include <math.h>
#if defined(__CYGWIN__)
// for POSIX file IO: seek, read, close
#include <unistd.h>
#endif

#include "cwg-shared.h"
#include "z85.h"
#include "licor8xx-shared.h"

/////////////////////////
// Macros
/////////////////////////

// These macros should only be defined for
// application main files rather than general C files
/*
 /// @def PRODUCT
 /// @brief header software product name
 #define PRODUCT "TBD_PRODUCT"

 /// @def COPYRIGHT
 /// @brief header software copyright info
 #define COPYRIGHT "Copyright 2002-YYYY MBARI Monterey Bay Aquarium Research Institute, all rights reserved."
 /// @def NOWARRANTY
 /// @brief header software terms of use
 #define NOWARRANTY  \
 "This program is distributed in the hope that it will be useful,\n"\
 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"\
 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"\
 "GNU General Public License for more details (http://www.gnu.org/licenses/gpl-3.0.html)\n"
 */

#define RAW2X_NAME "rawxfrm"
#ifndef RAW2X_BUILD
/// @def RAW2X_BUILD
/// @brief module build date.
/// Sourced from CFLAGS in Makefile
/// w/ -DLIB_BUILD=`date`
#define RAW2X_BUILD ""VERSION_STRING(LIB_BUILD)
#endif

/////////////////////////
// Declarations
/////////////////////////
#define RAW2X_MAX_FILES 64
#define LICOR_MAX_RAW_RECORD_LEN 1024
#define LICOR_MAX_CSV_LEN 1024

#define LICOR_CSV_HEADER "#time,ctx,co2,co2abs,h2o,h2odewpoint,h2oabs,celltemp,cellpres,ivolt,flowrate,rawco2,rawco2ref,rawh20,rawh2oref"

/// @enum orecord_odev_flag
/// @brief output device flags.
typedef enum{
    OROF_NONE=      0x00000000,
    OROF_CONSOLE=   0x00000001,
    OROF_FILE=      0x00000002,
    OROF_DEV_RES0=  0x00000004,
    OROF_DEV_RES1=  0x00000008,
    OROF_DEV_RES2=  0x00000010,
    OROF_DEV_RES3=  0x00000020,
    OROF_DEV_RES4=  0x00000040,
    OROF_DEV_RES5=  0x00000080,

    OROF_PRETTY=    0x00000100,
    OROF_RAW=       0x00000200,
    OROF_BASE85=    0x00000400,
    OROF_UUENCODE=  0x00000800,
    OROF_HEX=       0x00001000,
    OROF_PRETTY_HEX=0x00002000,
    OROF_CSV=       0x00004000,
    OROF_FMT_RES0=  0x00008000,

    OROF_CON_VERBOSE= 0x00010000,
    OROF_CON_HEADER=  0x00020000,
    OROF_CON_RES1=  0x00040000,
    OROF_CON_RES2=  0x00080000,
    OROF_CON_RES3=  0x00100000,
    OROF_CON_RES4=  0x00200000,
    OROF_CON_RES5=  0x00400000,
    OROF_CON_RES6=  0x00800000,

    OROF_CON_RES7=  0x01000000,
    OROF_CON_RES8=  0x02000000,
    OROF_CON_RES9=  0x04000000,
    OROF_CON_RES10= 0x08000000,
    OROF_CON_RES11= 0x10000000,
    OROF_CON_RES12= 0x20000000,
    OROF_CON_RES13= 0x40000000,
    OROF_CON_RES14= 0x80000000
}orecord_output_flag;
#define OROF_DEV_FLAGS     0x000000FF
#define OROF_FMT_FLAGS     0x0000FF00
#define OROF_CONTENT_FLAGS 0xFFFF0000


typedef struct app_cfg_s {
    char *files[RAW2X_MAX_FILES];
    bool verbose;
    bool test;
    bool header;
    int file_count;
    FILE *fs;
    int fd;
    int idx;
    orecord_output_flag oflags;
}app_cfg_t;

/////////////////////////
// Imports
/////////////////////////

/////////////////////////
// Module Global Variables
/////////////////////////
int g_signal=0;
int g_interrupt=false;

/////////////////////////
// Function Definitions
/////////////////////////

/// @fn void s_show_help()
/// @brief output user help message to stdout.
/// @return none
static void s_show_help()
{
    char help_message[] = "\n Format CWG raw licor data\n";
    char usage_message[] = "\n raw2x [options] file(s)...\n"
    " --debug      : debug output\n"
    " --verbose=n  : verbose output\n"
    " --help       : output help message\n"
    " --version    : output version info\n"
    " --header     : output header\n"
    //    " --test       : test codec\n"
    "\n";
    printf("%s",help_message);
    printf("%s",usage_message);
}
// End function s_show_help

/// @fn void parse_args(int argc, char ** argv, app_cfg_t * cfg)
/// @brief parse command line args, set application configuration.
/// @param[in] argc number of arguments
/// @param[in] argv array of command line arguments (strings)
/// @param[in] cfg application config structure
/// @return none
void parse_args(int argc, char **argv, app_cfg_t *cfg)
{
    extern char WIN_DECLSPEC *optarg;
    int option_index;
    int c;
    bool help=false;
    bool version=false;

    static struct option options[] = {
        {"debug", no_argument, NULL, 0},
        {"verbose", required_argument, NULL, 0},
        {"help", no_argument, NULL, 0},
        {"version", no_argument, NULL, 0},
        {"header", no_argument, NULL, 0},
        {"test", no_argument, NULL, 0},
        {NULL, 0, NULL, 0}};

    // process argument list
    while ((c = getopt_long(argc, argv, "", options, &option_index)) != -1){
        switch (c) {
                // long options all return c=0
            case 0:
                // verbose
                if (strcmp("verbose", options[option_index].name) == 0) {
                    cfg->verbose=true;
                    g_vprint=atoi(optarg);
                }
                if (strcmp("debug", options[option_index].name) == 0) {
                    g_dprint=1;
                }

                // help
                else if (strcmp("help", options[option_index].name) == 0) {
                    help = true;
                }
                // version
                else if (strcmp("version", options[option_index].name) == 0) {
                    version = true;
                }
                // header
                else if (strcmp("header", options[option_index].name) == 0) {
                    cfg->oflags|=OROF_CON_HEADER;
                }

                // map
                else if (strcmp("test", options[option_index].name) == 0) {
                    cfg->test=true;
                }
                break;
            default:
                help=true;
                break;
        }

        int i=++optind;
        for(;i<argc;i++){
            // is a file path
            cfg->files[cfg->file_count++]=strdup(argv[i]);
        }

        if (version) {
            fprintf(stderr,"%s build %s\n",RAW2X_NAME,RAW2X_BUILD);
            exit(0);
        }
        if (help) {
            //            mbtrn_show_app_version(TCPC_NAME,TCPC_BUILD);
            s_show_help();
            exit(0);
        }
    }// while
    VPRINT("verbose     [%s]\n",(cfg->verbose?"Y":"N"));
    VPRINT("header      [%s]\n",(cfg->header?"Y":"N"));
    int i=0;
    for(i=0;i<cfg->file_count;i++)
        VPRINT("file[%02d]   [%s]\n",i,cfg->files[i]);
}
// End function parse_args

/// @fn void termination_handler (int signum)
/// @brief termination signal handler.
/// @param[in] signum signal number
/// @return none
static void s_termination_handler (int signum)
{
    switch (signum) {
#if ! defined(__WIN32) && ! defined(__WIN64)
        case SIGHUP:
#endif
        case SIGINT:
        case SIGTERM:
            VPRINT("sig received[%d]\n",signum);
            g_interrupt=true;
            g_signal=signum;
            break;
        default:
            fprintf(stderr,"s_termination_handler: sig not handled[%d]\n",signum);
            break;
    }
}
// End function termination_handler

static app_cfg_t *app_cfg_new()
{
    app_cfg_t *instance = (app_cfg_t *)malloc(sizeof(app_cfg_t));

    if(NULL!=instance){
        memset(instance,0,sizeof(app_cfg_t));
        int i=0;
        for(i=0;i<RAW2X_MAX_FILES;i++)
            instance->files[i]=NULL;
        instance->file_count=0;
        instance->test=false;
        instance->fs=NULL;
        instance->fd=-1;
        instance->oflags=OROF_CONSOLE|OROF_CSV;
    }
    return instance;
}

static void app_cfg_destroy(app_cfg_t **pself)
{
    if(NULL!=pself){
        app_cfg_t *self = (app_cfg_t *)(*pself);
        if(NULL!=self){
            int i=0;
            for(i=0;i<self->file_count;i++){
                if(NULL!=self->files[i])
                    free(self->files[i]);
            }
            free(self);
            *pself=NULL;
        }
    }
}

static char *s_orecord_csvstr(char **dest, licor_orecord_t *src, char *rid)
{
    char *retval=NULL;
    if(NULL!=dest && NULL!=src){
        if(NULL==*dest){
            *dest=(char *)malloc(LICOR_MAX_CSV_LEN);
        }

        if(NULL != *dest){
            char *obuf = *dest;
            char *cur=*dest;
            sprintf(cur,"%"FMT_U64",%s",src->timestamp,(NULL==rid?"":rid));
            cur=obuf+strlen(obuf);
            int i=0;
            for(i=0;i<FID_LICOR_FIELDS;i++){
                if(isnan(src->fields[i])){
                    sprintf(cur,",");
                }else{
                    sprintf(cur,",%e",src->fields[i]);
                }
                cur=obuf+strlen(obuf);
            }
        }
        retval = *dest;
    }else{
        fprintf(stderr,"ERR - invalid argument (NULL config or record)\n");
    }
    return retval;
}

static int s_out_record(app_cfg_t *cfg, licor_orecord_t *record, char *rid)
{
    int retval=-1;

    if(NULL!=cfg && NULL!=record){
        retval=0;
        char *out_buf=NULL;

        // select format
        if(cfg->oflags&OROF_CSV){
            out_buf = s_orecord_csvstr(&out_buf,record,rid);
        }
        // else other format options...

        // send to one or more outputs...
        if(NULL!=out_buf ){
            if(cfg->oflags&OROF_CONSOLE){
            	fprintf(stdout,"%s\n",out_buf);
            }
            free(out_buf);
        }

        retval=0;
    }else{
        fprintf(stderr,"ERR - invalid argument (NULL config or record)\n");
    }
    return retval;
}

static int s_parse_records(app_cfg_t *cfg)
{
    int retval=-1;

    if(NULL!=cfg && cfg->fd>0){
        char *line=NULL;
        size_t llen=0;

        int parsed_count=0;
        int invalid_count=0;

        cfg->fs=fdopen(cfg->fd,"r");

        while( (NULL!=cfg->fs) &&
              (feof(cfg->fs)==0) &&
              (getline(&line, &llen, cfg->fs)>0) &&
              !g_interrupt){

            fprintf(stdout,"%s",line);
            char *ts_str=strtok(line,",");
            char *rid_str=strtok(NULL,",");
            char *xml_str=strtok(NULL,",");

            // if only two fields,
            // it's old format w/o record ID
            if(NULL!=rid_str && NULL==xml_str){
                xml_str=rid_str;
                rid_str=NULL;
            }

            licor_orecord_t record, *prec=&record;
            // initialize the record
            memset(prec,0,sizeof(licor_orecord_t));
            int i=0;
            for(i=0;i<FID_LICOR_FIELDS;i++){
                prec->fields[i]=NAN;
            }
            if(NULL!=ts_str && NULL!=xml_str ){
                struct tm ts_tm;
                if(NULL!=strstr(xml_str,"<li850>") &&
                   NULL!=strptime(ts_str,"%Y-%m-%dT%T",&ts_tm)){
                    time_t ts_tt = mktime(&ts_tm);
                    uint64_t ts_u64=ts_tt;

                    if(NULL!=licor_parse_raw(prec,xml_str,ts_u64)){
                        parsed_count++;
                        s_out_record(cfg,prec,rid_str);
                    }else{
                        invalid_count++;
                    }
                }else{
                    invalid_count++;
                }
            }else{
                invalid_count++;
            }

            ZERO_MEM(line,llen);
        }
        VPRINT("%s: lines[%d] valid[%d] invalid[%d]\n",cfg->files[cfg->idx],(parsed_count+invalid_count),parsed_count,invalid_count);
    }else{
        fprintf(stderr,"ERR - invalid argument (NULL config or file)\n");
    }
    return retval;
}

static int s_app_main(app_cfg_t *cfg)
{
    int retval=-1;
    if(NULL!=cfg){

        if(cfg->oflags&OROF_CONSOLE){
            fprintf(stdout,"%s\n",LICOR_CSV_HEADER);
        }

        int i=0;
        for(i=0;i<cfg->file_count;i++)
            if( NULL!=cfg->files[i]){
                if( (cfg->fd=open(cfg->files[i],O_RDONLY))>0){
                    cfg->idx=i;
                    lseek(cfg->fd,0,SEEK_SET);
                    s_parse_records(cfg);
                    close(cfg->fd);
                }else{
                    fprintf(stderr,"ERR - fopen [%s] failed [%d/%s]\n",cfg->files[i], errno,strerror(errno));
                }
            }

        if(g_interrupt)VPRINT("interrupted - exiting\n");
        retval=0;
    }else{
        fprintf(stderr,"ERR - invalid argument (NULL config)\n");

    }
    return retval;
}

int main(int argc,char **argv)
{
    int retval=-1;

#if defined(__WIN32) || defined(__WIN64)
    signal(SIGINT,s_termination_handler);
#else
    // configure signal handling
    // for main thread
    struct sigaction saStruct;

    sigemptyset(&saStruct.sa_mask);
    saStruct.sa_flags = 0;
    saStruct.sa_handler = s_termination_handler;
    sigaction(SIGINT, &saStruct, NULL);
#endif

    app_cfg_t *cfg = app_cfg_new();

    // parse args...
    parse_args(argc,argv,cfg);

    // call main
    retval=s_app_main(cfg);

    // release resources
    app_cfg_destroy(&cfg);

    return retval;
}

