/****************************************************************
// source file name		: TYAD_file.cc
// date of preperation	: 2016.03.10
// date of last update	: 2016.03.10
// developer			: mskim
// description 			: 파일 검색
****************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include "crd_def.h"
#include "dir_path.h"
#include "file_ctrl.h"

extern	char 	gacHomePath[128];
extern	long  glRunning;

extern	PL_FILE_CHECK_LIST	gastPlFileCheckList[];

PL_FILE_RECV_LIST gstPlFileRecvList;

/******************************************************************************
 * chk_read_file
 * parameter
 * Return
 ******************************************************************************/
int chk_read_file(char* pcFileName)
{
	int		i=0, result=0;
	while(1){ 
		if(!gastPlFileCheckList[i].pcFileName)	break;
			
		if(memcmp(gastPlFileCheckList[i].pcFileName, pcFileName, 4) == 0)
		{
			gastPlFileCheckList[i].iReadFlag = 1;
			memcpy(gstPlFileRecvList.acFileNameList[i], pcFileName, 8);
			
			//proc_log("[PL] FOUND FILE.. [%.8s]", gstPlFileRecvList.acFileNameList[i]);
		}
		
		if(gastPlFileCheckList[i].iReadFlag != 1)
		{
			result = -1;
		}
		
		i++;
	}

	i=0;
	if (result == 0)
	{
		while(1)
		{
			if(!gastPlFileCheckList[i].pcFileName)	break;
		
			gastPlFileCheckList[i].iReadFlag = 0;
			
			i++;
		}
	}

	return result;
}

/******************************************************************************
 * searchFile
 * parameter
 * Return
 ******************************************************************************/
int searchFile(int div)
{
	char			acSchFileFullPath[512];
	char			path[128], spath[128];
	char			*pcData = 0x00;
	DIR				*pDir;
	ISS_COMP		*iss_f = 0x00;
	struct	dirent	*pstEnt;
	struct	stat	stStatBuf;
	int i, cnt, pre_cnt, aft_cnt;
	int iOffSet = 0;
	int isfd, isrc, ret = 1;
	int chk_Iss = 0;
	char tmp_read[21];
	FILE *fp = NULL;
	FILE *bfp = NULL;
	FILE *afp = NULL;
	long lFileSize, lReadSize;
	
	memset(acSchFileFullPath, 0x00, sizeof(acSchFileFullPath));
	memset(&gstPlFileRecvList, 0x00, sizeof(gstPlFileRecvList));
	memset(path, 0x00, sizeof(path));
	memset(spath, 0x00, sizeof(spath));

	sprintf(acSchFileFullPath, "%s/FILE/RECV", DIR_TCS);
	
	pDir = opendir(acSchFileFullPath);

	cnt = getCountPL();

    if(div == 1)
	{
		if(cnt != MAX_PL_FILE_CNT)
		{
			ret = -1;
			return ret;
		}
	}

	if(pDir != NULL)
	{
		while((pstEnt = readdir(pDir)) != NULL)
		{
			stat(pstEnt->d_name, &stStatBuf);
			if (( (strncmp(pstEnt->d_name, "WSSF", 4) == 0)	|| (strncmp(pstEnt->d_name, "WTSF", 4) == 0)	|| (strncmp(pstEnt->d_name, "WXSF", 4) == 0)	
					|| (strncmp(pstEnt->d_name, "WYSF", 4) == 0)	|| (strncmp(pstEnt->d_name, "WBSF", 4) == 0)	|| (strncmp(pstEnt->d_name, "WZSF", 4) == 0)
					|| (strncmp(pstEnt->d_name, "VWSF", 4) == 0)	|| (strncmp(pstEnt->d_name, "VXSF", 4) == 0)	|| (strncmp(pstEnt->d_name, "WASF", 4) == 0)
					|| (strncmp(pstEnt->d_name, "NVSF", 4) == 0)	|| (strncmp(pstEnt->d_name, "NUSF", 4) == 0)	|| (strncmp(pstEnt->d_name, "NTSF", 4) == 0)
					|| (strncmp(pstEnt->d_name, "BHSF", 4) == 0)	|| (strncmp(pstEnt->d_name, "NSSF", 4) == 0)	|| (strncmp(pstEnt->d_name, "NRSF", 4) == 0)
					|| (strncmp(pstEnt->d_name, "NQSF", 4) == 0)	|| (strncmp(pstEnt->d_name, "NPSF", 4) == 0)	|| (strncmp(pstEnt->d_name, "NOSF", 4) == 0)
					|| (strncmp(pstEnt->d_name, "HLSF", 4) == 0)	|| (strncmp(pstEnt->d_name, "EPSF", 4) == 0)	|| (strncmp(pstEnt->d_name, "EQSF", 4) == 0)
					|| (strncmp(pstEnt->d_name, "V3SF", 4) == 0)	|| (strncmp(pstEnt->d_name, "V4SF", 4) == 0)	|| (strncmp(pstEnt->d_name, "V5SF", 4) == 0)
					|| (strncmp(pstEnt->d_name, "V6SF", 4) == 0)	|| (strncmp(pstEnt->d_name, "V7SF", 4) == 0) 
					|| (strncmp(pstEnt->d_name, "W1SF", 4) == 0)	|| (strncmp(pstEnt->d_name, "W2SF", 4) == 0)	|| (strncmp(pstEnt->d_name, "W3SF", 4) == 0)
					|| (strncmp(pstEnt->d_name, "W4SF", 4) == 0)	|| (strncmp(pstEnt->d_name, "W5SF", 4) == 0)	|| (strncmp(pstEnt->d_name, "W6SF", 4) == 0)
					|| (strncmp(pstEnt->d_name, "W7SF", 4) == 0)	|| (strncmp(pstEnt->d_name, "W8SF", 4) == 0)	|| (strncmp(pstEnt->d_name, "W9SF", 4) == 0)
					))
			{
				if(chk_read_file(pstEnt->d_name) == 0){
					proc_log("-----------------------------------------------------------");
					proc_log("[PL] FOUND ALL FILE..");

					for(i=0; i<MAX_PL_FILE_CNT; i++)
					{
						proc_log("Search File [%d] [%.8s]", i, gstPlFileRecvList.acFileNameList[i]);

						if(strncmp(gstPlFileRecvList.acFileNameList[i], "", 1) == 0)
						{
							proc_log("Search File init...");
							cnt=0;
							while(1)
							{
								if(!gastPlFileCheckList[cnt].pcFileName)	break;
								gastPlFileCheckList[cnt].iReadFlag = 0;
								cnt++;
							}
							closedir(pDir);
							ret = 0;
							return ret;
						}
					}

					set_PL_file(gstPlFileRecvList);
					memset(&gstPlFileRecvList, 0x00, sizeof(gstPlFileRecvList));
				}
			}
			else if(strncmp(pstEnt->d_name, "WRSF", 4) == 0){
				proc_log("-----------------------------------------------------------");
				proc_log("[BL] FOUND FILE.. [%.8s]", pstEnt->d_name);
				//set_WRSF_data(pstEnt->d_name);
			}
		}
		closedir(pDir);
	}

	return ret;
}


