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

   PROGRAM:  DB2TODB3
   USAGE:    DB2TODB3 <control file>

   This program creates a CODAS version 3 database from CODAS version 2
   block files.  It converts CODAS block files written under version 2 into
   CODAS version 3 block files and recreates the block directory file
   for the converted block files.

   The <control file> must contain the following keywords and information:

      DB_NAME  <new database name>
      PRD_NAME <version 3 producer definition file name>
      <version 2 block file #1>
      <version 2 block file #2>
        .
        .
        .

   See the file DB2TODB3.CNT for an example of the control file.
   The producer definition file must use the new version 3 format
   for producer definition files.  See CODAS3.DOC for a description.
   See MWADCP.DEF for an example.

   1989/05/02 - J. Ranada - University of Hawaii JIMAR

*/
#include "vc.h"
#define BUFSIZE 50

#if (COMPILER == TURBO_C)
#include <math.h>
#endif

#if PROTOTYPE_ALLOWED
void main(int argc, char *argv[])
#else
void main(argc, argv)
int argc;
char *argv[];
#endif
{
   FILE *fp_cnt;
   char buf[BUFSIZE];
   int db_id = 1;
   int im = 0;
   int ier;
   FILE_NAME_TYPE cnt_file,
                  db_name,
                  prd_name;
   char *fn_new_block;

#if (COMPILER == TURBO_C)
   sin(0.0);
#endif

   if (argc < 2)
   {
      printf("\n ENTER CONTROL FILE NAME ==> ");
      scanf(" %79s", cnt_file);
   }
   else
      strncpy(cnt_file, argv[1], 79);
   cnt_file[79] = '\0';

   if ((fp_cnt = fopen(cnt_file, "rt")) == NULL)
   {
      printf("\n ERROR: Unable to open control file %s\n", cnt_file);
      return;
   }

   if ((getwords_nc(fp_cnt, buf, BUFSIZE, 2) != 2) ||
      (sscanf(buf, "DB_NAME %79s", db_name) != 1))
   {
      printf("\n ERROR: Scanning control file for database name\n");
      return;
   }

   if ((getwords_nc(fp_cnt, buf, BUFSIZE, 2) != 2) ||
      (sscanf(buf, "PRD_NAME %79s", prd_name) != 1))
   {
      printf("\n ERROR: Scanning control file for producer definition file name\n");
      return;
   }
   printf("\n DATABASE NAME: %s", db_name);
   printf("\n PRODUCER DEFINITION FILE: %s\n", prd_name);

   DBCREATE(&db_id, db_name, prd_name, &im, &ier);
   if (DBERROR(&ier, "in DBCREATE")) return;

   while (getword_nc(fp_cnt, buf, BUFSIZE))
   {
      printf("\n %s ---> ", buf);
      fn_new_block = add_block_file(buf);
      if (fn_new_block == NULL)
      {
         printf("\n ERROR: Adding block file = %s", buf);
         goto close_db;
      }
      printf("%s", fn_new_block);
   }

   close_db:
      DBCLOSE(&ier);
      DBERROR(&ier, "in DBCLOSE");
}

/*-----------------------------------------------------------------------------

   FUNCTION:  add_block_file

      It creates a block directory entry for the block file to be created
      and calls convert_block_file to perform the conversion.

   PARAMETER:

      fn_old_block = pointer to name of version 2 block file to be converted

   RETURNS:

      pointer to name of version 3 block file created, if successful
      NULL otherwis

*/
#if PROTOTYPE_ALLOWED
char *add_block_file(FILE_NAME_TYPE fn_old_block)
#else
char *add_block_file(fn_old_block)
FILE_NAME_TYPE fn_old_block;
#endif
{
   FILE *fp_new_block;
/*
 ---> Set up a block directory entry and set the file ID.
*/
   create_block_dir_entry();
/*
  ---> SET NAME OF BLOCK FILE
*/
   sprintf( (db->block_file+db->path_nchar),
      db->block_dir_hdr.block_file_template,
      db->block_dir_entry_ptr->file_id);

   if (!EXISTS(db->block_file))
   {
      if (convert_block_file(fn_old_block, db->block_file))
      {
         printf("\n ERROR: Converting %s to %s", fn_old_block, db->block_file);
         goto error_found;
      }
   }
/*
 ---> If a file with the new name exists, but this name is not the
      same as that of the current old block file, then there is a
      danger that the old file names may be out of sequence, or for
      some other reason we may be in danger of accidentally
      overwriting a block file.  The file name comparison is done
      without case sensitivity.
*/
   else if (stricmp(fn_old_block, db->block_file) != 0)
   {
      printf("\n ERROR: in add_block_file");
      printf("\n        File name duplicates an old one, but the files do not correspond.\n");
      goto error_found;
   }

   if ( (fp_new_block = fopen(db->block_file, "rb")) == NULL )
   {
      printf("\n ERROR: Can't open %s\n", db->block_file);
      goto error_found;
   }
   fread(&(db->block_hdr), BLOCK_HDR_SIZE, 1, fp_new_block);
   fclose(fp_new_block);

   update_block_dir_entry();
   if (add_block_dir_entry())
      goto error_found;
   db->new_block_is_open = 0;
   db->block_dir_modified = 1;
   return(db->block_file);

   error_found:
      report_db_error("add_block_file");
      return(NULL);
}

