/*
 * Copyright (c) 2005-2006 Tom N Harris.
 *
 *     This program is free software; you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation; either version 2 of the License, or
 *     (at your option) any later version.
 *
 *     A copy of the GNU General Public License can be found at
 *     http://www.gnu.org/
 *
 * Written by Tom N Harris <telliamed@whoopdedo.org>
 *
 */

#include "win32.h"
#include "cString.h"
#include "Registry.h"

#include <shlobj.h>
#include <shlwapi.h>

#include "dark.h"

using namespace Setup;

static TCHAR const* const _base_dirs[] = { 
	TEXT("games\\"), 
	TEXT(""), 
	TEXT("thief\\"), 
	TEXT("juex\\"), 
	TEXT("juegos\\"), 
	TEXT("pelit\\"), 
	TEXT("spiel\\"), 
	TEXT("spiele\\"), 
	TEXT("spil\\"),
	TEXT("spel\\"), 
	TEXT("spelen\\"), 
	TEXT("giochi\\"), 
	NULL
};
static TCHAR const* const _t1_dirs[] = { 
	TEXT("Thief\\"),
	TEXT("ThiefG\\"),
	TEXT("TG\\"),
	TEXT("Thief - The Dark Project\\"),
	TEXT("Thief - The Dark Project Gold Edition\\"),
	NULL
};
static TCHAR const* const _t2_dirs[] = { 
	TEXT("Thief2\\"),
	TEXT("Thief 2\\"),
	TEXT("Thief II\\"),
	TEXT("T2\\"),
	TEXT("Thief - The Metal Age\\"),
	TEXT("Thief II - The Metal Age\\"),
	NULL
};
static TCHAR const* const _ss2_dirs[] = { 
	TEXT("Shock2\\"),
	TEXT("SShock2\\"),
	NULL
};

static TCHAR _drive_letters[128] = TEXT("?");


static bool 
_LocateInReg(LPTSTR path, LPCTSTR exe)
{
	Registry apppaths = Registry::getKey(HKEY_LOCAL_MACHINE, 
			    TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths"),
			    KEY_READ);
	if (!apppaths)
		return false;
	Registry appkey = apppaths.getKey(exe, KEY_READ);
	if (!appkey)
		return false;
	int szlen = appkey.getValueString(TEXT("path"), path, MAX_PATH);
	return szlen <= MAX_PATH;
}

static bool 
_LocatePath(LPTSTR path, LPCTSTR const* dir, LPCTSTR exe)
{
	if (! ::PathFileExists(path))
		return false;
	LPTSTR base = path + lstrlen(path);
	while (*dir)
	{
		::PathCombine(base, *dir, exe);
		if (::PathFileExists(path))
		{
			::PathRemoveFileSpec(path);
			return true;
		}
		*base = TEXT('\0');
		++dir;
	}
	return false;
}

static String 
_LocateDir(LPCTSTR const* game_dirs, LPCTSTR game_exe)
{
	TCHAR path[MAX_PATH];

	if (_LocateInReg(path, game_exe))
	{
		return String(path);
	}

	LPITEMIDLIST pidl;
	if (NOERROR == ::SHGetSpecialFolderLocation(NULL, CSIDL_PROGRAM_FILES, &pidl))
	{
		::SHGetPathFromIDList(pidl, path);
		::PathAddBackslash(path);
		if (_LocatePath(path, game_dirs, game_exe))
		{
			return String(path);
		}
	}

	if (_drive_letters[0] == TEXT('?'))
	{
		::GetLogicalDriveStrings(sizeof(_drive_letters)-1, _drive_letters);
	}

	LPCTSTR drive = _drive_letters;
	while (*drive)
	{
		if (::GetDriveType(drive) == DRIVE_FIXED)
		{
			for (LPCTSTR const* dir = _base_dirs; *dir; ++dir)
			{
				::PathCombine(path, drive, *dir);
				if (_LocatePath(path, game_dirs, game_exe))
				{
					return String(path);
				}
			}
		}
		drive += lstrlen(drive) + 1;
	}

	return String();
}

#if 0
static String
_LocateDeadlyShadows(void)
{
	Registry TDSkey = Registry::getKey(HKEY_LOCAL_MACHINE,
			    TEXT("SOFTWARE\\Ion Storm\\Thief - Deadly Shadows"), 
			    KEY_READ);
	if (TDSkey)
	{
		return tdskey.getValueString(TEXT("ION_ROOT"));
	}

	return String();
}
#endif

String 
Setup::LocateGameDir(int game)
{
	switch (game)
	{
	case DARKGAME_T1:
		return _LocateDir(_t1_dirs, TEXT("thief.exe"));
	case DARKGAME_T2:
		return _LocateDir(_t2_dirs, TEXT("thief2.exe"));
	case DARKGAME_SS2:
		return _LocateDir(_ss2_dirs, TEXT("shock2.exe"));
	/*
	case DARKGAME_T3:
		return _LocateDeadlyShadows();
	*/
	}
	return String();
}

