/*
 *========================================================================
 *
 *   시스템명 : 요금징수 시스템
 *   파 일 명 : manager.pc
 *   작 성 자 : 오효섭 (catacath@neighbor21.co.kr)
 *   작 성 일 : 2012.10.11
 *   수 정 일 : 
 *   설    명 : 
 *
 *========================================================================
*/

#include    <stdio.h>
#include    <signal.h>
#include    <sys/wait.h>
#include    <sys/errno.h>
#include    <sys/types.h>
#include    <sys/ipc.h>
#include    <sys/msg.h>
#include    <time.h>

#include "dir_path.h"
#include "path_def.h"
#include "chg_struc.h"
#include "def_set.h"
#include "crd_def.h"

long  glRunning     = PROC_RUNNING;

extern int f_errno;
extern int f_recnum;

/*
 *========================================================================
 *
 *   함 수 명 : SIG_term
 *   작 성 자 : 오효섭 (catacath@neighbor21.co.kr)
 *   작 성 일 : 2012.10.10
 *   수 정 일 :
 *   설    명 : 
 *
 *========================================================================
*/

void SIG_term(int sig)
{
    disconnect_db();
    exit(0);
}

/*
 *========================================================================
 *
 *   함 수 명 : main
 *   작 성 자 : 오효섭 (catacath@neighbor21.co.kr)
 *   작 성 일 : 2012.10.10
 *   수 정 일 :
 *   설    명 : 
 *
 *========================================================================
*/

int main(int argc, char *argv[])
{
    char uid[20]; memset(uid, 0x00, sizeof(uid));
    char pwd[20]; memset(pwd, 0x00, sizeof(pwd));
    char host[20]; memset(host, 0x00, sizeof(host));
    char plz_id[2];
    char s_date[16];
    char e_date[8+1];
    char f_date[16];
    char wrk_dir[128];
    char save_dir[128];
    char msg[1024];
    char oMsg[1024];
    char run_path[64];
    char bin_path[64];
    char spath[128];
    char tpath[128];
    char upid_bl_snd_yn[1+1];
    int  rvs_basic, rvs_wrkr, rvs_immcarno, rvs_rfid, rvs_bef, rvs_bin, rvs_pl;
    char fare_id[2+1];
    char tmp_rvs[2+1];
    char tmp_rvs2[2+1];
    char sToday[8+1];
    int  result;
    int    i, status, size;
    int    sckm = 0;
    FILE   *fp = NULL;
    time_t ctime;
    struct tm *ctm;
    time_t ytime;
    struct tm *ytm;
    int    nResult, blno;
    char   ch;
    char   *p;
    char   *pData;
    char   *pProc[10] = { NULL, };
    struct chg_struc cs;
    struct write_info wi;
    //struct basic_tbl bs;
    int seq, ret;
    //pid_t prc_id[99];
    int maxPrcnt = 0;
    int makePL = 0;
    //int procPid[99];
    pid_t pid;
    pid_t pid_child;
    struct {
        pid_t prc_id;
        int procPid;
    } proc_stat[99];

    p = argv[0];

    sprintf(wrk_dir, "%s/manager", LOG_DIR);
    mkdir(wrk_dir, 0755);
    chdir(wrk_dir);

    signal(SIGTERM, SIG_term);

    sprintf(plz_id, "%.2s", PLZ_CODE2);
    memcpy(fare_id, plz_id, 2);
    fare_id[2] = '\0';
    memset(run_path, 0x00, sizeof(run_path));
    
    sprintf(run_path, "%s/PROCESS.CFG", INI_DIR);
    fp = fopen(run_path, "r+");

    sckm = 1;
    fclose(fp);

    while(1)
    {
        ctime = time(NULL);
        ctm = localtime(&ctime);

        if((ctm->tm_hour == 24) && (ctm->tm_min == 05) && (ctm->tm_sec == 10))
        {
            ytime = time(NULL) - (24*60*60);
            ytm = localtime(&ytime);

			printf("1\n");
            memset(msg, 0x00, sizeof(msg));
            memset(uid, 0x00, sizeof(uid));
            memset(pwd, 0x00, sizeof(pwd));
            memset(host, 0x00, sizeof(host));
            printf("2\n");
			sprintf(uid, "btoll%c", '\0');
            sprintf(pwd, "btoll%c", '\0');
            sprintf(host,"150.50.46.11%c", '\0');  
            sprintf(msg, "%s/mget.sh %s %s %s /DOTR/%04d%02d%02d/%.2s/IMAGE /app_data/tcs/FILE/RECV/IMAGE *.JPG 22", DIR_BIN, host, uid, pwd, ytm->tm_year+1900, ytm->tm_mon+1, ytm->tm_mday, PLZ_CODE2);
            system(msg);
            sprintf(msg, "%s/mget.sh %s %s %s /DOTR/%04d%02d%02d/%.2s/IMAGE /app_data/tcs/FILE/RECV/IMAGE *.JPG 22", DIR_BIN, host, uid, pwd, ytm->tm_year+1900, ytm->tm_mon+1, ytm->tm_mday, PLZ_CODE2);
            system(msg);
            sleep(1);
			printf("3\n");
            memset(msg, 0x00, sizeof(msg));
            memset(uid, 0x00, sizeof(uid));
            memset(pwd, 0x00, sizeof(pwd));
            memset(host, 0x00, sizeof(host));
            sprintf(uid, "tes");
            sprintf(pwd, "tes");
            sprintf(host,"192.168.201.120");
            sprintf(msg, "%s/imageMk.sh %s %s %s /images/ANPRImage/cfs/%04d /images/ANPRImage/cfs/%04d/%.2s/%04d%02d%02d %.2s", DIR_BIN, host, uid, pwd, ytm->tm_year+1900, ytm->tm_year+1900, PLZ_CODE2, ytm->tm_year+1900, ytm->tm_mon+1, ytm->tm_mday, PLZ_CODE2);
            system(msg);
			sleep(1);
			printf("4\n");
            memset(msg, 0x00, sizeof(msg));
            memset(uid, 0x00, sizeof(uid));
            memset(pwd, 0x00, sizeof(pwd));
            memset(host, 0x00, sizeof(host));
            sprintf(uid, "tes");
            sprintf(pwd, "tes");
            sprintf(host,"192.168.201.120");
            sprintf(msg, "%s/mput.sh %s %s %s /images/ANPRImage/cfs/%04d/%.2s/%04d%02d%02d /app_data/tcs/FILE/RECV/IMAGE *.JPG 22", DIR_BIN, host, uid, pwd, ytm->tm_year+1900, PLZ_CODE2, ytm->tm_year+1900, ytm->tm_mon+1, ytm->tm_mday);
			system(msg);
            sprintf(msg, "%s/mput.sh %s %s %s /images/ANPRImage/cfs/%04d/%.2s/%04d%02d%02d /app_data/tcs/FILE/RECV/IMAGE *.JPG 22", DIR_BIN, host, uid, pwd, ytm->tm_year+1900, PLZ_CODE2, ytm->tm_year+1900, ytm->tm_mon+1, ytm->tm_mday);
			system(msg);
			printf("5\n");
			memset(msg, 0x00, sizeof(msg));
			sprintf(msg, "rm -rf /app_data/tcs/FILE/RECV/IMAGE/*.JPG");
			system(msg);
        }

        sleep(1);
    }
}
