/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Verbosio XML Editor.
 *
 * The Initial Developer of the Original Code is
 * Alexander J. Vincent <ajvincent@gmail.com>.
 * Portions created by the Initial Developer are Copyright (C) 2007
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

/* This is code Verbosio uses to launch test instances of Verbosio in a
   secondary profile and with a secondary Verbosio objdir.  Only the default
   objdir has this code.  Thus, developers can use the default objdir to edit
   Verbosio's files and extend Verbosio.  This is how Verbosio eats its own
   dogfood and becomes a nice, big Siberian husky.  You provide the water dish.
 */

/* XXX ajvincent The toolkit profile service is NOT included in XPCOM after the
   profile has been selected; it's explicitly excluded.  So we cannot do any
   realistic, cross-platform checks to ensure a needed profile exists and is
   unlocked.
 */

function editVerbosioApp(aProfileName) {
  if (gDocumentPack) {
    throw new Error("We don't support editing Verbosio code in a new window.");
  }

  var appURI = "resource://app/";
  var ioService = Components.classes["@mozilla.org/network/io-service;1"]
                            .getService(C_i.nsIIOService);
  var uri = ioService.newURI(appURI, null, null)
                     .QueryInterface(C_i.nsIFileURL);
  var localFile = uri.file.QueryInterface(C_i.nsILocalFile);
  assert(localFile.leafName == "default", "Not the default Verbosio app?");
  assert(localFile.isDirectory(), "Not a directory?");
  localFile = localFile.parent;
  localFile.append(aProfileName);
  if (!localFile.exists()) {
    throw new Error(aProfileName + " directory doesn't exist?");
  }
  if (!localFile.isDirectory()) {
    throw new Error("Not a directory?");
  }

  // Confirm the application exists.
  localFile.append("application.ini");
  if (!localFile.exists()) {
    throw new Error("application.ini doesn't exist?");
  }

  verbosio.openDocumentPack(localFile.path);

  // Change the command.
  var localeBundle = document.getElementById("verbosioAppLocale");
  var itemLabel = localeBundle.getString("verbosio.launch.testing");
  var menuitem  = document.getElementById("app-item-launch-" + aProfileName);
  menuitem.setAttribute("command", "app-command-launch-" + aProfileName);
  menuitem.setAttribute("label", itemLabel);
}

function editVerbosioDotXUL() {
  editVerbosioApp('testing');
  verbosio.openDocument("chrome://verbosio/content/verbosio.xul");
}

function launchVerbosioApp(aProfileName) {
  var appBundle = document.getElementById("verbosioAppProperties");

  try {
    var en = Components.classes["@mozilla.org/process/environment;1"]
                       .getService(Components.interfaces.nsIEnvironment);
    var noRemoteExists = en.exists("MOZ_NO_REMOTE");
    var noRemote = "";
    if (noRemoteExists) {
      noRemote = en.get("MOZ_NO_REMOTE");
    }

    en.set("MOZ_NO_REMOTE", "1");

    var exe = Components.classes["@mozilla.org/file/local;1"]
                        .createInstance(Components.interfaces.nsILocalFile);

    var binPath = appBundle.getString("BIN_PATH");

    var appPath = appBundle.getString("app." + aProfileName);
    var slash = (appPath.indexOf("/") == -1) ? "\\" : "/";
    appPath += slash + "run_verbosio.sh";
    binPath += slash + "bash";
    if (slash == "\\") {
      binPath += ".exe";
    }

    exe.initWithPath(binPath);
    var proc = Components.classes['@mozilla.org/process/util;1']
                         .createInstance(Components.interfaces.nsIProcess);
    dump(binPath + "\n");
    proc.init(exe.nsIFile);
    var args = [appPath];
    proc.run(true, args, args.length);

    en.set("MOZ_NO_REMOTE", noRemote);
  } catch (e) {
    alert("Unable to launch XULRunner: " + e);
  }
}

const xeITestEngine = Components.interfaces.xeITestEngine;
const xeTestEngine = Components.classes["@verbosio.mozdev.org/test-engine;1"]
                               .getService(xeITestEngine);
