const APP_DISPLAY_NAME = "ColorZilla";
const APP_VERSION = "1.0";
const APP_SHORT_NAME = "colorzilla";
const APP_AUTHOR = "iosart";
const APP_PACKAGE = "/" + APP_AUTHOR + "/" + APP_SHORT_NAME;
const APP_JAR_FILE = APP_SHORT_NAME + ".jar";
const BLD_ID_MOZILLA_MIN = 20040101; // 1.6
const APP_WHERE_MESSAGE = "Install " + APP_DISPLAY_NAME +
				" to your profile folder (OK) or to your program folder (Cancel) ?";
const APP_BUILD_TOO_OLD = APP_DISPLAY_NAME + " " + APP_VERSION 	+
				" must be installed to builds released after " +
				BLD_ID_MOZILLA_MIN + " !";

const APP_CONTENT_FOLDER = "content/";
const APP_LOCALE_FOLDER	 = "locale/";
const APP_LOCALES 	 = ["cs-CZ", "de-DE", "fr-FR", "ja-JP", "pt-BR", "sk-SK", "pl-PL", "tr-TR", "ca-AD", "da-DK", "de-CH", "es-AR", "es-ES", "fi-FI", "hu-HU", "it-IT", "nl-NL", "pt-PT", "ro-RO", "ru-RU", "uk-UA", "zh-CN", "zh-TW"];
const APP_SKIN_FOLDER	 = "skin/classic/";

const APP_COMPONENTS_FOLDER	= "components/";
const XPT_FILE_NAME 		= "ColorZilla.xpt";
const DLL_FILE_NAME	 	= "ColorZilla.dll";

function verifyNoError(errorMessage) {
	err = getLastError();

	if (err != SUCCESS) {
		alert(errorMessage + " (" + err + ")");
        	cancelInstall(err);
	}

	return err;
}

function getPlatform() {
	var platformStr;
	if ('platform' in Install) {
		platformStr = new String(Install.platform);
		if (platformStr.search(/Windows/gi) != -1) {
			return "windows";
		}

		if (!platformStr.search(/Linux/gi) != -1) {
			return "linux";
		}

		if (!platformStr.search(/Mac/gi) != -1) {
			return "mac";
		}
    	}
	return '';
}

function installPalettes() {
	// copy the included palettes to the 
	// Profile/colorzilla/palettes/default folder
	// the extension itself will move the files from 
	// this folder into Profile/colorzilla/palettes
	// and remove the installedPalettes

	initInstall("palette-install", "/colorzilla-palettes-install", "0.0"); 

	var target = getFolder("Profile", "colorzilla"); 	
	if (!File.exists(target)) {
		File.dirCreate(target);
	}

	var target = getFolder(target, "palettes"); 	
	if (!File.exists(target)) {
		File.dirCreate(target);
	}

	addDirectory("palettes", APP_VERSION, "defaults/palettes", target, 'default');

	performInstall(); 
}

function platformSpecificInstall() {
	var platform = getPlatform();

	var componentsDir = getFolder("Components"); 
	
	if (platform == 'windows') {
		addFile(DLL_FILE_NAME, APP_VERSION, APP_COMPONENTS_FOLDER + DLL_FILE_NAME, componentsDir, "");  
		if (verifyNoError("Couldn't install " + DLL_FILE_NAME) != SUCCESS) { return false; }
		return true;
	}

        var platformSpecificDllFileName = DLL_FILE_NAME + "." + platform;
	addFile(DLL_FILE_NAME, APP_VERSION, APP_COMPONENTS_FOLDER + platformSpecificDllFileName, componentsDir, "");  
	if (verifyNoError("Couldn't install " + platformSpecificDllFileName) != SUCCESS) { return false; }

	var file = getFolder("Components", platformSpecificDllFileName); 	
	if (verifyNoError("Couldn't get platform specific file: " + platformSpecificDllFileName) != SUCCESS) { return false; }

	if (!File.exists(file)) {
		alert("Couldn't find platform specific file: " + platformSpecificDllFileName);
        	cancelInstall(err);
		return false;
	}

	// make the platform specific file the main dll:
	File.rename(file, DLL_FILE_NAME);
	if (verifyNoError("Couldn't rename platform specific file: " + platformSpecificDllFileName +
				" to " + DLL_FILE_NAME) != SUCCESS) { return false; }

	return true;
}

