function checkGIS(siteID,NomeSito,conn,FileName)

text = 'checkGIS function started';
fileID = fopen(FileName,'a');
fmt = '%s \r';
fprintf(fileID,fmt,text);
fclose(fileID);

comando = ['select gis_enable, gis_duedate from sites where id like ''' siteID ''' '];
curs = exec(conn,comando);
curs = fetch(curs);
GIS = curs.Data;
if cell2mat(GIS(1)) == 1 % GIS attivato
    if strcmp(char(GIS(2)),'null') == 0              
        FileNameGIS = ['GIS' siteID '-Deadline.txt'];
        if isfile(FileNameGIS) == 0 % NON Esiste
            outdat = fopen(FileNameGIS,'wt+');
            fileID_site = fopen(FileNameGIS,'a');
            text = now;
            fmt = '%f \r';
            fprintf(fileID_site,fmt,text);
            fclose(fileID_site);
        end
        
        % Scarico dati di riferimento
        Data_Rif = importdata(FileNameGIS);
        [rA,~] = size(Data_Rif);
        if rA == 0
            Data_Rif = 0;
        end
        
        % Controllo la data di scadenza del GIS
        G = cell2mat(GIS(2));
        [lS] = length(G);
        date = G(1:10);
        time = G(12:lS);
        Date = [date ' ' time];

        Info = 0;
        if now <= datenum(Date) && datenum(Date) < now+30 % fra 1 giorno e 1 Mese
            if datenum(Data_Rif) > datenum(Date)-30
                mail = 0; % la mail è già stata inviata
            else
                mail = 1;
            end
        elseif now > datenum(Date) && datenum(Data_Rif) < datenum(Date) % GIS scaduto
            mail = 1;
            Info = 1;
            desc = 'Scaduto';
        else
            mail = 0;
        end
            
        if mail == 1
            DATAinsert = cell(1,5);
            sms = 0;
            Data = datestr(now,'yyyy-mm-dd HH:MM:SS');
            DATAinsert{1,1} = 8; % Allarme scadenza GIS
            DATAinsert{1,2} = cellstr(['GIS|' char(NomeSito)]);
            DATAinsert{1,3} = Data;
            DATAinsert{1,4} = sms;
            if Info == 1
                DATAinsert{1,5} = desc;
            end
                
            % Cerco se il dato è già presente
            if Info == 1
                comando = ['select id, type_id, date_time from alarms where tool_name = ''' ...
                    NomeSito ''' and date_time = ''' Data ''' and type_id = ''' DATAinsert{1,1} ...
                    ''' and description like ''' desc ''' order by date_time'];
            else
                comando = ['select id, type_id, date_time from alarms where tool_name = ''' ...
                    NomeSito ''' and date_time = ''' Data ''' and type_id = ''' DATAinsert{1,1} ...
                    ''' order by date_time'];
            end
            curs = exec(conn,comando);
            curs = fetch(curs);
            idDate = curs.Data;
            [~,cI] = size(idDate);
            if  cI == 1
                idElabData = 0; % 0 indica che il dato non è presente su DB
            else
                idElabData = cell2mat(idDate(:,1));
            end
            tablename = 'alarms';
            if Info == 1
                colnames = {'type_id','tool_name','date_time','send_sms','description'};
                data = [DATAinsert(1,1),DATAinsert(1,2),DATAinsert(1,3),DATAinsert(1,4),DATAinsert(1,5)];
            else
                colnames = {'type_id','tool_name','date_time','send_sms'};
                data = [DATAinsert(1,1),DATAinsert(1,2),DATAinsert(1,3),DATAinsert(1,4)];
            end
            if idElabData == 0 % Scrivo
                fastinsert(conn,tablename,colnames,data);
                text = ['ALERT was written in the DB by checkGIS function for date: ''' Data ''' '];
            end
            fileID = fopen(FileName,'a');
            fmt = '%s \r';
            fprintf(fileID,fmt,text);
            fclose(fileID);
        end
        outdat = fopen(FileNameGIS,'wt+');
        fileID_site = fopen(FileNameGIS,'a');
        text = now;
        fmt = '%f \r';
        fprintf(fileID_site,fmt,text);
        fclose(fileID_site);
    end
end

text = 'checkGIS function worked correctly';
fileID = fopen(FileName,'a');
fmt = '%s \r';
fprintf(fileID,fmt,text);
fclose(fileID);

end