ACC SHELL

Path : /proc/self/root/usr/lib/YaST2/servers_non_y2/
File Upload :
Current File : //proc/self/root/usr/lib/YaST2/servers_non_y2/ag_bootloader

#!/usr/bin/perl

use lib "/usr/lib/YaST2/agents_non_y2";
use ycp;
use strict;

use Bootloader::Library;

my $lib_ref = undef;

my $result;

sub DumpLog {
  foreach my $rec (@{$lib_ref->GetLogRecords ()})
  {
    if ($rec->{"level"} eq "debug")
    {
      y2debug ($rec->{"message"});
    }
    elsif ($rec->{"level"} eq "milestone")
    {
      y2milestone ($rec->{"message"});
    }
    elsif ($rec->{"level"} eq "warning")
    {
      y2warning ($rec->{"message"});
    }
    elsif ($rec->{"level"} eq "error")
    {
      y2error ($rec->{"message"});
    }
    else
    {
      y2error ("Uncomplete log record");
      y2error ($rec->{"message"});
    }
  }
}

sub DumpLogAndReturn {
  my $ret = shift;

  DumpLog ();
  ycp::Return ($ret);
}

while ( <STDIN> )
{
  my ($command, $path, $argument) = ycp::ParseCommand ($_);

  if ($command eq "Read")
  {
    if ($path eq ".settings")
    {
      my $ret = $lib_ref->ReadSettings ();
      if (! $ret)
      {
	DumpLogAndReturn (undef);
      }
      $ret = $lib_ref->GetSettings ();
      DumpLogAndReturn ($ret);
    }
    elsif ($path eq ".fromstrings" && ref ($argument) eq "HASH")
    {
      my %contents = %{$argument};
      my $ret = $lib_ref->SetFilesContents (\%contents);
      if (! $ret)
      {
	DumpLogAndReturn (undef);
      }
      DumpLogAndReturn ($lib_ref->GetSettings ());
    }
    elsif ($path eq ".tostrings" && ref ($argument) eq "HASH")
    {
      $lib_ref->SetSettings ($argument);
      DumpLogAndReturn ($lib_ref->GetFilesContents ());
    }
#    elsif ($path eq ".opttypes")
#    {
#      DumpLogAndReturn ($lib_ref->GetOptionTypes ());
#    }
    else
    {
      y2error ("Incorrect arguments for Read");
      ycp::Return ("false");
    }
  }
  elsif ($command eq "Write")
  {
    if ($path eq ".settings" && ref ($argument) eq "HASH")
    {
      $lib_ref->SetSettings ($argument);
      DumpLogAndReturn ($lib_ref->WriteSettings ()
	? "true"
	: "false");
    }
    else
    {
      y2error ("Incorrect arguments for Write");
      ycp::Return ("false");
    }
  }
  elsif ($command eq "Execute")
  {
    if ($path eq ".agent_init" && ! ref ($argument))
    {
      my $lt = $argument;
      y2milestone ("Initializing for $lt");
      $lib_ref = Bootloader::Library->new ();
      my $ret = $lib_ref->Initialize ($lt);
#      DumpLog ();
      y2milestone ("Initialization status: $ret");
      DumpLogAndReturn ($ret ? "true" : "false");
    }
    elsif ($path eq ".disk_init" && ref ($argument) eq "HASH")
    {
      my %args = %{$argument};
      $lib_ref->DefineMountPoints ($args{"mountpoints"});
      $lib_ref->DefinePartitions ($args{"partitions"});
      $lib_ref->DefineMDArrays ($args{"md_arrays"});
      DumpLogAndReturn ("true");
    }
    elsif ($path eq ".update")
    {
      DumpLogAndReturn ($lib_ref->UpdateBootloader ()
	? "true"
	: "false");
    }
    elsif ($path eq ".initialize")
    {
      DumpLogAndReturn ($lib_ref->InitializeBootloader ()
	? "true"
	: "false");
    }
    else
    {
      y2error ("Incorrect arguments for Execute");
      ycp::Return ("false");
    }
  }
  elsif ($command eq "result")
  {
    exit 0;
  }
  else
  {
    y2error ("Wrong path or arguments");
    ycp::Return ("false");
  }
}

=item
  elsif (ycpCommandIsRead)
  {
    my $ret = $lib_ref->ReadSettings ();
    if (! $ret)
    {
      ycpReturnSkalarAsBoolean (undef);
    }
    ycpReturnHashAsMap ($lib_ref->GetSettings);
  }
=cut

# end

ACC SHELL 2018