/**
 *  File:
 *	OSRCommon.ycp
 *
 *  Module:
 *	repair
 *
 *  Summary:
 *	Common functions and variables, used by other OSR modules
 *
 *  Author:
 *	Jiri Suchomel <jsuchome@suse.de>
 *
 * $Id: OSRCommon.ycp 20450 2004-12-01 11:55:31Z jsuchome $
 */
{

/**
 * Variables and functions in these module were moved from other
 * OSR* modules to break their cyclic dependencies.
 *
 * This module should not import any other OSR module
 */

module "OSRCommon";

textdomain "repair";

/**
 * Functions that could be used as a default in lookups for functin pointers
 * @return false
 */
global define boolean False () ``{
    return false;
}

/**
 * @return `error
 */
global define symbol SymbolError () ``{
    y2error ("method not found");
    return `error;
}

/**
 * @return ""
 */
global define string EmptyString () ``{
    return "";
}

/**
 * @return $[]
 */
global define map EmptyMap () ``{
    return $[];
}

/**
 * @return []
 */
global define list EmptyList () ``{
    return [];
}


// ---------------------------------------------------------------------
// from OSRDirect.ycp:

/**
 * The name of the current direct repair method.
 */
global string current_direct_name	= "";


// ---------------------------------------------------------------------
// from OSRRepair.ycp:

/**
 * The name of the repair and rescue tool.
 */
global string tool_name		= _("YaST System Repair");


// ---------------------------------------------------------------------
// from OSR.ycp:

/**
 * The name of the current module during detection.
 * e.g.: osr_module_partition
 */
global string current_module_name	= "";


/**
 * ["init", "mbr_check", "swap_check", "fs_check", "fstab_check"]
 */
global list<string> detect_group_list	= [];

/**
 * The current detection map during detection.
 *
 * $[
 "group":"init",
 "method": ``OSRInitFloppy(),
 "progress":10,
 "provides":["repair_target"],
 "requires":[],
 "summary":"Init target system"
 ]
 */
global map current_detect_map	        = $[];

/**
 * $[	"fs_check" : $[
 *		"help"	: "Mounting a filesystem is not possible",
 *		"text"	: "Check filesystem"
 *	],
 *	"fstab_check" : $[
 *		"help"	: "Mounting a filesystem is not possible",
 *		"text"	: "Check fstab entries"
 *	],
 *	"init" : $[
 *		"help"	: "",
 *		"mode"	: "forall",
 *		"text"	: "Init repair system"
 *	],
 *	"mbr_check" : $[
 *		"help"	: "If you have installed a other os after inst. Linux",
 *		"text"	: "Check master boot record"
 *	],
 *	........
 * ]
 */
global map global_entries		= $[];

  /**
   * The global_provides map contains entries created during the system scan.
   * The different detection methods access to the global_provides map to get
   * informations from former detection methods.
   * $["fs_module_loaded":true,
       "has_floppy":true,
       "just_mounted":[$["mountpoint":"/mnt", "partition":"/dev/hda3"]],
       "linux_partition_list":["/dev/hda1", "/dev/hda3", "/dev/hda6"],
       "mount_possible_list":["/dev/hda1", "/dev/hda3", "/dev/hda6"],
       "mounted_partitions":[$["file":"/mnt", "freq":0, "mntops":"rw", "passno":0, "spec":"/dev/hda3", "vfstype":"reiserfs"],..,],
       "repair_target":"",
       "root_mounted":true,
       "root_mountpoint":"/mnt",
       "root_partition":"/dev/hda3",
       "root_partition_type":"reiserfs",
       "swap_partition_list":["/dev/hda2", "/dev/hda5"],
       "swapable_partitions":["/dev/hda2", "/dev/hda5"],
       "valid_fstab":true,
       "valid_root_partitions":["/dev/hda3"],
       "valid_swap_partitions":["/dev/hda2", "/dev/hda5"],
       "valid_target":true, "valid_target_list":["/dev/hda"]]
   */
  map<string,any> global_provides			= $[];


  /**
   *  Stores the given list with the specified name into the global_provides map
   *  in the osr_map.
   *
   *  API function,
   *
   *  @param string name The name of the provided value.
   *  @param list value The provided value.
   *  @return boolean True.
   */
  global define boolean ProvideList(string name, list value) ``{
      global_provides = add (global_provides, name, value );
      return true;
  };

  /**
   *  Stores the given map with the specified name into the global_provides map
   *  in the osr_map.
   *
   *  API function,
   *
   *  @param string name The name of the provided value.
   *  @param map value The provided value.
   *  @return boolean True.
   */
  global define boolean ProvideMap(string name, map value) ``{
      global_provides	= add(global_provides, name, value );
      return true;
  };

  /**
   *  Stores the given string with the specified name into the global_provides map
   *  in the osr_map.
   *
   *  API function,
   *
   *  @param string name The name of the provided value.
   *  @param string value The provided value.
   *  @return boolean True.
   */
  global define boolean ProvideString(string name, string value) ``{
      global_provides	= add(global_provides, name,value ); // provides_map  );
      return true;
  };

  /**
   *  Stores the given path with the specified name into the global_provides map
   *  in the osr_map.
   *
   *  API function,
   *
   *  @param string name The name of the provided value.
   *  @param path value The provided value.
   *  @return boolean True.
   */
  global define boolean ProvidePath(string name, path value) ``{
      global_provides	= add(global_provides, name, value ); //provides_map  );
      return true;
  };

  /**
   *  Stores the given boolean value with the specified name into the global_provides map
   *  in the osr_map.
   *
   *  API function,
   *
   *  @param string name The name of the provided value.
   *  @param boolean value The provided value.
   *  @return boolean True.
   */
  global define boolean ProvideBoolean(string name, boolean value) ``{
      global_provides	= add(global_provides, name,value); // provides_map  );
      return true;
  };

  /**
   *  Returns the required string from the global_provides map
   *  in the osr_map.
   *
   *  API function,
   *
   *  @param string name The name of the required value.
   *  @return string value The required string, "" as default.
   */
  global define string RequireString(string name) ``{

      if ( ! haskey( global_provides, name ) )
      {
	  y2warning ("The required value \"%1\" is not yet set.", name);
      }

      return global_provides[ name ]:"";
  };

  /**
   *  Returns the required list from the global_provides map
   *  in the osr_map.
   *
   *  API function,
   *
   *  @param string name The name of the required value.
   *  @return string The required list, [] as default.
   */
  global define list RequireList(string name) ``{

      if (! haskey (global_provides, name))
      {
	  y2warning ("The required value \"%1\" is not yet set.", name);
      }

      return global_provides[name]:[];
  };

  /**
   *  Returns the required map from the global_provides map
   *  in the osr_map.
   *
   *  API function,
   *
   *  @param string name The name of the required value.
   *  @return map The required map, $[] as default.
   */
  global define map RequireMap(string name) ``{

      if ( ! haskey ( global_provides, name ))
      {
	  y2warning ("The required value \"%1\" is not yet set.", name);
      }

      return global_provides[ name]:$[];
  };

  /**
   *  Returns the required path from the global_provides map
   *  in the osr_map.
   *
   *  API function,
   *
   *  @param string name The name of the required value.
   *  @return path value The required path, . as default.
   */
  global define path RequirePath(string name) ``{

      if ( ! haskey( global_provides , name ))
      {
	  y2warning ("The required value \"%1\" is not yet set.", name);
      }

      return global_provides[name]:.;
  };

  /**
   *  Returns the required boolean value from the global_provides map
   *  in the osr_map.
   *
   *  API function,
   *
   *  @param string name The name of the required value.
   *  @return boolean value The required boolean value, false as default.
   */
  global define boolean RequireBoolean(string name) ``{

      if (! haskey ( global_provides, name ))
      {
	  y2warning ("The required value \"%1\" is not yet set.", name);
      }

      return global_provides[ name]:false;
  };



  /**
   * Reset variables moved from OSR.ycp
   */
  global define void Reset () ``{

      global_provides	= $[];

      current_module_name	= "";
      current_detect_map	= $[];

      global_entries		= $[];
      detect_group_list	= [];
  }

  /**
   * Set the new value of global_provides map
   */
  global define void SetGlobalProvides (map new) ``{

      global_provides	= eval ((map<string,any>) new);
  }

  /**
   * Set the new value of global_provides map
   */
  global define map<string,any> GlobalProvides () ``{

      return global_provides;
  }

/**
 * Get group data.
 */
global define map GetGroupMap (string key) ``{

    return global_entries[key]:$[];
}

  /**
   *  Checks if the first specified list (of strings) is a subset of
   *  the second one. The sequence of entries in the lists is nonrelevant.
   *
   *  For internal use only.
   *  See the testsuite.
   *
   *  @example OSRIsSubSet([], ["a", "b"]) -> true;
   *  @example OSRIsSubSet(["a"], ["a", "b"]) -> true;
   *  @example OSRIsSubSet(["c"], ["a", "b"]) -> false;
   *  @example OSRIsSubSet(["a", "b", "c"], ["a", "b"]) -> false;
   *  @param list The first list.
   *  @param list The second list.
   *  @return True if all elements of l1 are contained in l2 or if l1 is empty.
   */
  global define boolean IsSubSet (list<string> l1, list<string> l2) {

      integer index = 0;

      // remove duplicate entries and sort the lists
      l1 = toset(l1);
      l2 = toset(l2);

      // check special cases
      if ((size(l1) < 0) || (size(l2) < 0))   return false;

      else if (size(l1) > size(l2))  return false;

      else if ((size(l1) == 0)) return true;

      else if (size(l1) <= size(l2))
      {
	  // this is the main subset-checker
	  while ((index >= 0) && (index < size(l1)))
	  {
	      if (!contains (l2, l1[index]:""))
	      {
		  return false;
	      }
	      index = index + 1;
	  }
	  return true;
      }
      else
      {
	  return false;
      }
  };

}//EOF
