/*
 *========================================================================
 *
 *   시스템명 : 요금징수 시스템
 *   파 일 명 : ft_rtagmst.pc
 *   작 성 자 : 조보용 (catacath@neighbor21.co.kr)
 *   작 성 일 : 2012.10.10
 *   수 정 일 :
 *   설    명 :
 *
 *========================================================================
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "bl_mst.h"
#include "bl_chg.h"

#include "imsi_bl.h"
#include "chg_struc.h"
#include "ora_sql.h"
#include "file_no.h"
#include "dir_path.h"
#include "file_lb.h"
#include "lb_func_list.h"
#include "func_list.h"
#include "iss_comp.h"
#include "tcs_db_def.h"

static struct write_info wi;
static struct bl_tbl    bl_chg_tbl_f;
static int    errflg=0;
char     msg[128];

/*
 *========================================================================
 *
 *   함 수 명 : chng_t2f_blmst
 *   작 성 자 : 조보용 (catacath@neighbor21.co.kr)
 *   작 성 일 : 2012.10.10
 *   수 정 일 :
 *   설    명 : 무효TAG 전체 (oprm_rgstr_tag_bl) 테이블을 무효TAG 마스터 파일로 변환처리 한다.
 *
 *========================================================================
*/
int chng_t2f_blchg()
{
    char   path[128], path2[128], ipath[128], bpath[128], spath[128], tpath[128];
    int    isfd, isrc;
    int    size, cnt, blcnt, chk = 0;
    int    ii, jj, kk, ret, dchk = 0;
    unsigned int   bl, chk_cnt = 0;
    unsigned short im_bl = 0;
#pragma pack(push, 1)
    unsigned char buff[2+1], comp[2+1], buff2[2+1];
	unsigned char tmp_bl[28], tmp_bin[6];
    struct iss_comp_tbl iss[2048];
	struct bl_tbl bl_chg;
    //struct bl_chg_tbl bl_chg;
#pragma pack(pop)
    char   iss_comp[4+1], tmp_comp[4+1];
    char   tmp_card[4+1], tmp_card2[4+1];
	char   snd_no_m[10]; memset(snd_no_m, 0x00, sizeof(snd_no_m));
	char   snd_no_c[10]; memset(snd_no_c, 0x00, sizeof(snd_no_c));
    struct tm    jm;
    struct stat file_info;
    struct stat iss_info;
    struct stat bl_info;
    struct dirent *entry;
	struct write_info wf;
	struct bl_mst_tbl bl_mst;
    time_t j_clock;
    FILE  *fp;
    FILE  *ofp;
    FILE  *ifp;

    sprintf(path, "%s/%s/IMSIBLMST_S", DIR_TCS, DIR_BASEINFO);
	unlink(path);
    sprintf(path2, "%s/FILE/RECV", DIR_TCS);
    sprintf(ipath, "%s/%s/ISSTBL", DIR_TCS, DIR_BASEINFO);
    sprintf(bpath, "%s/%s/TPCARDMST", DIR_TCS, DIR_BASEINFO);

    memset(tmp_bl, 0x00, sizeof(tmp_bl));
    ofp = fopen(path, "a+");
    ifp = fopen(ipath, "r+");

    DIR *dir = opendir(path2);
    if (dir == NULL) {
        return;
    }

    while ((entry = readdir(dir)) != NULL) 
    {
        if(strncmp((char *)entry->d_name, "WRSF", 4) == 0 )
        {
            proc_log("%s\n",entry->d_name);

            stat(ipath, &iss_info);
            cnt = iss_info.st_size / sizeof(struct iss_comp_tbl);

            stat(bpath, &bl_info);
            blcnt = bl_info.st_size / sizeof(struct bl_mst_tbl);
			//isfd = lb_freadx(&wf, FNO_BLMST, &bl_mst, FLAST);
			//sprintf(snd_no_m, "%08x%c", bl_mst.mngt_no, '\0');
proc_log("Master [%08x]", blcnt);

            chk_cnt = blcnt;
proc_log("Master [%d][%08x]", chk_cnt, chk_cnt);
            //lb_close(isfd);
            sprintf((char *)spath, "%s/%s", path2, entry->d_name);
			sprintf((char *)tpath, "%s/SAVE/%s", path2, entry->d_name);
            fp = fopen(spath, "rb");

            if(fp == NULL)
            {
                proc_log("후불고정BL[WRSF] File Not Found!(%s)\n", spath);
            }

            for(jj=0; jj<cnt; jj++)
            {
                fseek(ifp, jj*sizeof(struct iss_comp_tbl), SEEK_SET);
                fread(&iss[jj], 1, sizeof(struct iss_comp_tbl), ifp);
            }

            fclose(ifp);

            if( 0 > stat(spath, &file_info))
            {
                proc_log("후불고정BL[WRSF]\n");
            }else
            {
                size = file_info.st_size;

                //chk_cnt = blcnt;
                for(ii = 0; ii < size/512; ii++)
                {
                    fseek(fp, ii*512, SEEK_SET);
                    fread(comp, 1, 2, fp);
                    sprintf(tmp_comp, "%02x%02x%c", comp[0], comp[1], '\0');

                    for(jj = 0; jj < cnt; jj++)
                    {
                        sprintf(iss_comp, "%.4s%c", iss[jj].comp_cd, '\0');
                        if(strtoul(iss_comp, NULL, 16) == strtoul(tmp_comp, NULL, 16))
                        {
                            dchk = 1;
                            break;
                        }
                    }

                    if(dchk != 1) continue;

                    sprintf((char *)tmp_bin, "%.6s", iss[jj].bin_code+2);
                    chk = 0;

                    for(kk = 0; kk < 255; kk++)
                    {
                        fseek(fp, (chk*2)+2+(ii*512), SEEK_SET);
                        fread((char *)buff, 1, 2, fp);

                        sprintf(tmp_card, "%02x%02x%c", buff[0], buff[1], '\0');

                        if(strncmp(tmp_card, "ffff", 4) == 0)
                            break;

                        im_bl = strtoul(tmp_card, NULL, 16);
                        im_bl &= 0x7FFF;
                        im_bl = im_bl*32767;

                        chk++;
                        fseek(fp, (chk*2)+2+(ii*512), SEEK_SET);
                        fread(buff2, 1, 2, fp);
                        sprintf(tmp_card2, "%02x%02x%c", buff2[0], buff2[1], '\0');

                        if(buff2[0] < 0x80)
                        {
                            im_bl += strtoul(tmp_card2, NULL, 16);
                            chk++;
                        }

                        chk_cnt++;
#if 0
                        //sprintf((char *)bl_chg.snd_no, "%010d", chk_cnt);
                        //bl_chg.mngt_no = chk_cnt;
						sprintf((char *)bl_chg.snd_no, "%010d", chk_cnt);
                        //sprintf((char *)bl_chg.div, "00");
                        sprintf((char *)bl_chg.inst_yn, "00");
                        sprintf((char *)bl_chg.crd_no, "%.6s%09d0", iss[jj].bin_code+2, bl);
#else
	                    bl = im_bl;
                        sprintf((char *)tmp_bl, "%010d00%06d%09d0", chk_cnt, lb_data2atoi(tmp_bin, 6), bl);
					    //printf("BL CARD[%s]\n", tmp_bl);
						//printf("############[%09d]\n", bl);
#endif
                        //ret = fwrite((char *)&bl_chg, 22, 1, ofp);
						ret = fwrite(tmp_bl, 28, 1, ofp);

                        if(chk > 254) break;
                    }

                    if(ret < 0)
                    {
                        proc_log("후불고정BL[WSSF] Write Fail\n");
                        break;
                    }
                    chk_cnt++;
                }
            }
            fclose(fp);
        }
    }

    lb_rename(spath, tpath);
    fclose(ofp);

    /*------------------------------------------------------*/
    /* OLD    PTAGMST ==>  PTAGMST_O                     */
    /*------------------------------------------------------*/
    sprintf(spath, "%s/%s/IMSIBLMST", DIR_TCS, DIR_BASEINFO);
    sprintf(tpath, "%s/%s/IMSIBLMST_O", DIR_TCS, DIR_BASEINFO);
    lb_rename(spath, tpath);
    
    /*------------------------------------------------------*/
    /* NEW    PTAGMST_S ==>  PTAGMST                     */
    /*------------------------------------------------------*/
    
    sprintf(spath, "%s/%s/IMSIBLMST_S", DIR_TCS, DIR_BASEINFO);
    sprintf(tpath, "%s/%s/IMSIBLMST", DIR_TCS, DIR_BASEINFO);
    lb_rename(spath, tpath);

    closedir(dir);

    return(0);
}
