// p_merge.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "p_merge.h"
#include "..\\mergebin.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

void _MergeBinary(CString strVer, CString strCall, CString strRes, CString strOem);

void CommandLineDefault1(int argc, TCHAR* argv[], CString & strVer, CString & strCall, CString & strRes, CString & strOem);

/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;

	// initialize MFC and print and error on failure
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: change error code to suit your needs
		cerr << _T("Fatal Error: MFC initialization failed") << endl;
		nRetCode = 1;
	}
	else
	{
		// TODO: code your application's behavior here.
//		CString strHello;
//		strHello.LoadString(IDS_HELLO);
//		cout << (LPCTSTR)strHello << endl;
		CString strVer, strCall, strRes, strOem;
		
		CommandLineDefault1(argc, argv, strVer, strCall, strRes, strOem);
		_MergeBinary(strVer, strCall, strRes, strOem);
	}

	return nRetCode;
}

CString _GetVersion(CString strCurDir)
{
	CString strVersion = "Unknown";
	CStdioFile in;
	CString strIn;
	CString str;
	BOOL bStart;

	if (!in.Open(strCurDir + "inc\\version.h", CFile::modeRead|CFile::typeText))
	{
		goto End;
	}

	strVersion = "";
	bStart = FALSE;
	while (in.ReadString(strIn))
	{
		if (strIn.Left(10) == "/*########")
		{
			bStart = TRUE;
			continue;
		}
		if (!bStart) continue;

		if (strIn.Left(7) == "#define")
		{
			str = strIn.Right(strIn.GetLength() - 7);
			str.TrimLeft();
			if (str.Left(13) == "SOFT_VER_HIGH")
			{
				str = str.Right(str.GetLength() - 13);
				str.TrimLeft();
				strVersion += str;
			}
			else if (str.Left(12) == "SOFT_VER_LOW")
			{
				str = str.Right(str.GetLength() - 12);
				str.TrimLeft();
				strVersion += str;
			}
			else if (str.Left(14) == "SOFT_VER_BUILD")
			{
				str = str.Right(str.GetLength() - 14);
				str.TrimLeft();
				strVersion += str;
			}
			else if (str.Left(14) == "ENG_BUILD_HIGH")
			{
				str = str.Right(str.GetLength() - 14);
				str.TrimLeft();
				strVersion += str;
			}
			else if (str.Left(13) == "ENG_BUILD_MID")
			{
				str = str.Right(str.GetLength() - 13);
				str.TrimLeft();
				strVersion += str;
			}
			else if (str.Left(13) == "ENG_BUILD_LOW")
			{
				str = str.Right(str.GetLength() - 13);
				str.TrimLeft();
				strVersion += str;
			}
		}
	}

	in.Close();

End:
	return strVersion;
}

void _MergeBinary(CString strVer, CString strCall, CString strRes, CString strOem)
{
	CString strDstName, strCurDir;
	char szCurDir[128];
	
	GetCurrentDirectory(128, szCurDir);
	strCurDir.Format("%s\\", szCurDir);

	strDstName = strVer + "_" + strCall + "_" + strRes + "_";
	if (strOem != "Unknown")
	{
		strDstName += strOem + "_";
	}
	strDstName += _GetVersion(strCurDir);
	
	strVer.MakeLower();
	if (strVer == "pa168t" || strVer == "pa168v" || strVer == "wlink" || strVer == "iplink"	|| strVer == "wg2203" || strVer == "wg3051")
	{
		MergePa168t(strDstName, strCurDir, strCurDir);
	}
	else if (strVer == "pa168s" || strVer == "pa168p" || strVer == "pa168q"
			|| strVer == "ywh100" || strVer == "ywh10" || strVer == "ywh200" || strVer == "ywh300" || strVer == "ywh500"
			|| strVer == "maxlink" || strVer == "5111phone" || strVer == "cosun" || strVer == "kingtel"
			|| strVer == "ep668" || strVer == "yodao"
			|| strVer == "iph_e00_8b_f" || strVer == "iph_e00_8b_nf" || strVer == "iph_e00_2g"
			)
	{
		MergePa168s(strDstName, strCurDir, strCurDir);
	}
	else if (strVer == "pa168f" || strVer == "pa168fs" || strVer == "pinghe"
			|| strVer == "wg2201" || strVer == "wg2202"
			|| strVer == "iph_e00" || strVer == "iph_e00_4t")
	{
		MergePa168f(strDstName, strCurDir, strCurDir);
	}
	else if (strVer == "yuxin" || strVer == "unisen_d")
	{
		MergeYuxin(strDstName, strCurDir, strCurDir);
	}
}
