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

   FILE:  lstblock.C  List Block Times
   USAGE: lstblock <database-name> <output file name>

          Utility for writing to a file the time range of
          each block in a database.

          On the command line are the database name and the
          name of the output file.

                            Eric Firing
                             88-03-13

          Modified 88-04-14 to write the block file name along with
          the block number.

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

#include "geninc.h"   /* DB*() */
#include "ioserv.h"   /* check_fopen(), check_error() */
#include "use_db.h"   /* get_nblocks(), get_nprofs() */

static char dbname[128], filename[128];


#if PROTOTYPE_ALLOWED
int main(int argc, char *argv[])
#else
int main(argc, argv)
int argc;
char *argv[];
#endif
{
   FILE_NAME_TYPE block_filename;
   unsigned int n;
   int      iarg, ierr, iblkprf[2];
   int      write_dp_mask = 0;
   int      search_mode = BLOCK_PROFILE_SEARCH,
            type,
            dbid = 1,                  /* database id */
            accmode = READ_ONLY,
            memmode = DIR_IN_MEMORY;
   YMDHMS_TIME_TYPE t1, t2;
   ULONG data_proc_mask;
   FILE *fp_out;
   int nprofs, nblocks;
   int i;

   check_error( (argc < 3),
      "command line\n   USAGE: lstblock [-p] <dbname> <output file name>\n\n");

   iarg = 1;
   while (argv[iarg][0] == '-')
   {
      if (strchr(argv[iarg], 'p') )
         write_dp_mask = 1;
      /* if (strchr(argv[iarg], 't') )
         time_key = 1; */
      iarg++;
   }
   strncpy(dbname, argv[iarg++], 127);
   strncpy(filename, argv[iarg++], 127);

   DBOPEN(&dbid, dbname, &accmode, &memmode, &ierr);
   check_error(ierr, "DBOPEN");
   printf("\n\n DATABASE:  %s SUCCESSFULLY OPENED\n",dbname);

   fp_out = check_fopen(filename,"w");

   check_error( ((nblocks = get_nblocks()) < 0), "nblocks");

   fprintf(fp_out, "/* Database summary for: %s */", dbname);
   fprintf(fp_out, "\n/* Number of blocks: %d */", nblocks);

   for (i=0; i<nblocks; i++)
   {
      iblkprf[0] = i;
      iblkprf[1] = 0;
      DBSRCH(&search_mode, (char *) iblkprf, &ierr);
      check_error(ierr, "DBSRCH");

      type = TIME;
      n = sizeof(YMDHMS_TIME_TYPE);
      DBGET(&type, (char *) &t1, &n, &ierr);
      check_error(ierr, "DBGET");

      check_error( ((nprofs = get_nprofs()) < 0), "get_nprofs");

      iblkprf[1] = nprofs-1;
      DBSRCH(&search_mode, (char *) iblkprf, &ierr);
      check_error(ierr, "DBSRCH");

      n = sizeof(YMDHMS_TIME_TYPE);
      DBGET(&type, (char *) &t2, &n, &ierr);
      check_error(ierr, "DBGET");

      type = BLOCK_FILE;
      n = sizeof(block_filename);
      DBGET(&type, block_filename, &n, &ierr);
      check_error(ierr, "DBGET");

      fprintf(fp_out, "\n/* %3d  n= %3d  %12s */    ",
         iblkprf[0], nprofs, block_filename);
      write_ymdhms_time(fp_out, &t1);
      fprintf(fp_out, "  to   ");
      write_ymdhms_time(fp_out, &t2);

      if (write_dp_mask)
      {
         type = DATA_PROC_MASK;
         n = sizeof(data_proc_mask);
         DBGET(&type, (char *) &data_proc_mask, &n, &ierr);
         check_error(ierr, "DBGET");

         fprintf(fp_out, " %lx", data_proc_mask);
      }

   }
   fprintf(fp_out, "\n");
   fclose(fp_out);
   dbclose_cnf();
   return(0);
}

