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

int main(void) {
    
	// VARIABLES DECLARATION
	char		address[30], userName[20], passwd[30], name[64], ParName[8], channelname[30], stringaexec[80], directory_data[80];
	int         is, n, slot, slotr, pw, status, station, id_channel, id_channelr, i, fixedtime, timestamp, timestampread, start, vlong;
	float		vmon, imon, imin, imax, istart, istop, pmin, pmax, pstart, pstop;
    int         slot_r, write=0, ok=0, ovc=0, reject=0;
	time_t 		now;
    
    float       dew, pressure=0,rainlast,rainday,rainmonth,rainyear,heat,temp1=0,temp2=0,rh1=0,rh2=0,wind,gust,dir;
    int         day,month,year,hour,min;
    time(&now);
    //now is actual unix time
    
    strcpy (directory_data,"/home/user/re4data/");

//sleep(60);    
	// Reading the Configuration file GAPS (it cointains the path file also)
	FILE *stream6;
	stream6=fopen("/srv/www/htdocs/conf_gap.txt","r");
    if (        (stream6 = fopen("/srv/www/htdocs/conf_gap.txt","r")) == NULL              ) {
        printf("Error opening file: %s\n", "/srv/www/htdocs/conf_gap.txt");
        exit(1);
    }
    fscanf(stream6,"%s\n",name);
	fscanf(stream6,"%s\n",address);
        
    // Reading if start or not the process
	FILE *stream5;
	stream5=fopen("/srv/www/htdocs/gap_long_start.txt","r");
    if (    (stream5 = fopen("/srv/www/htdocs/gap_long_start.txt", "r")) == NULL    ) {
        printf("Error opening file: %s\n", "/srv/www/htdocs/gap_long_start.txt");
        exit(1);
    }
	fscanf(stream5,"%d %d %d %d\n",&timestamp, &start, &fixedtime, &vlong);
    fflush(stream5);
    fclose(stream5);
    
    
    if (start == 1) {
        if ( (now - timestamp) > fixedtime) {
            
            
             // Stopping future process
             FILE *stream51;
             stream51=fopen("/srv/www/htdocs/gap_long_start.txt","w");
             if (    (stream51 = fopen("/srv/www/htdocs/gap_long_start.txt", "w")) == NULL    ) {
                printf("Error opening file: %s\n", "/srv/www/htdocs/gap_long_start.txt");
                exit(1);
             }
             fprintf(stream51,"%s\n","0 0 0 0");
             fflush(stream51);
             fclose(stream51);
            
            
            // Reading values from the existing data files
            while (!feof(stream6)) {
                
                // READING THE CONFIGURATION FILE
                fscanf(stream6,"%s %d %d %d\n",channelname, &slot_r, &id_channelr, &station);
                //printf("Slot: %d Channel in the slot = %d \n gap_station = %d\n", slot_r,id_channelr, station);
                
                //printf("%s\n",channelname);
                
                // OPENING FILE TO ESTIMATE MIN/MAX (chamber by chamber)
                strcpy (directory_data,"/home/user/re4data/");
                strcpy (stringaexec,directory_data);
                strcat (stringaexec,channelname);
                strcat (stringaexec,"_gap.dat");
                
                FILE *stream2;
                stream2=fopen(stringaexec,"r");
                if (     (stream2 = fopen(stringaexec, "r")) == NULL) {
                    printf("Error opening file: %s\n", stringaexec);
                    //exit(1);
                }
                else {
                    
                    i=-1;
                    ok = 0;
                    while (!feof(stream2)) {
                        
                        i = i + 1;
                        fscanf(stream2,"%s %d %s %d %d %f %f %d %d %f %f %f\n", name, &timestampread, channelname, &slot, &id_channel, &vmon ,&imon, &status, &pw, &temp1, &rh1, &pressure);
                        //printf("%s %d %s %d %d %f %f %d %d %f %f %f\n", name, timestampread, channelname, slot, id_channel, vmon, imon, status, pw, temp1, rh1, pressure);
                        
                        if (  (i == 0) && (timestampread>timestamp) && (abs(vmon-vlong)<50) && (imon>0) && (pressure>0) && (status==1)   ) {
                            imin = imon;
                            imax = imon;
                            istart = imon;
                            pmin = pressure;
                            pmax = pressure;
                            pstart = pressure;
                            ok = 1;
                            //printf("%s\n","first line");
                        }
                        else {
                            //printf("%s\n","not considering...");
                            i = i - 1;
                        }
                        
                        if (   (ok==1) && (imon>0) && (pressure>0) && (timestampread>timestamp) && (abs(vmon-vlong)<50) && (status==1)  ) {
                            //printf("%s\n","processing...");
                            if (status == 512) ovc=1;
                            istop = imon;
                            pstop = pressure;
                            if (imin > imon) {
                                imin = imon;
                                pmin = pressure;
                            }
                            if (imax < imon) {
                                imax = imon;
                                pmax = pressure;
                            }
                            write = 1;
                        }
                    }
                    fflush(stream2);
                    fclose(stream2);
                    //printf("%s\n","file closed");
                    
                    if (write == 1 ) {
                        
                        // Writing GAP summary file
                        write = 0;
                        strcpy (directory_data,"/home/user/re4data/STABLE_GAP/");
                        strcpy (stringaexec,directory_data);
                        strcat (stringaexec,channelname);
                        strcat (stringaexec,"_stablegap.csv");
                        FILE *stream21;
                        stream21=fopen(stringaexec,"w");
                        if (    (stream21 = fopen(stringaexec, "w")) == NULL   ) {
                            printf("Error opening file: %s\n", stringaexec);
                            exit(1);
                        }
                        
                        vmon=vmon/100;
                        vmon= (int) vmon;
                        vmon=vmon*100;
                        
			//fprintf(stream21,"%s\n","name,hv,istart,istop,pstart,pstop,,imin,imax,pmin,pmax,rejectaccept");
                        fprintf(stream21,"%s,%.0f,%.5f,%.5f,%3.0f,%3.0f,%.5f,%.5f,%3.0f,%3.0f,%d,%d", channelname, vmon, istart, istop, pstart, pstop, imin, imax, pmin, pmax, reject,ovc);
                        fflush(stream21);
                        fclose(stream21);
                    }
                }
            }
        }
    }
    // CLOSING FILES
    fflush(stream6);
    fclose(stream6);
}
