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

#include"sitar.h"
#include<almemsys/almemsys.h>

extern global_data_t gd;
extern struct scan_buf sb;

// V6 対応

/* ------------------ CONF SETC ----------------- */
int conf_set( char *conf_filename )
{
FILE * fp;
int inA=1;
int stat = 0;
// char * tmp;

char * chB;
char * chC;

char * chA = (char *)calloc(IP_LEN, sizeof(char));
//char * chB = (char *)calloc(IP_LEN, sizeof(char));
//char * chC = (char *)calloc(IP_LEN, sizeof(char));


if((fp = fopen(conf_filename, "r"))!=NULL){
   while( 1 ){
      chA = safe_fpcat(fp, chA, &stat, BUF_LEN);
      if(stat == -1){
         fprintf(stderr,"conf_set(): Terminal of CONFIG FILE.\n");
         break;
         }
      else {
         int po = mem_tcount(chA, ':');
         int sz = strlen(chA);
         int gg = char_count(chA, ':');
         if( NULL != strchr(chA,':') && 3 <= po && po < sz && 5 <= sz && 8 >= gg &&
           1 <= gg && 1 != mem_count(chA, '#')){

            chB = (char *)calloc(strlen(chA), sizeof(char));
            chC = (char *)calloc(strlen(chA), sizeof(char));

            chB = count_memcpy2(chB, chA, 0, (mem_tcount(chA, ':') - 1));
            chC = safe_memcpy(chC, (strchr(chA, ':') + 1), BUF_MAX);

// v6 test 用コメント
printf("chA=%s chB=%s chC=%s\n", chA, chB, chC);


            if(BUF_LEN > strlen(chC) && IP_LEN > strlen(chB)){
               rm_crlf(chC, strlen(chC));
               inA = path_check(chB);
               switch(inA){
                  case 1 : gd.ip.ip_addr = safe_memcpy(gd.ip.ip_addr, chC, IP_LEN);
                           break;
                  case 2 : gd.ip.port = strtol(chC, (char **)NULL, 10);
                           gd.ip.port_s = safe_memcpy(gd.ip.port_s, chC, IP_LEN);
                           break;
                  case 3 : gd.bf.life_int= strtol(chC, (char **)NULL, 10);
                           break;
                  case 4 : gd.bf.life_los= strtol(chC, (char **)NULL, 10);
                           if(gd.bf.life_los < 0){
                              gd.bf.life_los = 0;
                              }
                           break;
                  case 5 : gd.bf.c_info = safe_memcpy(gd.bf.c_info, chC, BUF_LEN);
                           swap_mem(gd.bf.c_info, ',', '*');
                           break;
                  case 6 : gd.smtp.ch_MailCc = safe_memcpy(gd.smtp.ch_MailCc, chC, IP_LEN);
                           break;
                  case 7 : gd.bf.c_name = safe_memcpy(gd.bf.c_name, chC, BUF_LEN);
                           swap_mem(gd.bf.c_name, ',', '*');
                           break;
                  case 8 : gd.fi.bat_filename = safe_memcpy(gd.fi.bat_filename, chC, BUF_LEN);
                           gd.fi.bat_filename_2 = safe_sprintf(gd.fi.bat_filename_2,BUF_LEN, "%s_2", gd.fi.bat_filename);
                           break;
                  case 9 : gd.fi.bat2_filename = safe_memcpy(gd.fi.bat2_filename, chC, BUF_LEN);
                           gd.fi.bat2_filename_2 = safe_sprintf(gd.fi.bat2_filename_2,BUF_LEN, "%s_2", gd.fi.bat2_filename);
                           break;

                  case 10 : gd.bf.bat_int= strtol(chC, (char **)NULL, 10);
                           break;

                  case 11 : gd.bf.bat2_int= strtol(chC, (char **)NULL, 10);
                           break;
                  case 12 : gd.fi.command_path = safe_memcpy(gd.fi.command_path, chC, BUF_LEN);
                           break;
                  case 13 : gd.ip.ip2_addr = safe_memcpy(gd.ip.ip2_addr, chC, IP_LEN);
                           break;
                  case 14 : gd.ip.port2 = strtol(chC, (char **)NULL, 10);
                           gd.ip.port2_s = safe_memcpy(gd.ip.port2_s, chC, IP_LEN);
                           break;
                  case 15 : gd.fi.update_com = safe_memcpy(gd.fi.update_com, chC, BUF_LEN);
                           break;
                  case 16 : gd.td.tcount= strtol(chC, (char **)NULL, 10); // THREAD_MAX
                           break;
                  case 17 : gd.td.tbufmax= strtol(chC, (char **)NULL, 10); // TBUF_MAX
                           break;
                  case 18 : gd.ld.onoff= strtol(chC, (char **)NULL, 10); 
                           break;
                  case 19 : gd.fi.cron_path = safe_memcpy(gd.fi.cron_path, chC, BUF_LEN);
                           break;
                  case 20 : gd.fi.cronfile_filename = safe_memcpy(gd.fi.cronfile_filename, chC, BUF_LEN);
                           break;
                  case 21 : gd.fi.cronlist_filename = safe_memcpy(gd.fi.cronlist_filename, chC, BUF_LEN);
                            gd.fi.cronlist_filename_2 = safe_sprintf(gd.fi.cronlist_filename_2, BUF_LEN, "%s_2", gd.fi.cronlist_filename);
                  case 22 : gd.cron_onoff = strtol(chC, (char **)NULL, 10);
                           break;
                  case 23 : gd.fi.midump_filename = safe_memcpy(gd.fi.midump_filename, chC, BUF_LEN);
                           break;
                  case 24 : gd.td.midcount= strtol(chC, (char **)NULL, 10); // MID_MAX
                           break;
                  case 25 : gd.td.thread_max= strtol(chC, (char **)NULL, 10); // THREAD_MAX
                           break;
                  case 26 : gd.td.midint= strtol(chC, (char **)NULL, 10); // MID_INT
                           break;
                  case 27 : gd.fi.midump_filename_2 = safe_memcpy(gd.fi.midump_filename_2, chC, BUF_LEN);
                           break;
                  case 28 : gd.di.dns_name = safe_memcpy(gd.di.dns_name, chC, BUF_LEN);   // resolv dns ip
                           break;
                  case 29 : gd.di.dns_name2 = safe_memcpy(gd.di.dns_name2, chC, BUF_LEN);   // resolv dns ip 2
                           break;

                  default : break;
                  }
	       }

            free(chB);
            free(chC);
            }
         }
      }
   fclose( fp );
   }
else {
   fprintf(stderr,"-- CONF FILE NOT OPEN %s\n",conf_filename);
   }

free(chA);
// free(chB);
// free(chC);

return 0;
}



