/****************************************************************
// source file name		: PL_file.cc
// date of preperation	: 2016.03.09
// date of last update	: 2016.03.09
// developer			: mskim
// description 			: PL 파일처리
****************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <sys/file.h>
#include "util.h"
#include "crd_def.h"
#include "dir_path.h"

extern  long	glRunning;
extern	char 	gacHomePath[128];

/*********************************************************************
// PL 파일 추가 시... (Alias No 추가 시..)
// 	1) ../../include/crd_def.h에 MAX_PL_FILE_CNT 를 증가
// 	2) ../../include/crd_def.h에 enum FILE_ID 에 추가
//  3) ../../include/crd_def.h에 #define [파밀명]_START_NO_GET(a), #define [파일명]_END_NO 추가
// 	4) 아래 gastPlFileCheckList에 Alias No 순서대로 파일을 등록
//	5) PL_make_file.cc의 get_aliasno() -> switch()안에 추가
*********************************************************************/
PL_FILE_CHECK_LIST	gastPlFileCheckList [] = 
{
	 {(char*)"WSSF", 0x00, WSSF}
	,{(char*)"WTSF", 0x00, WTSF}
	,{(char*)"WXSF", 0x00, WXSF}
	,{(char*)"WYSF", 0x00, WYSF}
	,{(char*)"WBSF", 0x00, WBSF}
	,{(char*)"WZSF", 0x00, WZSF}
	,{(char*)"VWSF", 0x00, VWSF}
	,{(char*)"VXSF", 0x00, VXSF}
	,{(char*)"NVSF", 0x00, NVSF}
	,{(char*)"NUSF", 0x00, NUSF}
	,{(char*)"WASF", 0x00, WASF}
	,{(char*)"NTSF", 0x00, NTSF}
	,{(char*)"BHSF", 0x00, BHSF}
	,{(char*)"NSSF", 0x00, NSSF}
	,{(char*)"NRSF", 0x00, NRSF}
	,{(char*)"NQSF", 0x00, NQSF}
	,{(char*)"HLSF", 0x00, HLSF}
	,{(char*)"NPSF", 0x00, NPSF}
	,{(char*)"NOSF", 0x00, NOSF}
	,{(char*)"EPSF", 0x00, EPSF}
	,{(char*)"EQSF", 0x00, EQSF}
	,{(char*)"V3SF", 0x00, V3SF}
	,{(char*)"V4SF", 0x00, V4SF}
	,{(char*)"V5SF", 0x00, V5SF}
	,{(char*)"V6SF", 0x00, V6SF}
	,{(char*)"V7SF", 0x00, V7SF}
	
	,{(char*)0x00, NULL, UNKNOWN}
};

/******************************************************************************
 * set_PL_file
 * parameter
 * Return
 ******************************************************************************/
