
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

/***************MAIN********************************************/
#include "descript.h"
#include "flag.h"

//mikhailov 06. 12. 2004. : initialization of global parameteres for time sync. it is used
//in  in sort.cpp to convert OBT of Pamela to Moscow Time .
//this information is apdated in RunHeaderReader
long int RTtoMT,RT,Timesync,OBTtimesync;  // Resurs time to moscow, Resurs time, ...
 
void read(char [], char [], char [], char [], char [], char [],
		  char[], float [], int[], int &);
void verify(char [], char[], char [], char [], char [], char [], char [],
			char [], int[], int, float, int, int[]);
void confluence(char [], char [], char [], char [], char [], int[]);
void sort(char [], char [], char [], char[], char [], int[]);



int main(int argc, char *argv[])
{ time_t time_set=0, *time_p;
// char stop;
 float IndexQ, quality[100];     /*Quality index*/
 int real_route[64];
 int rc=0;
 int r_counter;                  /*Route counter*/               

 char MainPath[80];      /* /home/pamelaprod/ */
 char INF[80];           /*Full out information file name nnnnn_cln1.inf*/
 char fni[80];           /*Full initial file name nnnnnmmm.pam*/
 char fno[80];           /*Full out helping file name nnnnn_work.dat*/
 char path[80];          /*Initial path*/
 char name[80];          /*Initial file name nnnnn_out.pam*/
 char protocol[80];      /*Full out file name nnnnn_protocol.txt*/
 char RAW[80];           /*Directory for RAW data*/
 char nnnnn_TMP[80];     /*Directory for processed data*/
 char command[180];      /*DOS command*/
 char str_del[80];       /*Delete Cln1 directory in case of good quality*/
 char temp[80];          /*For temporary file*/ 
 char temp1[80];         /*For temporary file*/
 char temp2[80];         /*For temporary file*/ 
 char temp3[80];         /*For temporary file*/ 
 char temp4[80];

 char fni2[80];          /*Initial file mmm_nnnnn_cln1.pam*/
 char inf2[80];          /*File name mmm_nnnnn_cln2.inf*/
 char pam2[80];          /*File name mmm_nnnnn_cln2.pam*/
 char name2[80];         /*Initial file name mmm_counter_nnnnn_cln1.pam*/
 char name_ql[80];       /*File name mmm_nnnnn_ql.log*/
 char log2[80];          /*File name mmm_nnnnn_cln2.log*/
 char fno2[80];          /*Out help file mmm_nnnnn_work.dat*/
 char path2[80];         /*Path for ...Temp/nnnnn_TMP/mmm/Cln2/*/
 char mmm[80];           /*For route number*/
 char name_route[4];     /*Route number in the file name*/

 char path3[80];         /*Path for ...Level0/nnnnn_mmm_L0/*/

 int Rcount=0;           /*Number of Routes*/
 int packet_type[NumberPack]={0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x16, 0x17, 0x18, 0x19,
	  0x1A, 0x1B, 0x1C, 0x1D, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x30, 0x50, 0x51, 0x52, 0x53,
	  0x54, 0x55, 0x60, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x81, 0x82, 0x83, 0x84,
	  0x85, 0x86, 0x88, 0x89, 0x8A, 0xA1, 0xAB, 0xF0, 0xF1, 0xF2, 0xF3, 0xF4};/*PAMELA packet type code*/	  
 int i, j;
 int stop;
 char *ttt=new char[Nmax];
  
 for (i=0;i<100;i++) quality[i]==0;
 for (i=0;i<64;i++) real_route[i]==0; 
// int RTtoMT,RT,Timesync,OBTtimesync;

 FILE *ftime_sync;     
 FILE *fff, *tmp, *tmp1, *tmp2, *tmp3, *tmp4;

// mikhailov 06.12.2004 
ftime_sync=fopen("/home/pamelaprod/rawreader/source/timesync.dat","r+");
while(feof(ftime_sync)==0) 
{fscanf(ftime_sync,"%u%[ ]%u%[ ]%u%[ ]%u\n",&RTtoMT,temp,&RT,temp, &Timesync,temp,&OBTtimesync);}                            
fclose(ftime_sync);
// end mikhailov 

 time_set=time(time_p);
 
// cout<<copy(LINUX_flag)<<"\n";
// cout<<dir(LINUX_flag)<<"\n";
 cout<<"global_err="<<GLOBAL_LEN_ER<<"\n";

 strcpy(fni,argv[1]);
 strcat(fni,argv[2]);

 strcpy(name,argv[2]);
 name_route[0]=name[5];
 name_route[1]=name[6]; 
 name_route[2]=name[7]; 
 name_route[3]='\0';
 cout<<"!!!!!!!!!!!!!!!!!!!!!!!!!\n";
 if (LINUX_flag==1) strcpy(MainPath,"/home/pamelaprod/rawreader/level0/");
 if (LINUX_flag!=1) strcpy(MainPath,"c:\\home\\pamelaprod\\");

/**************Creation directory nnnnn**********************/
 if (LINUX_flag==1) strncat(strcpy(nnnnn_TMP,MainPath),argv[2],5);
 if (LINUX_flag!=1) strcat(strncat(strcat(strcpy(nnnnn_TMP,MainPath),"Temp\\"),argv[2],5),"_TMP");
 strcpy(temp,nnnnn_TMP);
 if (LINUX_flag==1)strcat(temp,"/tmp.dat");
 if (LINUX_flag!=1) strcat(temp,"\\tmp.dat");

 if ((tmp=fopen(temp,"w"))==0)  
 {
  if (LINUX_flag==1) strcpy(command,"mkdir ");
  if (LINUX_flag!=1) strcpy(command,"md ");
  strcat(command,nnnnn_TMP);
  system(command);
 }
 else
 {
  printf("directory %s already exists - rewriting \n",strncat(strcpy(temp1," "),argv[2],5));
  fclose(tmp);
  remove(temp);
//  exit(0);
 }
/****************************************************************/

 if (LINUX_flag==1) strcat(strncat(strcat( strcpy(INF,nnnnn_TMP),"/"),name,8),"_cln1.inf");
 if (LINUX_flag!=1) strcat(strncat(strcat( strcpy(INF,nnnnn_TMP),"\\"),name,8),"_cln1.inf");
 if (LINUX_flag==1) strcat(strcpy(path,nnnnn_TMP),"/"); 
 if (LINUX_flag!=1) strcat(strcpy(path,nnnnn_TMP),"\\");
 if (LINUX_flag==1) strcat(strncat(strcat(strcpy(fno,nnnnn_TMP),"/"),name,8),"_work.dat");
 if (LINUX_flag!=1) strcat(strncat(strcat(strcpy(fno,nnnnn_TMP),"\\"),name,8),"_work.dat");
 if (LINUX_flag==1) strcat(strncat(strcat(strcpy(protocol,nnnnn_TMP),"/"),name,8),"_protocol.txt");
 if (LINUX_flag!=1) strcat(strncat(strcat(strcpy(protocol,nnnnn_TMP),"\\"),name,8),"_protocol.txt");
 
 if (VRL_HEADER>0)
 {
  /*Creation of directories nnnnn_TMP\mmm_counter and ...\Cln1 in filename.cpp*/
  read(fni, fno, INF, protocol, path, name, nnnnn_TMP, quality, real_route, Rcount); ////////////////// //////////////
 }
 else
 {
  temp[0]=name[5];
  temp[1]=name[6];
  temp[2]=name[7];
  temp[3]='\0';
  if (LINUX_flag==1) strcat(strcat(strcpy(temp1,nnnnn_TMP),"/"),temp);
  if (LINUX_flag!=1) strcat(strcat(strcpy(temp1,nnnnn_TMP),"\\"),temp);
  if (LINUX_flag==1) strcat(strcpy(command,"mkdir "),temp1); 
  if (LINUX_flag!=1) strcat(strcpy(command,"md "),temp1);
  
  system(command); 
  if (LINUX_flag==1) strcat(temp1,"/Cln1");
  if (LINUX_flag!=1) strcat(temp1,"\\Cln1");
  if (LINUX_flag==1) strcat(strcpy(command,"mkdir "),temp1);
  if (LINUX_flag!=1) strcat(strcpy(command,"md "),temp1);
  system(command);
/**********Copy file to directory nnnnn_TMP\mmm\Cln1*************/
  if (LINUX_flag==1) strcat(strcat(strcat(strcat(strcat(strcpy(command,"cp "),fni),
	                        " "),temp1),"/"),name);
  if (LINUX_flag!=1) strcat(strcat(strcat(strcat(strcat(strcpy(command,"copy "),fni),
	                        " "),temp1),"\\"),name);
  system(command);
  if (LINUX_flag==1) strcat(strcat(strcpy(temp2,temp1),"/"),name);
  if (LINUX_flag!=1) strcat(strcat(strcpy(temp2,temp1),"\\"),name);
  if (LINUX_flag==1) strcat(strcat(strcat(strcpy(temp3,temp1),"/"),temp),"_");
  if (LINUX_flag!=1) strcat(strcat(strcat(strcpy(temp3,temp1),"\\"),temp),"_");
  strcat(strncat(temp3,name,5),"_cln1.pam");
  rename(temp2,temp3);
 }
 
 if (LINUX_flag==1) strcat(strcpy(temp,nnnnn_TMP),"/tmp.dat");
 if (LINUX_flag!=1) strcat(strcpy(temp,nnnnn_TMP),"\\tmp.dat");
 tmp=fopen(temp, "w+");

 for(i=0;i<256;i++)
 {
  fprintf(tmp,"%03i",i);
  fseek(tmp,0,0);
  fgets(mmm,10,tmp);
  fseek(tmp,0,0);
  if (LINUX_flag==1) strcat(strcat(strcat(strcat(strcat(strcpy(temp1,nnnnn_TMP),"/"),name_route),"_"),mmm),"/tmp.dat");
  if (LINUX_flag!=1) strcat(strcat(strcat(strcpy(temp1,nnnnn_TMP),"\\"),mmm),"\\tmp.dat");  
  if ((tmp1=fopen(temp1,"w"))!=0)
  {
   fclose(tmp1);
   remove(temp1);
   strcat(strncat(strcat(strcat(strcat(strcpy(name2,name_route),"_"),mmm),"_"),name,5),"_cln1.pam");
   if (LINUX_flag==1) strcat(strcat(strcat(strcat(strcat(strcat(strcpy(fni2,nnnnn_TMP),"/"),name_route),"_"),mmm),"/Cln1/"),name2);
   if (LINUX_flag!=1) strcat(strcat(strcat(strcat(strcpy(fni2,nnnnn_TMP),"\\"),mmm),"\\Cln1\\"),name2);
   strcpy(temp4,path);
   if (LINUX_flag==1) strcat(strcat(strcat(strcat(temp4,name_route),"_"),mmm),"/Cln2/tmp.dat");
   if (LINUX_flag!=1) strcat(strcat(temp4,mmm),"\\Cln2\\tmp.dat");
   if ((tmp4=fopen(temp4,"w"))==0)
   {
/**************Creation directory nnnnn\counter_mmm\Cln2****************/
    if (LINUX_flag==1) strcat(strcat(strcat(strcat(strcat(strcat(strcpy(command,"mkdir "),nnnnn_TMP),"/"),name_route),"_"),mmm),"/Cln2");
    if (LINUX_flag!=1) strcat(strcat(strcat(strcat(strcpy(command,"md "),nnnnn_TMP),"\\"),mmm),"\\Cln2");
    system(command);
/*******************************************************************/
   }
   else
   {
    fclose(tmp4);
    remove(temp4);
   }
   if (LINUX_flag==1) strcat(strcat(strcat(strcat(strcat(strcpy(path2,nnnnn_TMP),"/"),name_route),"_"),mmm),"/Cln2/");
   if (LINUX_flag!=1) strcat(strcat(strcat(strcpy(path2,nnnnn_TMP),"\\"),mmm),"\\Cln2\\");
   strcat(strncat(strcpy(inf2,path2),name2,13),"_cln2.inf");
   strcat(strncat(strcpy(log2,path2),name2,13),"_cln2.log");
   strcat(strncat(strcpy(pam2,path2),name2,13),"_cln2.pam");
   strcat(strncat(strcpy(fno2,path2),name2,13),"_work.dat");

   IndexQ=quality[i-1];
   r_counter=i;   
   verify(fni2, inf2, log2, pam2, fno2, path2, name, name2, packet_type, GLOBAL_LEN_ER, IndexQ, r_counter, real_route); /////////////////////////////////
   //Delete Cln1
   if (IndexQ>0.999)
   {strcpy(str_del,"rm -r -f ");
    strncat(str_del,fni2,strlen(fni2)-22);
    if (system(str_del)) cout<<"Can not delete Cln1 for counter="<<r_counter<<"\n";
   }
   if (LINUX_flag==1) strcat(strcat(strcat(strcat(strcat(strncat(strcpy(path3,MainPath),argv[2],5),"/"),name_route),"_"),mmm),"/packets");
   if (LINUX_flag!=1) strcat(strcat(strcat(strncat(strcpy(strcpy(path3,MainPath),
	                         "Level0\\"),argv[2],5),"_"),mmm),"_L0");
   if (LINUX_flag==1) strcat(strcpy(temp2,path3),"/tmpM.dat");
   if (LINUX_flag!=1) strcat(strcpy(temp2,path3),"\\tmpM.dat");
   if ((tmp2=fopen(temp2,"w"))==0)  
   {        
/**************Creation directory level0\nnnnn\counter_mmm\packets***************/
    if (LINUX_flag==1) strcat(strcpy(command,"mkdir "),path3);
    if (LINUX_flag!=1) strcat(strcpy(command,"md "),path3);
    system(command);
/*******************************************************************/      
   }
   else
   {
    fclose(tmp2);
    remove(temp2);
    printf("directory %s already exists - rewriting \n",
    strcat(strcat(strcat(strcat(strcat(strncat(strcpy(temp3," "),argv[2],5),"_"),name_route),"_"),mmm),"_L0"));
   }
   if (LINUX_flag==1) strcat(path3,"/");
   if (LINUX_flag!=1) strcat(path3,"\\");
   sort(log2, pam2, path2, path3, name2, packet_type); /////////////////////////////////////
   strcat(strncat(strcat(strcat(strcpy(name_ql,path3),mmm),"_"),argv[2],5),"_ql.log");
   if (LINUX_flag==1) strcpy(temp3,"/home/pamelaprod/rawreader/bin/coordinate.exe");
   if (LINUX_flag!=1) strcat(strcpy(temp3,MainPath),"Norad\\coordinate.exe");
   if ((tmp3=fopen(temp3,"r"))==0)
   {printf("file %s does not exist\n",temp3);}
   else
   {
    fclose(tmp3);
    strcat(strcat(strcpy(command,temp3)," "),name_ql);
//    system(command);
   }          
  }
 }
 fclose(tmp);
 remove(temp); delete [] ttt;

 return 0;
}
