//------------------------------------------------------------------------------
// emInstallInfo.cpp
//
// Copyright (C) 2006-2008 Oliver Hamann.
//
// Homepage: http://eaglemode.sourceforge.net/
//
// This program is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License version 3 as published by the
// Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License version 3 for
// more details.
//
// You should have received a copy of the GNU General Public License version 3
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//------------------------------------------------------------------------------

#if defined(_WIN32)
#	include <windows.h>
#	include <shlobj.h>
#endif
#include <emCore/emInstallInfo.h>


static void emInitBaseInstallPaths(emString basePaths[EM_NUMBER_OF_IDTS])
{
#if defined(_WIN32)
	char buf[MAX_PATH+64];
	DWORD d;
	HRESULT r;
	LPMALLOC pMalloc;
	LPITEMIDLIST pidl;
	BOOL b;
#endif
	emString prefix;
	const char * p;

	p=getenv("EM_DIR");
	if (!p) emFatalError("Environment variable EM_DIR not set.");
	prefix=emGetAbsolutePath(p);

	basePaths[EM_IDT_BIN]=emGetChildPath(prefix,"bin");

	basePaths[EM_IDT_INCLUDE]=emGetChildPath(prefix,"include");

	basePaths[EM_IDT_LIB]=emGetChildPath(prefix,"lib");

	basePaths[EM_IDT_HTML_DOC]=emGetChildPath(emGetChildPath(prefix,"doc"),"html");

	basePaths[EM_IDT_PS_DOC]=emGetChildPath(emGetChildPath(prefix,"doc"),"ps");

	basePaths[EM_IDT_HOST_CONFIG]=emGetChildPath(prefix,"etc");

	basePaths[EM_IDT_RES]=emGetChildPath(prefix,"res");

#if defined(_WIN32)

	r=SHGetMalloc(&pMalloc);
	if (r!=NOERROR) emFatalError("SHGetMalloc failed.");

	r=SHGetSpecialFolderLocation(NULL,CSIDL_PERSONAL,&pidl);
	if (r!=NOERROR) emFatalError("SHGetSpecialFolderLocation failed.");
	b=SHGetPathFromIDList(pidl,buf);
	if (!b) emFatalError("SHGetPathFromIDList failed.");
	pMalloc->Free(pidl);
	basePaths[EM_IDT_HOME]=buf;

	r=SHGetSpecialFolderLocation(NULL,CSIDL_APPDATA,&pidl);
	if (r!=NOERROR) emFatalError("SHGetSpecialFolderLocation failed.");
	b=SHGetPathFromIDList(pidl,buf);
	if (!b) emFatalError("SHGetPathFromIDList failed.");
	pMalloc->Free(pidl);
	basePaths[EM_IDT_USER_CONFIG]=emGetChildPath(buf,"eaglemode");

	d=GetTempPath(sizeof(buf),buf);
	if (d==0 || d>=sizeof(buf)) {
		emFatalError("GetTempPath failed.");
	}
	buf[d-1]=0;
	basePaths[EM_IDT_TMP]=buf;

#else
	p=getenv("HOME");
	if (!p) emFatalError("Environment variable HOME not set.");

	basePaths[EM_IDT_HOME]=p;

	basePaths[EM_IDT_USER_CONFIG]=emGetChildPath(p,".eaglemode");

	basePaths[EM_IDT_TMP]="/tmp";
#endif
}


emString emGetInstallPath(
	emInstallDirType idt, const char * prj, const char * subPath
)
{
	static emString basePaths[EM_NUMBER_OF_IDTS]; //??? not thread-reentrant.
	static bool basePathsInitialized=false;
	emString str;

	if (!basePathsInitialized) {
		emInitBaseInstallPaths(basePaths);
		basePathsInitialized=true;
	}

	if (!prj || !*prj) {
		emFatalError("emGetInstallPath: Illegal prj argument.");
	}

	switch (idt) {
		case EM_IDT_BIN:
		case EM_IDT_LIB:
		case EM_IDT_HTML_DOC:
		case EM_IDT_PS_DOC:
		case EM_IDT_TMP:
		case EM_IDT_HOME:
			str=basePaths[idt];
			break;
		case EM_IDT_INCLUDE:
		case EM_IDT_USER_CONFIG:
		case EM_IDT_HOST_CONFIG:
		case EM_IDT_RES:
			str=emGetChildPath(basePaths[idt],prj);
			break;
		default:
			emFatalError("emGetInstallPath: Illegal idt argument.");
	}

	if (subPath && *subPath) {
		str=emGetChildPath(str,subPath);
	}

	return str;
}
