//
//
// Create a DSW and DSP file with correct compiler settings for a GameOS based products
//
//
//
//
//
#include <windows.h> 
#include <commdlg.h> 
#include "resource.h"
//
//
HINSTANCE	hInst;	
POINT		DialogScreenPosition;
RECT		DialogRect;
char		ProjectName[MAX_PATH];
char		Buffer[64*1024];
char		Name[MAX_PATH];




//
//
// Empty DSW file project name replaced with ~ character
//
//
char DSWString[]=
"Microsoft Developer Studio Workspace File, Format Version 6.00\r\n"\
"# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!\r\n"\
"\r\n"\
"###############################################################################\r\n"\
"\r\n"\
"Project: \"~\"=\".\\~.dsp\" - Package Owner=<4>\r\n"\
"\r\n"\
"Package=<5>\r\n"\
"{{{\r\n"\
"}}}\r\n"\
"\r\n"\
"Package=<4>\r\n"\
"{{{\r\n"\
"}}}\r\n"\
"\r\n"\
"###############################################################################\r\n"\
"\r\n"\
"Global:\r\n"\
"\r\n"\
"Package=<5>\r\n"\
"{{{\r\n"\
"}}}\r\n"\
"\r\n"\
"Package=<3>\r\n"\
"{{{\r\n"\
"}}}\r\n"\
"\r\n"\
"###############################################################################\r\n"\
"\r\n";