/******************************************************************************
 * searchFile
 * parameter
 * Return
 ******************************************************************************/
int getCountPL()
{
	char			acSchFileFullPath[512];
	DIR				*pDir;
	struct	dirent	*pstEnt;
	struct	stat	stStatBuf;
	int i, cnt = 0;
	
	memset(acSchFileFullPath, 0x00, sizeof(acSchFileFullPath));
	memset(&gstPlFileRecvList, 0x00, sizeof(gstPlFileRecvList));
		
	sprintf(acSchFileFullPath, "%s/FILE/RECV", DIR_TCS);
	
	pDir = opendir(acSchFileFullPath);
	
	if(pDir != NULL)
	{
		while((pstEnt = readdir(pDir)) != NULL)
		{
			stat(pstEnt->d_name, &stStatBuf);
			if (( (strncmp(pstEnt->d_name, "WSSF", 4) == 0)	|| (strncmp(pstEnt->d_name, "WTSF", 4) == 0)	|| (strncmp(pstEnt->d_name, "WXSF", 4) == 0)	
					|| (strncmp(pstEnt->d_name, "WYSF", 4) == 0)	|| (strncmp(pstEnt->d_name, "WBSF", 4) == 0)	|| (strncmp(pstEnt->d_name, "WZSF", 4) == 0)
					|| (strncmp(pstEnt->d_name, "VWSF", 4) == 0)	|| (strncmp(pstEnt->d_name, "VXSF", 4) == 0)	|| (strncmp(pstEnt->d_name, "WASF", 4) == 0)
					|| (strncmp(pstEnt->d_name, "NVSF", 4) == 0)	|| (strncmp(pstEnt->d_name, "NUSF", 4) == 0)	|| (strncmp(pstEnt->d_name, "NTSF", 4) == 0)
					|| (strncmp(pstEnt->d_name, "BHSF", 4) == 0)	|| (strncmp(pstEnt->d_name, "NSSF", 4) == 0)	|| (strncmp(pstEnt->d_name, "NRSF", 4) == 0)
					|| (strncmp(pstEnt->d_name, "NQSF", 4) == 0)	|| (strncmp(pstEnt->d_name, "NPSF", 4) == 0)	|| (strncmp(pstEnt->d_name, "NOSF", 4) == 0)
					|| (strncmp(pstEnt->d_name, "HLSF", 4) == 0)	|| (strncmp(pstEnt->d_name, "EPSF", 4) == 0)	|| (strncmp(pstEnt->d_name, "EQSF", 4) == 0)
					|| (strncmp(pstEnt->d_name, "V3SF", 4) == 0)	|| (strncmp(pstEnt->d_name, "V4SF", 4) == 0)	|| (strncmp(pstEnt->d_name, "V5SF", 4) == 0)
					|| (strncmp(pstEnt->d_name, "V6SF", 4) == 0)	|| (strncmp(pstEnt->d_name, "V7SF", 4) == 0) 
					|| (strncmp(pstEnt->d_name, "W1SF", 4) == 0)	|| (strncmp(pstEnt->d_name, "W2SF", 4) == 0)	|| (strncmp(pstEnt->d_name, "W3SF", 4) == 0)
					|| (strncmp(pstEnt->d_name, "W4SF", 4) == 0)	|| (strncmp(pstEnt->d_name, "W5SF", 4) == 0)	|| (strncmp(pstEnt->d_name, "W6SF", 4) == 0)
					|| (strncmp(pstEnt->d_name, "W7SF", 4) == 0)	|| (strncmp(pstEnt->d_name, "W8SF", 4) == 0)	|| (strncmp(pstEnt->d_name, "W9SF", 4) == 0)
					))
			{
				cnt++;
			}
		}
		closedir(pDir);
	}
	
	return cnt;
}
