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

       This is a block-profile search version of LOGSCAN.C.

       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 block/profile number indicated
       in a subsequent log line.  The augmented version of the <infile> is
       written to the <outfile>.

       J. Ranada - 08/29/88 - University of Hawaii JIMAR

       Modified 05/25/89 to skip over comment lines (i.e. those preceded by '%')
                                                                              *
 *****************************************************************************/

#include "geninc.h"
#include "data_id.h"   /* CODAS OCEANOGRAPHIC DATA VARIABLE NAMES & IDs      */
#include "ioserv.h"    /* check_fopen(), error_found(), non_blank(), report_msg() */
#include "use_db.h"    /* db*_cnf(), check_blkprf(), 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(int blkprf[]);
#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;
   int              s_blkprf[2], c_blkprf[2], e_blkprf[2];

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

   fpin = check_fopen(infile, "r");
   fpout = check_fopen(outfile, "w");
   set_msg_file(fpout);      /* echo errors/warning 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);
      /*---------------------------------------------------------------------
          If the line is empty or a comment, copy the line 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 block-profile is
          indicated in a line containing just the tag and the block-profile
          index.  The end block-profile is in the subsequent log line structure.
        ---------------------------------------------------------------------*/
      else if (*ch == '1')
      {
         if (sscanf(line, "%*d %d %d", &s_blkprf[0], &s_blkprf[1]) != 2)
         {
            report_msg(
               "\n%% ERROR: Scanning start block-profile number from line:\n%  ");
            report_msg(line);
            goto close;
         }                                   /* gap start block & profile no. */

         do
         {
            if (fgets(line, 255, fpin) == NULL)
            {
               report_msg(
                  "\n%% ERROR: Missing end block-profile number for line:\n%  ");
               report_msg(line);
               goto close;
            }
            if ((is_comment = comment(non_blank(line))) == 1)
               fputs(line, fpout);
         } while (is_comment);

         if (sscanf(line, "%*d %d %d", &e_blkprf[0], &e_blkprf[1]) != 2)
         {
            report_msg(
               "\n%% ERROR: Scanning end block-profile number from line:\n%  ");
            report_msg(line);
            goto close;
         }                                                    /* gap end time */

         if (dbsrch_cnf(BLOCK_PROFILE_SEARCH, (char *) s_blkprf)) goto close;

         while (check_blkprf(c_blkprf, e_blkprf) == -1)  /* log while b-p no. */
         {                                            /* is less than end b-p */
            if (log_data(c_blkprf)) goto close;
            if (dbmove_cnf(1)) goto close;
         }
         fputs(line, fpout); /* copy end-of-gap 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", &s_blkprf[0], &s_blkprf[1]) != 2)
         {
            report_msg(
               "\n%% ERROR: Scanning start block-profile no. from line:\n%  ");
            report_msg(line);
            goto close;
         }

         do
         {
            fputs(line, fpout);
            if (fgets(line, 255, fpin) == NULL)
            {
               report_msg(
                  "\n%% ERROR: Missing end block-profile no. for line:\n%  ");
               report_msg(line);
               goto close;
            }
         } while (comment(non_blank(line)));

         if (sscanf(line, "%d %d", &e_blkprf[0], &e_blkprf[1]) != 2)
         {
            report_msg(
               "\n%% ERROR: Scanning end block-profile no. from line:\n%  ");
            report_msg(line);
            goto close;
         }

         if (dbsrch_cnf(BLOCK_PROFILE_SEARCH, (char *) s_blkprf)) goto close;

         if (dbmove_cnf(1)) goto close; /* skip to right after start blkprf */

         while (check_blkprf(c_blkprf, e_blkprf) < 1)    /* log until end b-p */
         {
            if (log_data(c_blkprf)) 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(int blkprf[])
#else
int log_data(blkprf)
int blkprf[];
#endif
{
   YMDHMS_TIME_TYPE ptime;
   unsigned int n = sizeof(YMDHMS_TIME_TYPE);

   if (dbget_cnf(TIME, (char *) &ptime, &n, "getting profile time"))
      return(1);

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

