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

    PROGRAM:  SET_BIT.C
    USAGE:    SET_BIT <bitmask> <dbname> [start-block] [end-block]

    This program can be used to set or clear a bit in the data processing mask
    of a CODAS3 ADCP database.  The value of <bitmask> is taken from the
    ADCP\INCLUDE\DPMASK.H, which defines masks for setting and clearing any
    given bit.  The <dbname> is the name (including path) of the database
    for which the data processing mask is to be altered.  The user may
    optionally provide the [start-block] and [end-block] parameters, if
    only a subset of blocks are to be altered; otherwise, the entire
    database will be altered.

*/

#include "geninc.h"
#include "use_db.h"  /* db*_cnf() */
#include "dbedit.h"  /* set_block_dpmask() */
#include "dpmask.h"  /* dpmask_list[] */

#define dbname  argv[2]
#define start_block argv[3]
#define end_block argv[4]

typedef struct
{
   int min, max;
} RANGE;

#if PROTOTYPE_ALLOWED
void main(int argc, char *argv[])
#else
void main(argc, argv)
int argc;
char *argv[];
#endif
{
   RANGE block_range;
   char answer[2], *mask_name;
   int iblkprf[2];
   int bitmask, ierr, type = BLOCK_PROFILE_SEARCH;

   if ( (argc != 3) && (argc != 5) )
   {
      printf("\n ERROR: Invalid no. of arguments");
      printf("\n USAGE: SET_BIT <bitmask> <dbname> [start-block] [end-block]\n");
      exit(-1);
   }
   bitmask = atoi(argv[1]);
   if ( (mask_name = get_name(dpmask_list, bitmask)) == NULL )
   {
      printf("\n ERROR: Unrecognized bitmask option %d", bitmask);
      printf("\n        Check DPMASK.H\n\n");
      exit(-1);
   }
   if (dbopen_cnf(1, dbname, READ_WRITE, DIR_IN_MEMORY)) exit(-1);
   if (argc == 3)
   {
      block_range.min = 0;
      block_range.max = get_nblocks() - 1;
   }
   else
   {
      block_range.min = atoi(start_block);
      block_range.max = atoi(end_block);
   }

   printf("\n\n DATABASE:  %s", dbname);
   printf("\n BIT MASK:  %s", mask_name);
   printf("\n MODIFY BLOCKS:  %d to %d", block_range.min, block_range.max);
   printf("\n\n Continue (y/n)? ");
   scanf("%1s", answer);
   if (toupper( (int) answer[0]) != 'Y') goto close;

   iblkprf[0] = block_range.min;  /* position database pointer at min. block */
   iblkprf[1] = 0;
   if (dbsrch_cnf(BLOCK_PROFILE_SEARCH, (char *) iblkprf)) goto close;

   do
   {
      printf("\n Processing block %3d", iblkprf[0]);
      if (set_block_dpmask(bitmask)) goto close;

      iblkprf[0]++;                                    /* process next block */
      if (iblkprf[0] > block_range.max) break;       /* reached end of range */
      else                         /* advance database pointer to next block */
      {
         DBSRCH(&type, (char *) iblkprf, &ierr);
         if (ierr)
            if (ierr != NO_SUCH_BLOCK_PROFILE)
            {
               DBERROR(&ierr, " Searching for block");
               printf(" %d", iblkprf[0]);
               goto close;
            }
      }
   } while (ierr != NO_SUCH_BLOCK_PROFILE);       /* reached end of database */
   puts("\n END OF BLOCK RANGE");

close:
   dbclose_cnf();
}

