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

      File: chprodid.c

      Purpose: Update the producer id within a codas database
               

      Input: CODAS database
      Output: CODAS database

     Control file options

     dbname:           {database name}
     producer_id:           {new producer id}
     P.Caldwell 10/feb/95
     ----------------------------------------------------------*/

#include <stdio.h>
#include <geninc.h>
#include <ioserv.h>  /* execute_options, error_found, check_error */
#include <use_db.h>  /* dbopen_cnf */

static FILE_NAME_TYPE dbname;
static char prd_id[1+sizeof(PRODUCER_ID_TYPE)]; /* one more for NULL terminator */
static OPTION_TYPE extract_options[] =
{
   "end"              , 0          , NULL         , NULL,
   "dbname:"          , TYPE_STRING, op_get_param , dbname,
   "producer_id:"     , TYPE_STRING, op_get_param , prd_id, 
   NULL               , 0          , NULL         , NULL
};


#if PROTOTYPE_ALLOWED
int do_it(FILE *fpcnt)
#else
int do_it(fpcnt)
FILE *fpcnt;
#endif
{
   int block_min, block_max;
   int iblk, ierr=0;
   unsigned int prd_id_size = sizeof(PRODUCER_ID_TYPE);
   int prd_id_type = PRODUCER_ID;
   int nprf;

   /* set defaults */
   strcpy(dbname, "");
   set_byte((UBYTE *) prd_id, '\0', 1+sizeof(PRODUCER_ID_TYPE));

   /* read user-specified options: */
   if (execute_options(fpcnt, extract_options, ECHO) < 0) return(-1);
   if (strcmp(dbname, "") == 0 || strcmp(prd_id, "") == 0)
   {
      fprintf(stderr, "\nA dbname: and producer_id: must be specified in the control file.\n");
      return(-1);
   }

   /* OPEN DATABASE */
   dbopen_cnf(1, dbname, READ_WRITE, DIR_IN_MEMORY);

   block_min = 0;
   block_max = get_nblocks();

   iblk=block_min;
   /* loop thru each block */
   do 
   {
     DBPUT(&prd_id_type, prd_id, &prd_id_size, &ierr);
     if (error_found(ierr, "DBPUT PRODUCER_ID")) goto close_all;

     nprf = get_nprofs();
     DBMOVE( &nprf, &ierr );
     iblk++;
   } while( iblk < block_max );

   close_all:
   DBCLOSE(&ierr);
   check_error(ierr, "DBCLOSE");
   return(0);
}
