/*
GISSORTSPLIT  This program takes the sorted output from SORTCODE and distributes
the file names to separate parameter files to be used by fmerge to
recombine the FITS files.

Input: stdin = sorted output from SORTCODE
Output: stderr = Error messages for shell job job log
       Files with lists of FITS files to be fmerged
	s000101h.prelist
        ||--------------- instrument (s0/s1/g2/g3)
          |||------------ group sequence number (001-999)
             ||---------- frfread mode number
               |--------- bit-rate (h,m,l)
                ||||||||- file id
        Note that a file is only created if data exists to be merged.

*/

#include <stdio.h>
#include <string.h>

#include "gissortcode.h"

main () {


    char last_sortcode[256];
    char output_filename[60];
    char split_seq[4];
    char *ptr;
    int open_file=1;
    FILE *fp;
    int merge_cnt=0;
    int filename_cnt=0;
    int file_cnt=0;
    int file_seq=0;
    int total_events=0;
    int nevents=0;

    fprintf (stderr,"GISSORTSPLIT:LO:Start program\n");
    fflush(stderr);
    strcpy(last_sortcode,"99999");
/* loop over the stdin input */
    while (gets(&sortcode.instrument) != NULL) {
	filename_cnt++; /* count the total number of input file names */
        sortcode.file_time[0]='\0';   
        if(strncmp(last_sortcode,&sortcode.instrument,5)!=0) {
	   file_seq=0;
	   }
	if (strcmp(last_sortcode,&sortcode.instrument)!=0) {
            /* reset new break code */
	   strcpy(last_sortcode,&sortcode.instrument);
	   if(open_file==0) { /* if a split file was open already */
	     fclose(fp);
	     open_file=1;
	     fprintf(stderr,
                     "GISSORTSPLIT:LO:%s merge count = %d photon cnt = %d\n",
		     output_filename,merge_cnt,total_events);
             fflush(stderr);
             fprintf(stdout,"%09d %s \'%-212.212s\'\n",
		total_events,output_filename,&sortcode.instrument);
	     file_cnt++; /* keep track of total files written */
	     merge_cnt=0; /* reset on new output file */
             total_events=0;
	     }
	   /* 1st part of output file name based on instrument */
	   if (sortcode.instrument=='2')
	      strcpy(output_filename,"g2");
	   else if (sortcode.instrument=='3')
	      strcpy(output_filename,"g3");
	   else {
	      fprintf(stderr,"GISSORTSPLIT:E3:Invalid instrument code\n");
              fflush(stderr);
	      fprintf(stderr,"GISSORTSPLIT:E3:%s\n",sortcode);
              fflush(stderr);
	      return 3 ;
	      }
           /* add temp seq number for now */
	   file_seq++;
	   sprintf(split_seq,"%03d", file_seq);
           strcat(output_filename,split_seq);
	   /* 3rd part of output file name based on data mode */
	   if (strncmp(sortcode.mode_code,"70",2)==0)
		strcat(output_filename,"70");
	   else if (strncmp(sortcode.mode_code,"71",2)==0)
		strcat(output_filename,"71");
	   else if (strncmp(sortcode.mode_code,"72",2)==0)
		strcat(output_filename,"72");
	   else if (strncmp(sortcode.mode_code,"73",2)==0)
		strcat(output_filename,"73");
	   else if (strncmp(sortcode.mode_code,"75",2)==0)
		strcat(output_filename,"71");
	   else if (strncmp(sortcode.mode_code,"76",2)==0)
		strcat(output_filename,"71");
	   else if (strncmp(sortcode.mode_code,"77",2)==0)
		strcat(output_filename,"71");
	   else if (strncmp(sortcode.mode_code,"78",2)==0)
		strcat(output_filename,"71");
	   else if (strncmp(sortcode.mode_code,"99",2)==0)
		strcat(output_filename,"99");
	   else if (strncmp(sortcode.mode_code,"00",2)==0)
		strcat(output_filename,"00");
	   else {
		fprintf(stderr,"GISSORTSPLIT:E3:Invalid mode code\n");
		fprintf(stderr,"GISSORTSPLIT:E3:%s\n",sortcode);
                fflush(stderr);
		return 3 ;
		}
           /* 3rd part is bit rate */
           if (sortcode.bit_rate=='H')
              strcat(output_filename,"h");
           else if (sortcode.bit_rate=='M')
              strcat(output_filename,"m");
           else if (sortcode.bit_rate=='L')
              strcat(output_filename,"l");
           else if (sortcode.bit_rate=='0')
              strcat(output_filename,"0");
           else {
		fprintf(stderr,"GISSORTSPLIT:E3:Invalid bit rate found\n");
		fprintf(stderr,"GISSORTSPLIT:E3:%s\n",sortcode);
                fflush(stderr);
                return 3 ;
                }
	   /* dummy extention to make it easy to ls *.prelist */
	   strcat(output_filename,".prelist");
	   fp = fopen(output_filename,"w");
	   if (fp==NULL) {
	      fprintf(stderr,"GISSORTSPLIT:E3:Unable to open output file\n");
	      fprintf(stderr,"GISSORTSPLIT:E3:%s\n",output_filename);
              fflush(stderr);
	      return 3 ;
	      }
	   open_file=0; /* mark file as now open */
	   fprintf(fp,
               "%s\n",sortcode.sort_filename); /* write 1st merge file name */
	   merge_cnt++;
           sscanf(sortcode.events,"%d",&nevents);
           total_events=total_events+nevents;
           }
        else {
	   fprintf (fp,
               "%s\n",sortcode.sort_filename); /* write Nth merge file name */
	   merge_cnt++;
           sscanf(sortcode.events,"%d",&nevents);
           total_events=total_events+nevents;
	   }
    }

   if(open_file==0) { /* if a split file was open already */
      fclose(fp);
      open_file=1;
      fprintf(stderr,
                     "GISSORTSPLIT:LO:%s merge count = %d photon cnt = %d\n",
		     output_filename,merge_cnt,total_events);
      fflush(stderr);
      fprintf(stdout,"%09d %s \'%-212.212s\'\n",
	total_events,output_filename,&sortcode.instrument);
      file_cnt++; /* keep track of total files written */
      merge_cnt=0; /* reset on new output file */
      total_events=0;
     }

   fprintf (stderr,"GISSORTSPLIT:LO:Total filenames split = %d\n",filename_cnt);
    fprintf (stderr,"GISSORTSPLIT:LO:Total split file cnt = %d\n",file_cnt);
    fprintf (stderr,"GISSORTSPLIT:LO:End program\n");
    fflush(stderr);
}