/* -*- Mode: Javascript; tab-width: 2; c-basic-offset: 2; -*-
 * 
 *
 * 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 jslib team code.
 * The Initial Developer of the Original Code is jslib team.
 *
 * Portions created by Mozdev Group, Inc. are
 * Copyright (C) 2003 Mozdev Group, Inc.  All
 * Rights Reserved.
 *
 * Original Author: 
 * Contributor(s):
 *
 *  $Id: install.js.jar.in,v 1.2 2005/12/18 05:42:19 ajvincent Exp $
 *  Last Edited by: $Author: ajvincent $
 *  $Revision: 1.2 $
 *  $Date: 2005/12/18 05:42:19 $
 *
 */

const G_VER             = "0.5.2";
const G_MSG             = "xulwidgets v"+G_VER;
const G_NAME            = "xulwidgets";
const G_CHROME          = "Chrome";
const G_USER            = "Current User";
const G_CONTENT         = "content/";
const G_SKIN            = "skin/";
const G_LOCALE          = "locale/en-US/";

const G_JAR_FILE        = "xulwidgets.jar";
const G_CHROME_DIR      = getFolder(G_CHROME);
const G_USER_CHROME_DIR = getFolder(G_USER, "chrome");
const G_PROGRAM_DIR     = getFolder("Program");

const COMNT_1           = "Global Chrome Install for xulwidgets as a jar . . . \n\n";
const COMNT_2           = "xulwidgets jar";
const KILO_BYTES        = 60; // Extracted xpi archive size - 60K
const UP_TO_DATE        = "xulwidgets v,"+G_VER+" [UP TO DATE] - no installation needed"

const CONFIRM_MSG       =   "xulwidgets has failed to install globally on your system. "
                          + "This is most likely due to you not having the required "
                          + "system permissions to do so.\n\n"
                          + "Would you like to install xulwidgets in your "
                          + "browser user profile directory instead?\n\n"
                          + "This means that only the current profile "
                          + "will be able to use xulwidgets.\n\n"
                          + "If you wish to have a global install of xulwidgets, you can cancel "
                          + "the install here and login as system administrator and retry "
                          + "the install from there. ";

logComment(COMNT_1);

var err = initInstall(G_MSG, G_NAME, G_VER);
log("Init Install", err);

if (verifyDiskSpace(G_PROGRAM_DIR, KILO_BYTES))
{
  var globalJar = getFolder(G_CHROME_DIR, G_JAR_FILE);
  var userJar = getFolder(G_USER_CHROME_DIR, G_JAR_FILE);

  if (File.exists(globalJar))
      upgradeGlobal();
  else if (File.exists(userJar))
    upgradeUser();
  else if (addGlobal() != SUCCESS)
    addUser();

  err = getLastError();

  if (err == SUCCESS)
    performInstall();
  else
    cancelInstall(err);
} else {
  cancelInstall(INSUFFICIENT_DISK_SPACE);
}

// local functions
function log (aMsg, aRes) 
{ 
  var rv = aRes;
  if (rv == 0)
    rv = "[SUCCESS]";
    
  logComment(aMsg+": "+rv); 
}

// this function verifies disk space in kilobytes
function verifyDiskSpace(aDirPath, aSpaceRequired)
{
  var spaceAvailable;
  var rv = true;

  // Get the available disk space on the given path
  spaceAvailable = fileGetDiskSpaceAvailable(aDirPath);

  // Convert the available disk space into kilobytes
  spaceAvailable = parseInt(spaceAvailable / 1024);

  // do the verification
  if (spaceAvailable < aSpaceRequired) {
    logComment("Insufficient disk space: " + aDirPath);
    logComment("  required : " + spaceRequired + " K");
    logComment("  available: " + spaceAvailable + " K");
    rv = false;
  }

  return rv;
}

function checkVersion ()
{
  var ver = new String(InstallTrigger.getVersion(G_NAME));

  // strip off build info
  ver = ver.substring(0, ver.lastIndexOf("."));

  return (G_VER >= ver);
}

function upgradeGlobal ()
{
  if (checkVersion())
    addGlobal();
  else
    logComment(UP_TO_DATE);
}

function upgradeUser ()
{
  if (checkVersion())
    addUser();
  else
    logComment(UP_TO_DATE);
}

function addGlobal ()
{
  var rv = addDirectory(COMNT_2, "chrome",  G_CHROME_DIR, "");
  log("addDirectory chrome", rv);

  if (rv == 0) {
    // register chrome in global chrome
    var jarFile = getFolder(G_CHROME_DIR, G_JAR_FILE);

    rv = registerChrome(CONTENT, jarFile, G_CONTENT);
    log("register content", rv);

    rv = registerChrome(SKIN, jarFile, G_SKIN);
    log("register skin", rv);

    rv = registerChrome(LOCALE, jarFile, G_LOCALE);
    log("register locale: ", rv);

  }

  return rv;
}

function addUser ()
{
  // cancel previous install attempt
  cancelInstall(ACCESS_DENIED);

  var rv;
  if (confirm(CONFIRM_MSG)) {
    // reinitialize the install
    rv = initInstall(G_MSG, G_NAME, G_VER);
    log("initInstall reinitialized", rv);

    rv = addDirectory(COMNT_2, "chrome",  G_USER_CHROME_DIR, "");
    log("addFile", rv);

    if (rv == 0) {
      var jarFile = getFolder(G_USER_CHROME_DIR, G_JAR_FILE);

      // register chrome in user home
      rv = registerChrome(CONTENT|PROFILE_CHROME, jarFile, G_CONTENT);
      log("content", rv);
      rv = registerChrome(SKIN|PROFILE_CHROME, jarFile,  G_SKIN);
      log("skin", rv);
      rv = registerChrome(LOCALE|PROFILE_CHROME, jarFile, G_LOCALE);
      log("locale", rv);
    }
  } else {
    rv = USER_CANCELLED;
  }

  return rv;
}
