
#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[], float, int, int[]);
void confluence(char [], char [], char [], char [], char [], int[]);
void sort(char [], char [], char [], char[], char [], int[]);

char MYSQL[120];
char database[120];
char arina[120];
float YODA_flag;
float transmit_flag;
int print_flag;
char timesync[120];  
 
int main(int argc, char *argv[])
{

// char stop;
 float IndexQ, quality[100];     /*Quality index*/
 int real_route[64];
 int rc=0;
 int r_counter;                  /*Route counter*/               

 char MainPath[120];      /* /home/pamelaprod/rawreader/ */

 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 nnnnnmmm.pam*/
 char protocol[80];      /*Full out file name nnnnn_protocol.txt*/
 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[120];         /*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[100];          /*Initial file mmm_nnnnn_cln1.pam*/
 char inf2[100];          /*File name mmm_nnnnn_cln2.inf*/
 char pam2[100];          /*File name mmm_nnnnn_cln2.pam*/
 char name2[100];         /*Initial file name mmm_counter_nnnnn_cln1.pam*/
 char log2[100];          /*File name mmm_nnnnn_cln2.log*/ 
 char name_ql[80];        /*File name mmm_nnnnn_ql.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[100];         /*Path for ...Level0/nnnnn_mmm_L0/*/
 char exten[10];          /*Extension for the raw initial file*/
 char temporary[80], Tymesync[80];
 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;
  
 for (i=0;i<100;i++) quality[i]=0;
 for (i=0;i<64;i++) real_route[i]=0; 

 FILE *ftime_sync;     
 FILE *fff, *tmp, *tmp1, *tmp2, *tmp3, *tmp4;
 ifstream fpath;
  
//Read parameters from the initial file 
 fpath.open("/home/pamelaprod/rawreader/bin/parameters.dat", ios::out);
 if (!fpath) {cout<<"can not open file with input parameters \n"; exit(1);}
 fpath.getline(MainPath,100,'\n');
 sscanf(MainPath,"%s",MainPath);
 fpath.getline(timesync,100,'\n');
 sscanf(timesync,"%s",timesync);
 fpath.getline(arina,100,'\n');
 sscanf(arina,"%s",arina);
 fpath.getline(MYSQL,100,'\n');
 sscanf(MYSQL,"%s",MYSQL); 
 fpath.getline(database,100,'\n');
 sscanf(database,"%s",database);
 fpath.getline(temp,120,'\n');
 sscanf(temp,"%f",&YODA_flag); 
 fpath.getline(temp,120,'\n');
 sscanf(temp,"%f",&transmit_flag);  
 fpath.getline(temp,120,'\n');
 sscanf(temp,"%i",&print_flag);   

/* cout<<"MainPath="<<MainPath<<" "<<strlen(MainPath)<<"\n";
 cout<<"timesync="<<timesync<<" "<<strlen(timesync)<<"\n";   
 cout<<"arina="<<arina<<" "<<strlen(arina)<<"\n"; 
 cout<<"MYSQL="<<MYSQL<<" "<<strlen(MYSQL)<<"\n"; 
 cout<<"database="<<database<<" "<<strlen(database)<<"\n"; 
 cout<<"YODA_flag="<<YODA_flag<<"\n";  
 cout<<"transmit_flag="<<transmit_flag<<"\n";
 cout<<"print_flag="<<print_flag<<"\n";*/
 
 fpath.close();
 
// mikhailov 06.12.2004 
ftime_sync=fopen(timesync,"r+");
if (!ftime_sync) {cout<<"can not open file with time synchronization \n"; exit(1);}
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 

 if (print_flag>0) cout<<"global_err="<<GLOBAL_LEN_ER<<"\n";
 strcpy(name,argv[2]);
 name_route[0]=name[5];
 name_route[1]=name[6]; 
 name_route[2]=name[7]; 
 name_route[3]='\0';
 
 for(i=0;i<3;i++)
 {exten[i]=name[i+9];}
 exten[3]='\0';
 
 if ((strlen(argv[2])!=12)||(strncmp(exten,"pam",3)!=0)) {cout<<"WRONG RAW FILE NAME\n"; exit(1);} 

 strcpy(fni,argv[1]);
 strcat(fni,argv[2]);
 
/**************Creation directory nnnnn**********************/
 strncat(strcpy(nnnnn_TMP,MainPath),argv[2],5);
 strcpy(temp,nnnnn_TMP);
 strcat(temp,"/tmp.dat");

 if ((tmp=fopen(temp,"w"))==0)  
 {
  strcpy(command,"mkdir ");
  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(1);
 }
/****************************************************************/

 strcat(strncat(strcat( strcpy(INF,nnnnn_TMP),"/"),name,8),"_cln1.inf");
 strcat(strcpy(path,nnnnn_TMP),"/"); 
 strcat(strncat(strcat(strcpy(fno,nnnnn_TMP),"/"),name,8),"_work.dat");
 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';
  strcat(strcat(strcpy(temp1,nnnnn_TMP),"/"),temp);
  strcat(strcpy(command,"mkdir "),temp1); 
  
  system(command); 
  strcat(temp1,"/Cln1");
  strcat(strcpy(command,"mkdir "),temp1);
  system(command);
/**********Copy file to directory nnnnn_TMP\mmm\Cln1*************/
  strcat(strcat(strcat(strcat(strcat(strcpy(command,"cp "),fni)," "),temp1),"/"),name);

  system(command);
  strcat(strcat(strcpy(temp2,temp1),"/"),name);
  strcat(strcat(strcat(strcpy(temp3,temp1),"/"),temp),"_");
  strcat(strncat(temp3,name,5),"_cln1.pam");
  rename(temp2,temp3);
 }
 
 if (CLN2_FLAG>0)
 {
  for(i=0;i<256;i++)
  {
   sprintf(temp4,"%03i",i);
   sscanf(temp4,"%3s",mmm); mmm[3]='\0';
   strcat(strcat(strcat(strcat(strcat(strcpy(temp1,nnnnn_TMP),"/"),name_route),"_"),mmm),"/tmp.dat");
  
   if ((tmp1=fopen(temp1,"w"))!=0)
   {
    fclose(tmp1);
    remove(temp1);
    name2[0]='\0';
    strcat(strcat(strcat(strcat(strcat(strncat(name2,name,5),"_"),name_route),"_"),mmm),"_cln1.pam");
    if (CLN1_MEPHI==0) 
    {strcat(strcat(strcat(strcat(strcat(strcat(strcpy(fni2,nnnnn_TMP),"/"),name_route),"_"),mmm),"/Cln1/"),name2);}
    else
    {strcat(strcat(strncat(strcpy(fni2,""),nnnnn_TMP,strlen(nnnnn_TMP)-5),"Cln1/"),name2);}
    strcpy(temp4,path);
    strcat(strcat(strcat(strcat(temp4,name_route),"_"),mmm),"/Cln2/tmp.dat");
    if ((tmp4=fopen(temp4,"w"))==0)
    {
/**************Creation directory nnnnn\xxx_mmm\Cln2****************/
     strcat(strcat(strcat(strcat(strcat(strcat(strcpy(command,"mkdir "),nnnnn_TMP),"/"),name_route),"_"),mmm),"/Cln2");
     system(command);
/*******************************************************************/
    }
    else
    {
     fclose(tmp4);
     remove(temp4);
    }
    strcat(strcat(strcat(strcat(strcat(strcpy(path2,nnnnn_TMP),"/"),name_route),"_"),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, IndexQ, r_counter, real_route); /////////////////////////////////    
    strcat(strcat(strcat(strcat(strcat(strcat(strcpy(fni2,nnnnn_TMP),"/"),name_route),"_"),mmm),"/Cln1/"),name2);
    //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";
    }
    strcat(strcat(strcat(strcat(strcat(strncat(strcpy(path3,MainPath),argv[2],5),"/"),name_route),"_"),mmm),"/packets");

    strcat(strcpy(temp2,path3),"/tmpM.dat");
    if ((tmp2=fopen(temp2,"w"))==0)  
    {        
/**************Creation directory level0\nnnnn\counter_mmm\packets***************/
     strcat(strcpy(command,"mkdir "),path3);
     system(command);
/*******************************************************************/      
    }
    else
    {
     fclose(tmp2);
     remove(temp2);
     printf("directory for file %s already exists - rewriting \n",
     strcat(strcat(strcat(strcat(strncat(strcpy(temp3," "),argv[2],5),"_"),name_route),"_"),mmm));
    }
    strcat(path3,"/");
    sort(log2, pam2, path2, path3, name2, packet_type); /////////////////////////////////////
    strcat(strncat(strcat(strcat(strcpy(name_ql,path3),mmm),"_"),argv[2],5),"_ql.log");
    strcpy(temp3,"/home/pamelaprod/rawreader/bin/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);
    }
   }              
  }
 }

 return 0;
}