//
// Empty DSP file - project name replaced with ~ character
//
char DSPString[]=
"# Microsoft Developer Studio Project File - Name=\"~\" - Package Owner=<4>\r\n"\
"# Microsoft Developer Studio Generated Build File, Format Version 6.00\r\n"\
"# ** DO NOT EDIT **\r\n"\
"\r\n"\
"# TARGTYPE \"Win32 (x86) Application\" 0x0101\r\n"\
"\r\n"\
"CFG=~ - Win32 Debug\r\n"\
"!MESSAGE This is not a valid makefile. To build this project using NMAKE,\r\n"\
"!MESSAGE use the Export Makefile command and run\r\n"\
"!MESSAGE \r\n"\
"!MESSAGE NMAKE /f \"~.mak\".\r\n"\
"!MESSAGE \r\n"\
"!MESSAGE You can specify a configuration when running NMAKE\r\n"\
"!MESSAGE by defining the macro CFG on the command line. For example:\r\n"\
"!MESSAGE \r\n"\
"!MESSAGE NMAKE /f \"~.mak\" CFG=\"~ - Win32 Debug\"\r\n"\
"!MESSAGE \r\n"\
"!MESSAGE Possible choices for configuration are:\r\n"\
"!MESSAGE \r\n"\
"!MESSAGE \"~ - Win32 Release\" (based on \"Win32 (x86) Application\")\r\n"\
"!MESSAGE \"~ - Win32 Profile\" (based on \"Win32 (x86) Application\")\r\n"\
"!MESSAGE \"~ - Win32 Armor\" (based on \"Win32 (x86) Application\")\r\n"\
"!MESSAGE \"~ - Win32 Debug\" (based on \"Win32 (x86) Application\")\r\n"\
"!MESSAGE \r\n"\
"\r\n"\
"# Begin Project\r\n"\
"# PROP Scc_ProjName \"\"\r\n"\
"# PROP Scc_LocalPath \"\"\r\n"\
"CPP=cl.exe\r\n"\
"MTL=midl.exe\r\n"\
"RSC=rc.exe\r\n"\
"\r\n"\
"!IF  \"$(CFG)\" == \"~ - Win32 Release\"\r\n"\
"\r\n"\
"# PROP BASE Use_MFC 0\r\n"\
"# PROP BASE Use_Debug_Libraries 0\r\n"\
"# PROP BASE Output_Dir \"Release\"\r\n"\
"# PROP BASE Intermediate_Dir \"Release\"\r\n"\
"# PROP BASE Target_Dir \"\"\r\n"\
"# PROP Use_MFC 0\r\n"\
"# PROP Use_Debug_Libraries 0\r\n"\
"# PROP Output_Dir \"Release\"\r\n"\
"# PROP Intermediate_Dir \"Release\"\r\n"\
"# PROP Ignore_Export_Lib 0\r\n"\
"# PROP Target_Dir \"\"\r\n"\
"# ADD BASE CPP /nologo /MD /W3 /GX /O2 /D \"WIN32\" /D \"NDEBUG\" /D \"_WINDOWS\" /YX /FD /c\r\n"\
"# ADD CPP /nologo /G6 /Gz /Zp4 /W4 /GR /Zi /MD /Ox /Ot /Oa /Og /Oi /GF /Gy /I \"..\\..\\Libraries\" /I \"..\\..\\..\\CoreTech\\Libraries\" /D \"WIN32\" /D \"NDEBUG\" /D \"_WINDOWS\" /YX /FD /c\r\n"\
"# ADD BASE MTL /nologo /D \"NDEBUG\" /mktyplib203 /o NUL /win32\r\n"\
"# ADD MTL /nologo /D \"NDEBUG\" /mktyplib203 /o NUL /win32\r\n"\
"# ADD BASE RSC /l 0x409 /d \"NDEBUG\"\r\n"\
"# ADD RSC /l 0x409 /d \"NDEBUG\"\r\n"\
"BSC32=bscmake.exe\r\n"\
"# ADD BASE BSC32 /nologo\r\n"\
"# ADD BSC32 /nologo\r\n"\
"LINK32=link.exe\r\n"\
"# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:windows /machine:I386\r\n"\
"# ADD LINK32 /nologo /subsystem:windows /debug /machine:I386 /fixed:no\r\n"\
"# SUBTRACT LINK32 /pdb:none\r\n"\
"\r\n"\
"!ELSEIF  \"$(CFG)\" == \"~ - Win32 Profile\"\r\n"\
"\r\n"\
"# PROP BASE Use_MFC 0\r\n"\
"# PROP BASE Use_Debug_Libraries 0\r\n"\
"# PROP BASE Output_Dir \"Profile\"\r\n"\
"# PROP BASE Intermediate_Dir \"Profile\"\r\n"\
"# PROP BASE Target_Dir \"\"\r\n"\
"# PROP Use_MFC 0\r\n"\
"# PROP Use_Debug_Libraries 0\r\n"\
"# PROP Output_Dir \"Profile\"\r\n"\
"# PROP Intermediate_Dir \"Profile\"\r\n"\
"# PROP Ignore_Export_Lib 0\r\n"\
"# PROP Target_Dir \"\"\r\n"\
"# ADD BASE CPP /nologo /MD /W3 /GX /O2 /D \"WIN32\" /D \"NDEBUG\" /D \"_WINDOWS\" /YX /FD /c\r\n"\
"# ADD CPP /nologo /G6 /Gz /Zp4 /W4 /GR /Zi /MD /Ox /Ot /Oa /Og /Oi /GF /Gy /I \"..\\..\\Libraries\" /I \"..\\..\\..\\CoreTech\\Libraries\" /D \"LAB_ONLY\" /D \"WIN32\" /D \"NDEBUG\" /D \"_WINDOWS\" /YX /FD /c\r\n"\
"# ADD BASE MTL /nologo /D \"NDEBUG\" /mktyplib203 /o NUL /win32\r\n"\
"# ADD MTL /nologo /D \"NDEBUG\" /mktyplib203 /o NUL /win32\r\n"\
"# ADD BASE RSC /l 0x409 /d \"NDEBUG\"\r\n"\
"# ADD RSC /l 0x409 /d \"NDEBUG\"\r\n"\
"BSC32=bscmake.exe\r\n"\
"# ADD BASE BSC32 /nologo\r\n"\
"# ADD BSC32 /nologo\r\n"\
"LINK32=link.exe\r\n"\
"# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:windows /machine:I386\r\n"\
"# ADD LINK32 /nologo /subsystem:windows /debug /machine:I386 /fixed:no\r\n"\
"# SUBTRACT LINK32 /pdb:none\r\n"\
"\r\n"\
"!ELSEIF  \"$(CFG)\" == \"~ - Win32 Debug\"\r\n"\
"\r\n"\
"# PROP BASE Use_MFC 0\r\n"\
"# PROP BASE Use_Debug_Libraries 1\r\n"\
"# PROP BASE Output_Dir \"Debug\"\r\n"\
"# PROP BASE Intermediate_Dir \"Debug\"\r\n"\
"# PROP BASE Target_Dir \"\"\r\n"\
"# PROP Use_MFC 0\r\n"\
"# PROP Use_Debug_Libraries 1\r\n"\
"# PROP Output_Dir \"Debug\"\r\n"\
"# PROP Intermediate_Dir \"Debug\"\r\n"\
"# PROP Ignore_Export_Lib 0\r\n"\
"# PROP Target_Dir \"\"\r\n"\
"# ADD BASE CPP /nologo /MDd /W3 /Gm /GX /Zi /Od /D \"WIN32\" /D \"_DEBUG\" /D \"_WINDOWS\" /YX /FD /c\r\n"\
"# ADD CPP /nologo /G6 /Gz /Zp4 /W4 /GR /Zi /Od /MDd /I \"..\\..\\Libraries\" /I \"..\\..\\..\\CoreTech\\Libraries\" /D \"WIN32\" /D \"_DEBUG\" /D \"NOMINMAX\" /D \"_ARMOR\" /D \"LAB_ONLY\" /D \"_WINDOWS\" /YX /FD /c\r\n"\
"# SUBTRACT CPP /Gf /Gy\r\n"\
"# ADD BASE MTL /nologo /D \"_DEBUG\" /mktyplib203 /o NUL /win32\r\n"\
"# ADD MTL /nologo /D \"_DEBUG\" /mktyplib203 /o NUL /win32\r\n"\
"# ADD BASE RSC /l 0x409 /d \"_DEBUG\"\r\n"\
"# ADD RSC /l 0x409 /d \"_DEBUG\"\r\n"\
"BSC32=bscmake.exe\r\n"\
"# ADD BASE BSC32 /nologo\r\n"\
"# ADD BSC32 /nologo\r\n"\
"LINK32=link.exe\r\n"\
"# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:windows /debug /machine:I386 /pdbtype:sept\r\n"\
"# ADD LINK32 /nologo /subsystem:windows /debug /machine:I386 /fixed:no\r\n"\
"# SUBTRACT LINK32 /pdb:none\r\n"\
"\r\n"\
"!ELSEIF  \"$(CFG)\" == \"~ - Win32 Armor\"\r\n"\
"\r\n"\
"# PROP BASE Use_MFC 0\r\n"\
"# PROP BASE Use_Debug_Libraries 0\r\n"\
"# PROP BASE Output_Dir \"Armor\"\r\n"\
"# PROP BASE Intermediate_Dir \"Armor\"\r\n"\
"# PROP BASE Ignore_Export_Lib 0\r\n"\
"# PROP BASE Target_Dir \"\"\r\n"\
"# PROP Use_MFC 0\r\n"\
"# PROP Use_Debug_Libraries 0\r\n"\
"# PROP Output_Dir \"Armor\"\r\n"\
"# PROP Intermediate_Dir \"Armor\"\r\n"\
"# PROP Ignore_Export_Lib 0\r\n"\
"# PROP Target_Dir \"\"\r\n"\
"# ADD BASE CPP /nologo /G6 /Zp4 /MD /W4 /GR /Zi /Ox /Ot /Oa /Og /Oi /Gy /I \"..\\..\\Libraries\" /I \"..\\..\\..\\CoreTech\\Libraries\" /D \"PROFILE\" /D \"WIN32\" /D \"NDEBUG\" /D \"_WINDOWS\" /D \"LAB_ONLY\" /YX /FD /GF /c\r\n"\
"# ADD CPP /nologo /G6 /Gz /Zp4 /MD /W4 /GR /Zi /Ox /Ot /Oa /Og /Oi /Gy /I \"..\\..\\Libraries\" /I \"..\\..\\..\\CoreTech\\Libraries\" /D \"NDEBUG\" /D \"LAB_ONLY\" /D \"_WINDOWS\" /D \"WIN32\" /D \"NOMINMAX\" /D \"_ARMOR\" /YX /FD /GF /c\r\n"\
"# ADD BASE MTL /nologo /D \"NDEBUG\" /mktyplib203 /o \"NUL\" /win32\r\n"\
"# ADD MTL /nologo /D \"NDEBUG\" /mktyplib203 /o \"NUL\" /win32\r\n"\
"# ADD BASE RSC /l 0x409 /d \"NDEBUG\"\r\n"\
"# ADD RSC /l 0x409 /d \"NDEBUG\"\r\n"\
"BSC32=bscmake.exe\r\n"\
"# ADD BASE BSC32 /nologo\r\n"\
"# ADD BSC32 /nologo\r\n"\
"LINK32=link.exe\r\n"\
"# ADD BASE LINK32 /nologo /subsystem:windows /debug /machine:I386 /fixed:no\r\n"\
"# SUBTRACT BASE LINK32 /pdb:none\r\n"\
"# ADD LINK32 /nologo /subsystem:windows /debug /machine:I386 /fixed:no\r\n"\
"# SUBTRACT LINK32 /pdb:none\r\n"\
"\r\n"\
"!ENDIF \r\n"\
"\r\n"\
"# Begin Target\r\n"\
"\r\n"\
"# Name \"~ - Win32 Release\"\r\n"\
"# Name \"~ - Win32 Profile\"\r\n"\
"# Name \"~ - Win32 Armor\"\r\n"\
"# Name \"~ - Win32 Debug\"\r\n"\
"# Begin Source File\r\n"\
"\r\n"\
"SOURCE=\".\\~.cpp\"\r\n"\
"# End Source File\r\n"\
"# End Target\r\n"\
"# End Project\r\n"\
"\r\n";