int set_PL_file(PL_FILE_RECV_LIST stPlFileRecvList)
{
	char	acMakeFile[512];	
	char	acInfoMakeFile[512];	
	char	acChkFile[512];	
	char	acStartSeq[12];
	int		iTotCnt = 0;
	int		iAliseEndNo = PL_END_NO;
	int		iFileSchCnt = 0;
	int 	iTemp = 0;
	FILE	*pFp;
	int i;
	long lFileSize;
	char* pcData;
	int iDataLen;

	memset(acMakeFile, 0x00, sizeof(acMakeFile));
	memset(acInfoMakeFile, 0x00, sizeof(acInfoMakeFile));

	// 다시한번 파일 체크 
	for(i=0; i<MAX_PL_FILE_CNT; i++)
	{
		memset(acChkFile, 0x00, sizeof(acChkFile));
		sprintf(acChkFile, "%s/FILE/RECV/%.8s", DIR_TCS, stPlFileRecvList.acFileNameList[i]);
		lFileSize = Get_File_Size_Char(acChkFile);
		
		proc_log("Check File Size [%d] [%.8s:%d]", i, stPlFileRecvList.acFileNameList[i], lFileSize);
		
		if(lFileSize < 0)
		{
			proc_log("[PL] File Read Size Error [file : %.8s]", stPlFileRecvList.acFileNameList[i]);
			return -1;	
		}
	}
	
	sprintf(acMakeFile, 	"%s/%s/PLTPCARDMST_S", DIR_TCS, DIR_BASEINFO);
	unlink(acMakeFile);
	sprintf(acInfoMakeFile, "%s/%s/PLTCRDINFO_S", DIR_TCS, DIR_BASEINFO);

	// PLTPCARDMST_S 파일에 실제 시작되는 Alias 앞에 0x00를 입력한다 
	{
		pcData = 0x00;
		pcData = (char *) malloc(PL_START_BUFFER_SIZE+1);
		memset(pcData, 0x30, PL_START_BUFFER_SIZE);
		proc_log("[mem chk] 1 pcData malloc");
	
		proc_log("-----------------------------------------------------------");
		proc_log("[PLTPCARDMST_S] Write Start Buffer : [%d]", PL_START_BUFFER_SIZE);
	
		//pFp = fopen(acMakeFile,"wb");
		
		//if (pFp == NULL)		
		//{
		//	proc_log("PLTCRDINFO_S File Create Fail..");
		//	return -1;
		//}
		//fclose(pFp);
		
		if(Add_Write_File_Char(pcData, PL_START_BUFFER_SIZE, acMakeFile) != 0)
		{
			proc_log("File Write Error.. [%s]", acMakeFile);
			return -1;
		}
				
		if(pcData!=0x00){
			proc_log("[mem chk] 1 pcData free");
			free(pcData);		
		}
		pcData=0x00;
	}
			
	while(PROC_RUNNING == glRunning)
	{
		if(gastPlFileCheckList[iFileSchCnt].pcFileName == (char*)0x00)	
		{
			break;
		}

		// 파일을 하나씩 읽어서 PLTPCARDMST_S 파일에 Alias 정보를 만든다
		for(i=0; i<MAX_PL_FILE_CNT; i++)
		{
			if(strncmp(stPlFileRecvList.acFileNameList[i], gastPlFileCheckList[iFileSchCnt].pcFileName, 4) == 0)
			{				
				iTotCnt += make_pl_file(stPlFileRecvList.acFileNameList[i]);
				if(iTotCnt < 0)
				{
					return -1;
				}
				
			}
		}

		iFileSchCnt++;
	}
	
	// PLTPCARDMST_S 파일은 8040byte 단위로 입력하므로, 나머지 빈 공간을 '0'으로 채워준다.
	lFileSize = Get_File_Size_Char(acMakeFile);
	
	if (lFileSize > 0)
	{
		pcData=0x00;

		iDataLen = lFileSize % FILE_WRITE_UNIT_SIZE;
		
		proc_log("-----------------------------------------------------------");
		proc_log("[PLTPCARDMST_S] Write Total Len : [%d]", lFileSize);

		if(iDataLen > 0)
		{
			iDataLen = FILE_WRITE_UNIT_SIZE - iDataLen;
			
			proc_log("[PLTPCARDMST_S] %dbyte Add Write Len : [%d]", FILE_WRITE_UNIT_SIZE, iDataLen);
			
			pcData = (char *) malloc(iDataLen+1);
			memset(pcData, 0x30, iDataLen);
			proc_log("[mem chk] 3 pcData malloc");

			if(Add_Write_File_Char(pcData, iDataLen, acMakeFile) != 0)
			{
				proc_log("File Write Error.. [%s]", acMakeFile);
				return -1;
			}
			
			if(pcData!=0x00){
				proc_log("[mem chk] 3 pcData free");
				free(pcData);		
			}
			pcData=0x00;
		}
	}
	else
	{
		proc_log("File Read Error.. [%s]", acMakeFile);
		return -1;
	}
	
	// PLTCRDINFO 파일을 만들다.
	memset(acStartSeq, 0x00, sizeof(acStartSeq));
	strncpy(acStartSeq, "1", 1);
	
	pFp = fopen(acInfoMakeFile,"wb");
	
	if (pFp == NULL)		
	{
		proc_log("PLTCRDINFO_S File Create Fail..");
		return -1;
	}
	else
	{
		fwrite(acStartSeq, 1, sizeof(acStartSeq), pFp);
		iTemp = 0;
		Byte_Ordering(&iTemp, &iAliseEndNo, 4);
		fwrite(&iTemp, sizeof(iTemp), 1, pFp);
	}
	
	fflush(pFp);
	fclose(pFp);
	proc_log("PL FILE HANDLE END..");
	
	return 0;
}