function preinstallCleanup() {
	initInstall("pre-install-cleanup", "/colorzilla-cleanup", "0.0"); 

	// remove old files if exist

	var oldFile = getFolder("Components", XPT_FILE_NAME); 	
	File.remove(oldFile);

	oldFile = getFolder("Components", DLL_FILE_NAME); 	
	File.remove(oldFile);

	performInstall(); 
}

function perfromInstallation() {
	var err;

	// verify that the Mozilla build is not too old
	var currBuild = new String(buildID).substr(0, 8) - 0;
	if (currBuild && currBuild < BLD_ID_MOZILLA_MIN) {
	   alert(APP_BUILD_TOO_OLD);
	   cancelInstall(INSTALL_NOT_STARTED);
	   return;
	}

        initInstall(APP_DISPLAY_NAME, APP_SHORT_NAME, APP_VERSION);
	if (verifyNoError("Couldn't init the installation process") != SUCCESS) { return; }

	// install chrome:
	var where, chromef;
	var inProfile = confirm(APP_WHERE_MESSAGE);
	if (inProfile) {
		chromef = getFolder("Profile", "chrome");
		where = PROFILE_CHROME;
   	} else {
		chromef = getFolder("chrome");
		where = DELAYED_CHROME;
   	}

    	addFile(APP_PACKAGE, APP_VERSION, "chrome/" + APP_JAR_FILE, chromef, null);
	err = verifyNoError("Failed to create " + APP_JAR_FILE + "\n" +
		            "You probably don't have appropriate permissions \n" +
		            "(write access to " + (inProfile ? "profile" : "browser") + "/chrome directory). \n"); 
	if (err != SUCCESS) { return; }

	var jar = getFolder(chromef, APP_JAR_FILE);

	registerChrome(CONTENT | where, jar, APP_CONTENT_FOLDER);
	if (verifyNoError("Couldn't register " + APP_CONTENT_FOLDER + " folder") != SUCCESS) { return; }

    // register the default (fallback) locale:
	registerChrome(LOCALE  | where, jar, APP_CONTENT_FOLDER + 'locale/en-US/');
  	for (var iLocale=0; iLocale < APP_LOCALES.length; iLocale++) {
	 	var localeFolder = APP_LOCALE_FOLDER + APP_LOCALES[iLocale] + "/";
		registerChrome(LOCALE  | where, jar, localeFolder);
		if (verifyNoError("Couldn't register " + localeFolder + " folder") != SUCCESS) { return; }
  	}

	registerChrome(SKIN    | where, jar, APP_SKIN_FOLDER);
	if (verifyNoError("Couldn't register " + APP_SKIN_FOLDER + " folder") != SUCCESS) { return; }

	// install XPCOM components:
	var componentsDir = getFolder("Components"); 

	addFile(XPT_FILE_NAME, APP_VERSION, APP_COMPONENTS_FOLDER + XPT_FILE_NAME, componentsDir, "");  
	if (verifyNoError("Couldn't install " + XPT_FILE_NAME) != SUCCESS) { return; }

	if (!platformSpecificInstall()) {
		return;
	}

      	performInstall();

	err = getLastError();
	if (err == SUCCESS) {
            alert(APP_DISPLAY_NAME + " version " + APP_VERSION + " has been successfully installed to your " +
	               (inProfile?"profile":"program") + " folder.\n\n" +
		       "Please, restart the browser before continuing!");
      	} else {
		if (err == REBOOT_NEEDED) {
			alert("Install failed because a previous version of " + 
					APP_DISPLAY_NAME + " has not been uninstalled.");
		} else {
			alert("Install failed! Error code:" + err);
		}
		cancelInstall(err);
      	}
}


preinstallCleanup();

// install the palettes into a temporary folder
installPalettes();

// Call the main installation function
perfromInstallation();