//
// Decode the DSW or DSP file passed into Buffer and expand project name
//
DWORD Decode( char* Source )
{
	char* Dest=Buffer;
	DWORD Size=0;
	while( *Source )
	{
		if( *Source=='~' )
		{
			strcpy( Dest, Name );
			Dest+=strlen( Name );
		}
		else
			*Dest++=*Source;
		Source++;
	}
	return Dest-Buffer;
}




BOOL APIENTRY Dialog( HWND hDlg, UINT message, UINT wParam, LONG lParam)
{
    switch (message)
    {
//
// Center dialog
//
        case WM_INITDIALOG:                
			RECT ParentRect;
			POINT DialogScreenPosition;
			GetWindowRect(hDlg,&DialogRect);
			GetClientRect(GetDesktopWindow(), &ParentRect);

			DialogScreenPosition.x = (ParentRect.right-ParentRect.left)/2;
			DialogScreenPosition.y = (ParentRect.bottom-ParentRect.top)/2;

			DialogScreenPosition.x -= (DialogRect.right-DialogRect.left)/2;
			DialogScreenPosition.y -= (DialogRect.bottom-DialogRect.top)/2;

			MoveWindow(hDlg,DialogScreenPosition.x,DialogScreenPosition.y,DialogRect.right-DialogRect.left,DialogRect.bottom-DialogRect.top,1);
			break;
//
// Close window - exit
//
		case WM_CLOSE:
			EndDialog( hDlg, 0 );
			break;

		case WM_COMMAND:                      
//
// Clicked on Ok - write out files
//
            if(LOWORD(wParam) == ID_OK) 
            {        
				GetDlgItemText( hDlg,IDC_BUILDLOCATION, ProjectName, sizeof(ProjectName) );
//
// Make sure the Project name does not end in DSW
//
				char NamePath[MAX_PATH];
				if( 0==strnicmp( &ProjectName[strlen(ProjectName)-4],".dsw", 4) )
				{
					ProjectName[strlen(ProjectName)-4]=0;	
				}
				strcpy( NamePath, ProjectName );
//
// Find just the name
//
				DWORD t1=strlen(NamePath);
				while( t1>0 && NamePath[t1-1]!='\\' )
					t1--;
				strcpy( Name, &NamePath[t1] );
//
// Write out DSW file
//
				char DSWFile[MAX_PATH];
				strcpy( DSWFile, NamePath );
				strcat( DSWFile, ".dsw" );

				HANDLE hFile = CreateFile( DSWFile, GENERIC_WRITE, 0,NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN, NULL );
				if( hFile!=INVALID_HANDLE_VALUE )
				{
					DWORD temp=0;
					WriteFile( hFile, Buffer, Decode( DSWString ), &temp, NULL );
					CloseHandle( hFile );
				}
//
// Write out DSP file
//
				char DSPFile[MAX_PATH];
				strcpy( DSPFile, NamePath );
				strcat( DSPFile, ".dsp" );

				hFile = CreateFile( DSPFile, GENERIC_WRITE, 0,NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN, NULL );
				if( hFile!=INVALID_HANDLE_VALUE )
				{
					DWORD temp=0;
					WriteFile( hFile, Buffer, Decode( DSPString ), &temp, NULL );
					CloseHandle( hFile );
				}
                EndDialog(hDlg, 1);
                return TRUE;
            }
//
// Clicked on 'Browse' button
//
            if(LOWORD(wParam) == IDC_BROWSE) 
            {        
				OPENFILENAME OpenFileName; 
				TCHAR szFile[MAX_PATH] = "";
				OpenFileName.lStructSize       = sizeof(OPENFILENAME); 
				OpenFileName.hwndOwner         = hDlg; 
				OpenFileName.lpstrFilter       = "Visual C Project (*.dsw)\0*.dsw\0\0"; 
				OpenFileName.lpstrCustomFilter = NULL; 
				OpenFileName.nMaxCustFilter    = 0;
				OpenFileName.nFilterIndex      = 1; 
				OpenFileName.lpstrFile         = szFile; 
				OpenFileName.nMaxFile          = sizeof(szFile); 
				OpenFileName.lpstrFileTitle    = NULL; 
				OpenFileName.nMaxFileTitle     = 0; 
				OpenFileName.lpstrInitialDir   = NULL; 
				OpenFileName.lpstrTitle        = "Location of project"; 
				OpenFileName.nFileOffset       = 0;
				OpenFileName.nFileExtension    = 0; 
				OpenFileName.lpstrDefExt       = NULL; 
				OpenFileName.Flags             = OFN_HIDEREADONLY | OFN_LONGNAMES; 

				if (GetOpenFileName(&OpenFileName)) 
				{ 
					strcpy( ProjectName, OpenFileName.lpstrFile );
					SetDlgItemText ( hDlg,IDC_BUILDLOCATION, ProjectName );
				}
			}
            break;
    }
    return FALSE;                           
}




//
//
// Main windows entry point - just displays one dialog.
//
//
//
int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
    hInst=hInstance;

	DialogBox(hInst, MAKEINTRESOURCE(IDD_Dialog), NULL, (DLGPROC)Dialog);

    return 0;
}


