
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/socket.h>
#include<netinet/in_systm.h>
#include<netinet/in.h>
#include<netinet/ip.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<string.h>
#include<pthread.h>
#include<time.h>
#include<errno.h>

#include"zither.h"
#include<almemsys/almemsys.h>
#include"jpreturn.h"


extern  global_data_t gd;



int clockfile_check(void)
{
static int ret;
FILE * fp;
FILE * fpl;
int interval = 120;
int fp_stat = 0;
int count0 = 0;
int count1 = 0;
int count2 = 0;
char chA;
char data[2][BUF_SLEN];
char * swap;
char * recdata; 

char * alllock_filename = (char *)calloc(BUF_DEFLEN, sizeof(char));
char * swapsubject = (char *)calloc(BUF_DEFLEN, sizeof(char));
char * swapdata = (char *)calloc(BUF_DEFLEN, sizeof(char));


ret = 0;
for(;;){
   recdata = (char *)calloc(BUF_DEFLEN, sizeof(char));
   #ifdef DEBUG
   printf("DEBUG: clockfile_check(): neg.conf   ->%s\n", gd.fp.neg_conf);
   printf("DEBUG: clockfile_check(): clock_path ->%s\n", gd.fp.clock_path);
   #endif

   fp_stat = 0;
   for(count2 = 0; count2 < interval; count2++){
      #ifdef DEBUG
      printf("DEBUG: clockfile_check(): sleep\n"); 
      #endif
      sleep(1);
      }
   if((fp = fopen(gd.fp.neg_conf, "r+"))!=NULL){
      while(fp_stat == 0){
         swap = (char *)calloc(BUF_DEFLEN, sizeof(char));
         chA = fgetc(fp);
         switch(chA){
            case ':' : data[count0][count1] = (char)0x00;
                       if(count0 < 2){
                          count0++; count1 = 0;
                          }
                       break;
            case ';' : data[count0][count1] = (char)0x00;
                       if(count1 != 0){
                          if(0 != strlen(data[count0])){
                             alllock_filename = filename_create(
                                alllock_filename, gd.fp.clock_path, "_lock", data[0]);
                             if((fpl = fopen(alllock_filename, "r"))!=NULL){
                                swap = safe_sprintf(swap, BUF_MAX, 
                                 "The server has stopped.\n%s:%s\nLockfile=%s\n",
                                 data[0], data[1], alllock_filename);
                                #ifdef DEBUG
                                printf("DEBUG: clockfile_check(): %s\n", swap);
                                printf("DEBUG: clockfile_check(): lock file: %s\n", alllock_filename);
                                #endif
                                fclose(fpl);
                                }
                             }
                          count0 = 0; count1 = 0;
                          }
                       break;
            case '\n' : data[count0][count1] = (char)0x00;
                       if(count1 != 0){
                          if(0 != strlen(data[count0])){
                             alllock_filename = filename_create(
                               alllock_filename, gd.fp.clock_path, "_lock", data[0]);
                             if((fpl = fopen(alllock_filename, "r"))!=NULL){
                                swap = safe_sprintf(swap, BUF_MAX, 
                                 "The server has stopped.\n%s:%s\nLockfile=%s\n",
                                 data[0], data[1], alllock_filename);
                                #ifdef DEBUG
                                printf("DEBUG: clockfile_check(): %s\n", swap);
                                printf("DEBUG: clockfile_check(): lock file: %s\n", alllock_filename);
                                #endif
                                fclose(fpl);
                                }
                             }
                          count0 = 0; count1 = 0;
                          }
                       break;
            case EOF : fp_stat = 1;
                       data[count0][count1] = (char)0x00;
                       break;
            default  : 
                       if(chA >= 0){
                          data[count0][count1] = chA;
                          if(count1 < BUF_SLEN){
                             count1++;
                             }
                          }
                       break;
            }

         recdata = safe_strncat(recdata, swap, BUF_MAX);
         free(swap);
         } /* while() */

      fclose(fp);
      } /* fopen() */
   else{
      fprintf(stderr,"clockfile_check(): Not open neg.conf %s\n", gd.fp.neg_conf);
      }

   #ifdef DEBUG
   fprintf(stdout,"DEBUG: clockfile_check(): %s\n", recdata);
   #endif


   if(2 < (int)strlen(recdata)){

      if(gd.md.in_mailonoff == 1){

      // subject を作ります
         swapsubject = make_subject(swapsubject, CLIENT_BODY_4, "administrator");

      // 本文を作ります
         swapdata = safe_sprintf(swapdata, BUF_MAX, "%s\n\n%s\n\n%s\n", CLIENT_BODY_3, recdata, CL_VAR);
         swap_mem(swapdata, '\n', (char)0x01);

         mail_grsend(gd.md.ch_MailServ,
                 gd.md.ch_MailFrom,
                 gd.md.ch_MailRcptTo,
                 " ",
                 swapdata,
                 swapsubject,
                 gd.on_bcc,
                 "1");

            }   

      }

   free(recdata);
   } /* for(;;) */

free(alllock_filename);
free(swapsubject);
free(swapdata);

return(ret);
}      







