

#include<stdio.h>
#include<unistd.h>
#include<sys/wait.h>
#include<netinet/in.h>
#include<string.h>
#include<stdlib.h>
#include<pthread.h>

#include"sitar.h"
#include"sitar_com.h"
#include<almemsys/almemsys.h>
#include"jpreturn.h"
#include<file_comp/file_comp.h>

extern global_data_t gd;

extern pthread_mutex_t fd_mutex;



// sitar側の設定が優先される項目をzitherに反映させる関数。
int setup_conf(void)
{
int stat, interval;

stat = 0;

char * rec = (char *)calloc(IP_LEN, sizeof(char));
char * recs = (char *)calloc(IP_LEN, sizeof(char));
char * recdata = (char *)calloc(IP_LEN, sizeof(char));
char * intr = (char *)calloc(IP_LEN, sizeof(char));

interval = (gd.bf.life_int + gd.bf.life_los + INT_PLUS);

// LIFE INT 
if(gd.bf.life_int != 0){
   intr = safe_sprintf(intr, BUF_MAX,"%s :: COM: SET DATA: LIFE_INT:%d\n", gd.bf.c_name, gd.bf.life_int);
   #ifdef SET_DEBUG 
   printf("SET: setup_conf(): %s :: COM: SET DATA: LIFE_INT:%d\n", gd.bf.c_name, gd.bf.life_int);
   #endif
// pthread_mutex_lock(&fd_mutex);
   safe_pthread_mutex_lock(&fd_mutex);
   if((stat = socket_send(gd.ip.sk, intr, interval)) > 0){
      recdata = select_fdcat(gd.ip.sk, recdata, &stat, BUF_MAX, interval);
      }
   if(stat < 0){
      safe_exit("setup_conf(1): socket err exit \n");
      }
   pthread_mutex_unlock(&fd_mutex);

   }
else{
   fprintf(stderr,"setup_conf(): set data err LIFE_INT\n");
   }

// LIFE LOSS
if(gd.bf.life_los >= 0){
   rec = safe_sprintf(rec, BUF_MAX,"%s :: COM: SET DATA: LIFE_LOS:%d\n",gd.bf.c_name,gd.bf.life_los);
   #ifdef SET_DEBUG 
   printf("SET: setup_conf() %s :: COM: SET DATA: LIFE_LOS:%d\n", gd.bf.c_name, gd.bf.life_los);
   #endif
// pthread_mutex_lock(&fd_mutex);
   safe_pthread_mutex_lock(&fd_mutex);
   if((stat = socket_send(gd.ip.sk, rec, interval)) > 0){
      recdata = select_fdcat(gd.ip.sk, recdata, &stat, BUF_MAX, interval);
      }
   if(stat < 0){
      safe_exit("setup_conf(2): socket err exit \n");
      }
   pthread_mutex_unlock(&fd_mutex);

   }
else{
   fprintf(stderr,"setup_conf(): set data err LIFE_LOS\n");
   }

// LIFE ONOFF
if(gd.ld.onoff == 0){
   rec = safe_sprintf(rec, BUF_MAX,"%s :: COM: SET DATA: LIFE_ONOFF:%d\n",
     gd.bf.c_name, gd.ld.onoff);
   #ifdef SET_DEBUG 
   printf("SET: setup_conf() %s :: COM: SET DATA: LIFE_ONOFF:%d\n",
      gd.bf.c_name, gd.ld.onoff);
   #endif
// pthread_mutex_lock(&fd_mutex);
   safe_pthread_mutex_lock(&fd_mutex);
   if((stat = socket_send(gd.ip.sk, rec, interval)) > 0){
      recdata = select_fdcat(gd.ip.sk, recdata, &stat, BUF_MAX, interval);
      }
   if(stat < 0){
      safe_exit("setup_conf(3): socket err exit \n");
      }
   pthread_mutex_unlock(&fd_mutex);

   }
else{
// fprintf(stderr,"setup_conf(): set data err LIFE_ONOFF\n");
   }

// MAIL CC
if(0 != strlen(gd.smtp.ch_MailCc)){
   if(0 != memcmp(gd.smtp.ch_MailCc, "NOT", 3)){
      recs = safe_sprintf(recs,BUF_MAX,"%s :: COM: SET DATA: MAIL_CC:%s\n",
        gd.bf.c_name, gd.smtp.ch_MailCc);
      #ifdef SET_DEBUG 
      printf("SET: setup_conf(): %s :: COM: SET DATA: MAIL_CC:%s\n", gd.bf.c_name, gd.smtp.ch_MailCc);
      #endif
//    pthread_mutex_lock(&fd_mutex);
      safe_pthread_mutex_lock(&fd_mutex);
      if((stat = socket_send(gd.ip.sk, recs, interval)) > 0){
         recdata = select_fdcat(gd.ip.sk, recdata, &stat, BUF_MAX, interval);
         }
      if(stat < 0){
         safe_exit("setup_conf(4): socket err exit \n");
         }
      pthread_mutex_unlock(&fd_mutex);
      }
   }

else{
   fprintf(stderr,"setup_conf(): set data err MAIL_CC\n");
   }

#ifdef FREE_BUG
printf("FREE: setup_conf(): statfile_free(1): \n");
#endif
free(rec);
free(recs);
free(recdata);
free(intr);

return 0;
}


