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

   CHTIME.C

   This is a limited-use utility for shifting the times in
   the WEPOCS II ADCP database.  It could be expanded to be
   more general.  For now it can shift only by a constant
   amount for a given block or range of blocks.


                     Eric Firing
                     Wed  02-22-1989
----------------------------------------------------------------

control file structure:                                         

     dbname:    (e.g. WEP)
     seconds=   (an integer)
     block1=
     block2=

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

#include "dbinc.h"
#include "ioserv.h"
#include "use_db.h"
#include "dpmask.h" /* TIME_CORRECTED */

#define  TRUE  1
#define  FALSE 0



/*
Main routine
*/

#if PROTOTYPE_ALLOWED
void do_it(FILE *fp_cnt)
#else
void do_it(fp_cnt)
FILE *fp_cnt;
#endif
{
   int      IERR = 0;
   int      i, nprof, STEPS = 1;
   struct { int block; int profile; } this; 

/***************************************************************
   static variables needed for initialization of
   the parameter array

*/

static   FILE_NAME_TYPE  dbname;
static   int   block1, block2, seconds;

static   PARAMETER_LIST_ENTRY_TYPE parameters[] =
   {
      { " dbname:",     " %79s", dbname,                 TYPE_STRING },
      { " seconds=",    " %d",   &seconds,               TYPE_INT    },
      { " block1=",     " %d",   &block1,                TYPE_INT    },
      { " block2=",     " %d",   &block2,                TYPE_INT    },
      { NULL,           NULL,    NULL,                   0           }
   };                   /* parameters[] */


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

   this.block = this.profile = 0;
   if(get_parameters(fp_cnt, parameters, NULL))  exit(-1);

   check_dbopen(1, dbname, READ_WRITE, DIR_IN_MEMORY);

/*
---> Start the loop through block range in the control file.
     This could be done using only a single DBSRCH, and all
     other moves with DBMOVE, but that would cause a final DBMOVE
     into a block beyond the last one desired.  This could be
     prevented with an if-statement, so it is still an alternative.
     The present method is more easily modified to work with an
     arbitrary list of blocks instead of a range of consecutive
     blocks.
*/
   for (this.block = block1; this.block <= block2; this.block++)
   {
      check_dbsrch(BLOCK_PROFILE_SEARCH, (char *)&this);
/*
---> Start the loop through profiles within the time range.
*/
      nprof = get_nprofs();
      for (i=0; i<nprof; i++)
      {
         db->profile_dir_entry_ptr->time += seconds;
         write_profile_dir_entry();
         /* Move to the next profile in THIS block; do not
            move into the following block.  Otherwise the
            file date of the following block would be changed,
            even if it is not a block on the list of blocks to
            be modified by this program.
         */
         if (i < nprof-1)   /* last profile is nprof-1 */
         {
            DBMOVE(&STEPS, &IERR);   /* IERR is checked at the start of the loop */
            if(IERR)
            {
               printf("\nerror in DBMOVE, block %d, profile %d",
                        this.block, i);
               goto close_all;
            }
         }

      }  /* end of loop through profiles within a block */
      /* Update and close the present block.  */
      if (set_block_dpmask(TIME_CORRECTED)) goto close_all;
      db->block_modified = TRUE;
      IERR = close_block();
      if(IERR)
      {
         printf("\nerror %d in close_block, block %d", IERR,
                  this.block);
         goto close_all;
      }

   }  /* end of loop through blocks */
   close_all:
   DBCLOSE(&IERR);
   check_error(IERR, "DBCLOSE");
   printf("\nCHTIME completed.\n");
   return;
}

