/**
 *  File:
 *    OSRSwap.ycp
 *
 *  Module:
 *    YaST OS Repair. Automatic error detection & repair tool for Linux.
 *
 *  Summary:
 *    YaST OS Repair. Automatic error detection & repair tool for Linux.
 *
 *  Author:
 *    Johannes Buchhold <jbuch@suse.de>
 *
 * $Id: OSRSwap.ycp 20509 2004-12-07 15:38:00Z jsuchome $
 */
{
  module "OSRSwap";

  import "OSRExecute";
  import "OSRPopup";

  import "Mode";
  import "Storage";
  import "Partitions";

  textdomain "repair";

  /**
   * Create swap area on a swap partition.
   */
  global define symbol Repair( string target )``{

      //////////////////////////////////////////////////////////////////////////
      //
      //  Repair swap area with mkswap
      //
      //////////////////////////////////////////////////////////////////////////

      // "%1": will be replaced with "/dev/hda" or "/dev/sda" ...
      string error_message = sformat(_("
The partition %1 has the file system ID 130, but
contains no valid swap area. To create
a valid swap area, press \"Repair\".

Press \"Skip\" to avoid repairing.
"), target);

      // helptext 1/2
      string help_text         = _("
<p>A partition with the ID 130 should be 
a Linux swap partition. All swap partitions
need a valid swap area, which is created with
the tool mkswap.</p>
") +

      // helptext 2/2
_("<p>If you know how to use mkswap, 
close YaST and create a new swap area
on the damaged swap partition. Otherwise
close this help dialog and press
\"Repair\" for automatic
repair.</p>
");

      while( true )
      {
	  if (!OSRPopup::Repair(_("Error detected."), error_message, help_text))
	      return `cancel;

	  UI::OpenDialog(`VBox(`VSpacing(1),
			       `Label(_("Creating swap area. Please wait...")),
			       `VSpacing(1)));

	  boolean ret = OSRExecute::Command(.local.bash,"/sbin/mkswap "+target);

	  UI::CloseDialog();
	  if ( ret ) return `ok;
      }
  };

  /**
   *  Get all valid swap-partitions.
   *  @return list The list of valid swap-partitions.
   */
  global define list<string> Partitions() ``{

    list<string> ret = [];
    foreach (string dev, map disk, (map<string,map<string,any> >)Storage::GetTargetMap(), ``{
	if( !disk["is_lvm_vg"]:false && dev != "/dev/md" )
	{
	    list l = filter (map p, disk["partitions"]:[], ``(
		!p["delete"]:false && p["fsid"]:0 == Partitions::fsid_swap)
	    );

	    ret = (list<string>) union (ret, maplist (map p, (list<map<string,any> >)l,
		``(Storage::GetDeviceName (dev,p["nr"]:nil))));
	}
    });

    y2debug ("GetPartitionList ret=%1", ret );
    return ret;
  };


  /**
   *  Returns a list of names (e.g. ["/dev/hda1", "/dev/hdb3"]) of all
   *	partitions that can be mounted to the system as swap out of
   *	the specified list.
   *
   *  @param list partition_list A list of partitions out of which
   *    the "swapable" partitions are to be returned,
   *	e.g. ["/dev/hda1", "/dev/hdb3"] -
   *  @return list The list of partition names that were successfully added
   *	to the system as swap partitions.
   */
  global define list<string> ValidPartitions(list<string> partition_list) ``{

      list<string> swap_possible_list = [];
      list already_swapped_on = Storage::SwappingPartitions();

      foreach(string partition_item, partition_list, ``{

	  if (contains(already_swapped_on, partition_item) || Mode::test ())
	  {
	      // this partition is already swapped on,so it seems to be swapable
	      swap_possible_list = add( swap_possible_list, partition_item );
	  }
	  else
	  {
	      // run /sbin/swapon
	      if ( WFM::Execute (.local.bash, "/sbin/swapon " + partition_item )  == 0)
	      {
		  // run /sbin/swapoff
		  if( WFM::Execute (.local.bash,  "/sbin/swapoff " + partition_item ) == 0 ) {
		      swap_possible_list = add(swap_possible_list, partition_item);
		  }
		  else {
		      y2error("swapoff paritition %1 is not possible",partition_item);
		  }
	      }
	  }
      });
      return swap_possible_list;
  };

}//EOF
