/*----------------------------------------------------------------------------
 * 
 * delprf.c -- Delete profiles from list of YMDHMS times
 * 
 * Author          : Jaccard Pierre
 * Created On      : 2000/08/28 15:04:29
 * Last Modified By: Jaccard Pierre
 * Last Modified On: 2000/11/24 11:01:11
 * Directory       : /home1/jaccard/data/codas/2000-11-21/codas3/gfi/src/apps/db/
 * ------------------------------------------------------------------------ * 
 * VERSION CONTROL: 
 * 
 *   $Author$
 *   $Date$
 *   $Id$
 *   $Name$
 *   $Revision$
 *   $State$
 *   $Log$
 * 
 *--------------------------------------------------------------------------*/

/*
 * USAGE
 *
 *   delblk <database> <file>
 *
 * where <file> contains a list of YMDHMS profile times to delete from the
 * database. A log file delblk.log is created/appended with all profiles being 
 * deleted. If a given profile time is within 1 second of the real profile
 * time from the database, the profile is deleted. Otherwise, the profile is
 * skipped.
 *
 * I would like to provide an option for undeleting profiles.
 */
 
#include <time.h>
#include <stdlib.h>

#include "geninc.h"     /* YMDHMS_TIME_TYPE, DBDELPRF(), DBDELBLK(), ... */
#include "use_db.h"     /* check_db*(), db*_cnf() */
#include "ioserv.h"
#include "dbglo.h"

#if PROTOTYPE_ALLOWED
int main(int argc, char *argv[])
#else
int main(argc, argv)
int argc;
char *argv[];
#endif
{
   YMDHMS_TIME_TYPE prftime, dbtime;
   FILE_NAME_TYPE   dbname, infile;
   FILE *fp_in=NULL, *fp_log=NULL;
   int n, ierr=0;
   unsigned int nt=sizeof(YMDHMS_TIME_TYPE);
   char buff[80], str[200];
   
   time_t systime;
   
   /*
    * Arguments
    */
   if (argc < 2)
     return(EXIT_FAILURE); 
   strcpy(dbname, argv[1]);
   strcpy(infile, argv[2]);

   /*
    * Initializations
    */
   fp_in = fopen(infile, "r");
   ierr = error_found(fp_in == NULL, "Opening input file");
   if(ierr)
     goto on_error;
   fp_log = fopen("delprf.log", "a");
   ierr = error_found(fp_log == NULL, "Opening log file");
   if(ierr)
     goto on_error;
   ierr = dbopen_cnf(1, dbname, READ_WRITE, 0);
   if(ierr)
     goto on_error;
   set_msg_file(fp_log);
   report_msg("\n\n%% DELPRF: ");
   time(&systime);
   report_msg(asctime(gmtime(&systime)));

   
   /*
    * Loop through input file
    */
   while(getline_nc(fp_in, buff, 80) > 0){
     
     /*
      * Get time of profile to be deleted
      */
     n = sscanf(buff, "%4hu%*c%2hu%*c%2hu %2hu%*c%2hu%*c%2hu",
                &(prftime.year), &(prftime.month), &(prftime.day),
                &(prftime.hour), &(prftime.minute), &(prftime.second));
     if(error_found(n != 6, "Reading profile time from file\n"))
       goto on_error;
     
     /*
      * Search for this profile
      */
     ierr = dbsrch_cnf(TIME_SEARCH, (char *) &prftime);
     if(ierr && 
        (ierr != SEARCH_BEYOND_END) && 
        (ierr != SEARCH_BEFORE_BEGINNING) )
       goto on_error;
     
     /*
      * Get profile time
      */
     ierr = dbget_cnf(TIME, (char *) &dbtime, &nt, "getting current time");
     if(ierr)
       goto on_error;
     
     /*
      * Compare profile times
      */
     sprintf(str, "\n%04hu/%02hu/%02hu %02hu:%02hu:%02hu %03d %03d ... ",
             prftime.year, prftime.month, prftime.day, 
             prftime.hour, prftime.minute, prftime.second,
             db->block_dir_index, db->profile_dir_index);
     report_msg(str);
     if(abs_val(TIMDIF(&prftime, &dbtime)) < 1.0){
       DBDELBLK(&ierr);
       if (DBERROR(&ierr, "in DELBLK")) 
         goto on_error;
       report_msg("DELETE!");
     }
     else{
       report_msg("*** SKIP! ***");
     }
     
   }

   report_msg("\n%% DELPRF: DONE");

 on_error:
   
   if(fp_in){
     fclose(fp_in);
     fp_in = NULL;
   }

   if(fp_log){
     fclose(fp_log);
     fp_log = NULL;
     set_msg_file(fp_log);
   }

   dbclose_cnf();

   if(ierr)
     return(EXIT_FAILURE);
   else
     return(EXIT_SUCCESS);
}





