/*****************************************************************************
*
    PROGRAM:  LOGSCAN.C
    USAGE:    LOGSCAN <dbname> <infile> <outfile>

       This program reads in each line from the <infile> listing of
       block-profiles that have been detected as bad.  If the tag element
       in the line structure is set to 1 or 2, the program augments the file
       with data for block-profiles from the database specified by <dbname>,
       in order to fill in the gap up to the time indicated in a subsequent
       log line.  The augmented version of the <infile> is written to the
       <outfile>.

       J. Ranada - 02/26/88 - University of Hawaii JIMAR

       5/26/89 - modified to skip over comment lines (i.e., those preceded
                 by a '%')
      12/14/89 - fixed bug introduced in converting to version 3: failed to
                 add 1900 to end year in case of tag 2, causing no profiles
                 to be found

                                                                             *
 *****************************************************************************/

#include "geninc.h"
#include "data_id.h"                       /* CODAS variable names and IDs   */
#include "ioserv.h"                        /* check_fopen()                  */
#include "use_db.h" /* db*_cnf(), check_prftime(), check_prftime(), write_ymdhms_time() */
#include "dbedit.h"                        /* comment()    */

#define dbname argv[1]
#define infile argv[2]
#define outfile argv[3]

#if PROTOTYPE_ALLOWED
int log_data(YMDHMS_TIME_TYPE *logtime);
#else
int log_data();
#endif

FILE *fpin, *fpout;

#if PROTOTYPE_ALLOWED
int main(int argc, char *argv[])
#else
int main(argc, argv)
int argc;
char *argv[];
#endif
{
   char             line[255], *ch;
   int              is_comment;
   YMDHMS_TIME_TYPE stime, etime, ptime;

   if (argc < 4)
   {
      printf("\n ERROR: Invalid number of arguments on command line.");
      printf("\n USAGE: logscan <dbname> <infile name> <outfile name>\n\n");
      exit(-1);
   }

   fpin = check_fopen(infile, "rt");
   fpout = check_fopen(outfile, "wt");
   set_msg_file(fpout);   /* echo errors/warnings to output file */
   if (dbopen_cnf(1, dbname, READ_ONLY, DIR_IN_MEMORY)) exit(-1);

   while (fgets(line, 255, fpin) != NULL)
   {
      ch = non_blank(line);
      /*---------------------------------------------------------------------
          Copy any comment or blank lines over.
          If the tag is 0, then there is no gap to be filled.  The log lines
          are merely copied over to the output file.
        ---------------------------------------------------------------------*/
      if (comment(ch) || (*ch == '0'))
         fputs(line, fpout);

      /*---------------------------------------------------------------------
          A tag of 1 means the gap to be filled is prior to a block-profile
          already logged in the input file.  The start time is indicated in
          a line containing just the tag and the start time.  The end time
          is in the subsequent log line structure.
        ---------------------------------------------------------------------*/
      else if (*ch == '1')
      {
         if (sscanf(line, "%*d %hd/%hd/%hd %hd:%hd:%hd",
            &stime.year, &stime.month, &stime.day,
            &stime.hour, &stime.minute, &stime.second) != 6)
         {
            report_msg("\n%% ERROR: Scanning start time from line:\n\n%  ");
            report_msg(line);
            goto close;
         }                                                 /* gap start time */
         stime.year = yr4digit(stime.year);

         do
         {
            if (fgets(line, 255, fpin) == NULL)
            {
               report_msg("\n%% ERROR: Missing end time for start time = ");
               write_ymdhms_time(stdout, &stime);
               write_ymdhms_time(fpout, &stime);
               goto close;
            }
            if ((is_comment = comment(non_blank(line))) == 1)
               fputs(line, fpout);
         } while (is_comment);

         if (sscanf(line, "%*d %*d %*d %hd/%hd/%hd %hd:%hd:%hd",
            &etime.year, &etime.month, &etime.day,
            &etime.hour, &etime.minute, &etime.second) != 6)
         {
            report_msg("\n%% ERROR: Scanning end time from line:\n%  ");
            report_msg(line);
            goto close;
         }                                                   /* gap end time */
         etime.year = yr4digit(etime.year);

         if (dbsrch_cnf(TIME_SEARCH, (char *) &stime)) goto close;

         while (check_prftime(&ptime, &etime) == -1)/* log while profile time */
         {                                          /* is less than end time  */
            if (log_data(&ptime)) goto close;
            if (dbmove_cnf(1)) goto close;
         }

         fputs(line, fpout);        /* copy end time log line to output file */
      }

      /*--------------------------------------------------------------------
          A tag of 2 means the gap to be filled comes after a block-profile
          already logged in the input file.  The start time is therefore in
          a regular log line structure, while the end time is in a subsequent
          line all by itself.
        ---------------------------------------------------------------------*/
      else if (*ch == '2')
      {
         *ch = '0';                          /* reset tag for future use */
         if (sscanf(line, "%*d %*d %*d %hd/%hd/%hd %hd:%hd:%hd",
            &stime.year, &stime.month, &stime.day,
            &stime.hour, &stime.minute, &stime.second) != 6) /* gap start time */
         {
            report_msg("\n%% ERROR: Scanning start time from line\n\n%  ");
            report_msg(line);
            goto close;
         }
         stime.year = yr4digit(stime.year);

         do
         {
            fputs(line, fpout);
            if (fgets(line, 255, fpin) == NULL)
            {
               report_msg("\n%% ERROR: Missing end time for start time = ");
               write_ymdhms_time(stdout, &stime);
               write_ymdhms_time(fpout, &stime);
               goto close;
            }
         } while (comment(non_blank(line)));

         if (sscanf(line, "%hd/%hd/%hd %hd:%hd:%hd",
            &etime.year, &etime.month, &etime.day,
            &etime.hour, &etime.minute, &etime.second) != 6)    /* gap end time */
         {
            report_msg("\n%% ERROR: Scanning end time from line\n\n%  ");
            report_msg(line);
            goto close;
         }
         etime.year = yr4digit(etime.year);

         if (dbsrch_cnf(TIME_SEARCH, (char *) &stime)) goto close;

         if (check_prftime(&ptime, &stime) == 0)
            if (dbmove_cnf(1)) goto close;/* skip to right after start time */

         while (check_prftime(&ptime, &etime) < 1)      /* log until end time */
         {
            if (log_data(&ptime)) goto close;
            if (dbmove_cnf(1)) goto close;
         }
      }
   } /* end while fgets */

close:
   fclose(fpout);
   fclose(fpin);
   dbclose_cnf();
   return(0);
}

/*---------------------------------------------------------------------------
    log_data writes block & profile numbers and time of profiles to fill
    in the gaps noted in the infile.  It also puts some dummy values for
    the other fields of the log line structure.
  ---------------------------------------------------------------------------*/
#if PROTOTYPE_ALLOWED
int log_data(YMDHMS_TIME_TYPE *logtime)
#else
int log_data(logtime)
YMDHMS_TIME_TYPE *logtime;
#endif
{
   int iblkprf[2];
   unsigned int n;

   n = 2 * sizeof(int);
   if (dbget_cnf(BLOCK_PROFILE_INDEX, (char *) iblkprf, &n,
      "getting block-profile index")) return(1);

   fprintf(fpout,"  0 %3d %3d ", iblkprf[0], iblkprf[1]);
   write_ymdhms_time(fpout, logtime);
   fputs(
      "    N 32767 32767  -1 255 255     -1.0 32767     0     0     0 32767\n",
      fpout);                    /* default values */
   return(0);
}

