/*
 ************************************************************************
 *
 *			  BIBLIOGRAPHY DATA BASE
 *		Root module of loading data base from the tape
 *
 * Usage
 *	tf_main	tf_file_name[/location_to_begin_with] report_file_name
 *
 * where tf_file_name specifies the file in BEN SSSR tape internal format
 * (converted to ASCII and preprocessed).
 * The optional key specifies the location (hex number) the processing is to 
 * be started from. On default, 0 is used that means tf_file will be processed 
 * from the very beginning. 
 * When the program failed to load the whole file into the data base,
 * the report file specifies location for the record caused the trouble.
 * When the error is fixed, the program has to be executed with the
 * key being equal the location of the record.
 *
 * Report file name is written with information on records loaded successfully
 * and records failed
 *
 ************************************************************************
 */

#include "stdio.h"


FILE * RDO_file;

				/* Initializing routines		*/
void parse_init(const FILE *file_ptr);
void load_base_init(void);

int parse_new_document();

void load_base_term(void);


/*
 *========================================================================
 *			Initialization routines
 */

static void help()
{
  message("\n\n\tLoad the bibliography data base from the TF file in BEN format");
  message("\n\n\t\ttf_main tf_file_name[/location_to_begin_with] report_file_name");
  message("\n\n");
  exit();
}


/*
 *-----------------------------------------------------------------------
 *				Root module
 *  Parse the command string
 *  Two arguments are expecting (excluding the pgm name as the 0. arg)
 *  one     "input_file_name[/location_to_begin_with]"
 *  another "report_file_name"
 */

main(argc,argv)
int argc;				/* No. of arguments being passed*/
char *argv[];				/* Vector of pointers to args 	*/
{
 FILE *fi, *fr;                         /* Files under operation        */
 int loc;				/* Location to begin with	*/
 int counter;				/* Records counter		*/

 if( argc != 3 )                        /* Too few or too many arguments*/
   help();

 fi  = fopenc(strtok(argv[1],"/"),"rb");
 {
   register char * p = strtok((char *)0,"/");
   if( p == (char *)0 )
     loc = 0;				/* No key specifyed, default loc */
   else					/* is used			 */
     if( sscanf(p,"%x",&loc) != 1 )
     {
       message("\nInvalid key specifyed '%s'",p);
       help();
     }
 }
 if( fseek(fi,loc,0) == EOF )
 {
   message("\nInvalid starting location '%x' was specified",loc);
   help();
 }
 fr  = fopenc(argv[2],"w");

 parse_init(fi);
 for(counter=0; ; counter++)
 {
   fseek(fr,0,0);			/* Report for the new record	*/
   fprintf(fr,"\t\tReport on records being loaded to the data base\n\n");
   fprintf(fr,"%d records has been processed\n",counter);
   fprintf(fr,"Location in input file for the record being processed is %x\n",
           ftell(fi));
   fflush(fr);

   if( parse_new_document() == EOF )
   break;
 }


 message("\n\n========== %d documents has been processed\n\n",counter);

 fclose(fi);
 fclose(fr);
}
