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

#include "stdafx.h"
#include "relink.h"

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

void _RewriteLinkFile(CString strFileName, CString strVer, CString strCall, CString strRes, CString strOem);

void CommandLineDefault(int argc, TCHAR* argv[], CString & strFileName, 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
	{
		CString strFileName, strVer, strCall, strRes, strOem;
		
		CommandLineDefault(argc, argv, strFileName, strVer, strCall, strRes, strOem);
		_RewriteLinkFile(strFileName, strVer, strCall, strRes, strOem);
	}

	return nRetCode;
}

BOOL _ProcessString(CString & str, CString strVer, CString strCall, CString strRes, CString strOem)
{
	CString strNew;

	if (str.Left(7) != "..\\lib\\")		return FALSE;

	if (str.Right(5) == "arp.o")
	{
		strNew = "..\\lib\\arp.o";
		if (strVer == "ar168o" || strVer == "ar168kd")
		{
			strNew = "..\\lib\\dm9003\\arp.o";
		}
		else if (strVer == "ar168p" || strVer == "ar168km")
		{
			strNew = "..\\lib\\ksz8842\\arp.o";
		}

		if (strNew == str)		return FALSE;
	}
	else if (str.Right(6) == "chip.o")
	{
		strNew = "..\\lib\\chip.o";
		if (strVer == "ar168k" || strVer == "ar168kd" || strVer == "ar168km" || strVer == "ar168m" || strVer == "ar168ms" 
			|| strVer == "gp2266" || strVer == "bt2008n" || strVer == "fwv2800" || strVer == "dxdt" || strVer == "ar168o" 
			|| strVer == "ar168p" || strVer == "ar168l" || strVer == "ar168ls")
		{
			strNew = "..\\lib\\chip_plus\\chip.o";
		}

		if (strNew == str)		return FALSE;
	}
	else if (str.Right(7) == "codec.o")
	{
		strNew = "..\\lib\\codec.o";
		if (strVer == "ar168k" || strVer == "ar168kd" || strVer == "ar168km" || strVer == "ar168m" || strVer == "ar168ms" 
			|| strVer == "gp2266" || strVer == "bt2008n" || strVer == "fwv2800" || strVer == "dxdt" || strVer == "ar168o" 
			|| strVer == "ar168p" || strVer == "ar168l" || strVer == "ar168ls")
		{
			strNew = "..\\lib\\chip_plus\\codec.o";
		}
		else if (strVer == "gp1266" || strVer == "bt2008" || strVer == "qx900w" || strVer == "ar168j")
		{
			strNew = "..\\lib\\dual_mic\\codec.o";
		}

		if (strNew == str)		return FALSE;
	}
	else if (str.Right(5) == "dsp.o")
	{
		strNew = "..\\lib\\dsp.o";
		if ((strVer == "bt2008n" && strOem == "ivr") || strVer == "ar168l" || strVer == "ar168ls")
		{
			strNew = "..\\lib\\ivr\\dsp.o";
		}
		else if (strVer == "ar168k" || strVer == "ar168kd" || strVer == "ar168km" || strVer == "ar168m" || strVer == "ar168ms" 
			|| strVer == "gp2266" || strVer == "bt2008n" || strVer == "fwv2800" || strVer == "dxdt" || strVer == "ar168o" 
			|| strVer == "ar168p")
		{
			strNew = "..\\lib\\chip_plus\\dsp.o";
		}

		if (strNew == str)		return FALSE;
	}
	else if (str.Right(11) == "dsp_bank1.o")
	{
		strNew = "..\\lib\\dsp_bank1.o";
		if (strVer == "ar168l" || strVer == "ar168ls")
		{
			strNew = "..\\lib\\slic\\dsp_bank1.o";
		}

		if (strNew == str)		return FALSE;
	}
	else if (str.Right(7) == "flash.o")
	{
		strNew = "..\\lib\\flash.o";
		if (strVer == "ar168k" || strVer == "ar168m" || strVer == "ar168l")
		{
			strNew = "..\\lib\\chip_plus\\flash.o";
		}
		else if (strVer == "gp2266" || strVer == "bt2008n" || strVer == "fwv2800" || strVer == "dxdt" || strVer == "ar168kd" || strVer == "ar168km"
			|| strVer == "ar168ms" || strVer == "ar168o" || strVer == "ar168p" || strVer == "ar168ls")
		{
			strNew = "..\\lib\\sst\\flash.o";
		}

		if (strNew == str)		return FALSE;
	}
	else if (str.Right(6) == "icmp.o")
	{
		strNew = "..\\lib\\icmp.o";
		if (strVer == "ar168o" || strVer == "ar168kd")
		{
			strNew = "..\\lib\\dm9003\\icmp.o";
		}
		else if (strVer == "ar168p" || strVer == "ar168km")
		{
			strNew = "..\\lib\\ksz8842\\icmp.o";
		}

		if (strNew == str)		return FALSE;
	}
	else if (str.Right(4) == "ip.o")
	{
		strNew = "..\\lib\\ip.o";
		if (strVer == "ar168o" || strVer == "ar168kd")
		{
			strNew = "..\\lib\\dm9003\\ip.o";
		}

		if (strNew == str)		return FALSE;
	}
	else if (str.Right(5) == "isr.o")
	{
		strNew = "..\\lib\\isr.o";
		if (strVer == "ar168k" || strVer == "ar168kd" || strVer == "ar168km" || strVer == "ar168j" || strVer == "gp1266" 
			|| strVer == "bt2008" || strVer == "qx900w"	|| strVer == "ar168l" || strVer == "ar168ls")
		{
			strNew = "..\\lib\\key_5x5\\isr.o";
		}
		else if (strVer == "gp2266" || strVer == "bt2008n" || strVer == "fwv2800" || strVer == "dxdt" || strVer == "ar168o" || strVer == "ar168p")
		{
			strNew = "..\\lib\\key_8x5\\isr.o";
		}
		else if (strVer == "ar168m" || strVer == "ar168ms" || (strVer == "ar168f" && strOem == "uart"))
		{
			strNew = "..\\lib\\uart\\isr.o";
		}

		if (strNew == str)		return FALSE;
	}
	else if (str.Right(5) == "key.o")
	{
		//	ar168j, ar168k, gp1266, bt2008, qx900w, gp2266, bt2008n, fwv2800, ar168f&uart, ar168m, ar168ms, dxdt, ar168o
		strNew = "..\\lib\\key.o";
		if (strVer == "gp1266" || strVer == "bt2008" || strVer == "qx900w")
		{
			strNew = "..\\lib\\key_5x5\\key.o";
		}
		else if (strVer == "ar168k" || strVer == "ar168kd" || strVer == "ar168km" || strVer == "ar168j" || strVer == "gp2266" 
			|| strVer == "bt2008n" 	|| strVer == "fwv2800" || strVer == "dxdt" || strVer == "ar168o" || strVer == "ar168p" 
			|| strVer == "ar168l" || strVer == "ar168ls")
		{
			strNew = "..\\lib\\key_8x5\\key.o";
		}
		else if ((strVer == "ar168f" && strOem == "uart") || strVer == "ar168ms" || strVer == "ar168m")
		{
			strNew = "..\\lib\\uart\\key.o";
		}

		if (strNew == str)		return FALSE;
	}
	else if (str.Right(10) == "network2.o")
	{
		strNew = "..\\lib\\network2.o";
		if (strVer == "ar168o" || strVer == "ar168kd")
		{
			strNew = "..\\lib\\dm9003\\network2.o";
		}
		else if (strVer == "ar168p" || strVer == "ar168km")
		{
			strNew = "..\\lib\\ksz8842\\network2.o";
		}

		if (strNew == str)		return FALSE;
	}
	else if (str.Right(5) == "sys.o")
	{
		//	gp2266, bt2008n, fwv2800, dxdt, ar168o, ar168ms, ar168kd
		strNew = "..\\lib\\sys.o";
		if (strVer == "gp2266" || strVer == "bt2008n" || strVer == "fwv2800" || strVer == "dxdt" || strVer == "ar168kd" || strVer == "ar168km"
			|| strVer == "ar168ms" || strVer == "ar168o" || strVer == "ar168p" || strVer == "ar168ls")
		{
			strNew = "..\\lib\\sst\\sys.o";
		}

		if (strNew == str)		return FALSE;
	}
	else if (str.Right(5) == "tcp.o")
	{
		strNew = "..\\lib\\tcp.o";
		if (strVer == "ar168o" || strVer == "ar168kd")
		{
			// ar168o
			strNew = "..\\lib\\dm9003\\tcp.o";
		}

		if (strNew == str)		return FALSE;
	}
	else if (str.Right(6) == "uart.o")
	{
		// ar168f&uart, ar168m, ar168ms
		strNew = "..\\lib\\uart.o";
		if (strVer == "ar168m" || strVer == "ar168ms" || (strVer == "ar168f" && strOem == "uart"))
		{
			strNew = "..\\lib\\uart\\uart.o";
		}

		if (strNew == str)		return FALSE;
	}
	else if (str.Right(5) == "udp.o")
	{
		strNew = "..\\lib\\udp.o";
		if (strVer == "ar168o" || strVer == "ar168kd")
		{
			// ar168o
			strNew = "..\\lib\\dm9003\\udp.o";
		}

		if (strNew == str)		return FALSE;
	}
	else if (str.Right(8) == "udprun.o")
	{
		strNew = "..\\lib\\udprun.o";
		if (strVer == "ar168o" || strVer == "ar168kd")
		{
			// ar168o
			strNew = "..\\lib\\dm9003\\udprun.o";
		}
		else if (strVer == "ar168p" || strVer == "ar168km")
		{
			strNew = "..\\lib\\ksz8842\\udprun.o";
		}

		if (strNew == str)		return FALSE;
	}
	else
	{
		return FALSE;
	}

	str = strNew;
	return TRUE;
}

void _RewriteLinkFile(CString strFileName, CString strVer, CString strCall, CString strRes, CString strOem)
{
	CStdioFile file;
	CString strName;
	CString str;
	char szCurDir[128];
	CStringList list;
	POSITION pos, old;
	BOOL bChanged;

	GetCurrentDirectory(128, szCurDir);
	strName.Format("%s\\%s", szCurDir, strFileName);

	strVer.MakeLower();
	strCall.MakeLower();
	strRes.MakeLower();
	strOem.MakeLower();

	// read first
	if (!file.Open(strName, CFile::modeRead|CFile::typeText))
	{
		return;
	}
	while (file.ReadString(str))
	{
		list.AddTail(str);
	}
	file.Close();

	// process
	bChanged = FALSE;
	for (pos = list.GetHeadPosition(); pos != NULL;)
	{
		old = pos;
		str = list.GetNext(pos);
		if (_ProcessString(str, strVer, strCall, strRes, strOem))
		{
			list.SetAt(old, str);
			bChanged = TRUE;
		}
	}

	if (!bChanged)	return;		// no need to rewrite

	// write
	file.Open(strName, CFile::modeCreate|CFile::modeWrite|CFile::typeText);
	while (!list.IsEmpty())
	{
		str = list.RemoveHead();
		str += "\n";
		file.WriteString(str);
	}
	file.Close();
}
