# COPYRIGHT_NOTICE_START
# Licensed Materials - Property of IBM
# "Restricted Materials of IBM" -- 5746-SM2
# (C) Copyright IBM Corp. 2007 All Rights Reserved.
# US Government Users Restricted Rights - Use, duplication or disclosure
# restricted by GSA ADP Schedule Contract with IBM Corp.
# COPYRIGHT_NOTICE_END

package Maiken::RO::SLES;
use base 'Maiken::RO';

=head1 NAME

  Maiken::RO::SLES - IBM PowerVM Lx86 installation for SLES

=cut

use strict;
use warnings;

use Cwd ();

sub initialize {
  my ($self) = @_;
  $self->SUPER::initialize;
}

=item B<setup_sw>

As the superclass but run /sbin/SuSEconfig at the end.

=cut

sub setup_sw {
  my ($self) = @_;
  $self->SUPER::setup_sw;
  my $command = '/sbin/SuSEconfig';
  if (-f "$self->{config}{sw_root_path}/$command") {
    $self->log("Running [_1]...", $command);
    my $output = $self->run_translated($command, '2>&1');
    if ($self->{exit}) {
      $self->croak("Error running [_1].", $command);
    }
  }
}

=item B<finalize_sw_installation>

As the superclass but run clean_rc_dirs at the beginning.

=cut

sub finalize_sw_installation {
  my ($self) = @_;
  # Let's clear rcX.d
  $self->clean_rc_dirs;
  $self->SUPER::finalize_sw_installation;
}

=item B<clean_rc_dirs>

Cleans the rcX.d dirs

=cut

sub clean_rc_dirs {
  my ($self) = @_;
  # It is probably safer to disable all services using
  # chkconfig and then - to get rid of the reboot, halt and single
  # scripts - unlink *
  my $command = "/sbin/chkconfig";
  # what services do we have?
  my @service_list = split /\n/, $self->run_translated($command, '-t');
  if( $self->{exit} == 0 ) {
    my @cmd_argv;
    foreach (@service_list) {
      # $1 is everything before the first space
      if (/^(\S+)/) {
	# list of services + off
	push @cmd_argv,$1,"off";
      }
    }
    $self->run_translated($command, @cmd_argv);
    if( $self->{exit} != 0 ) {
      $self->log("WARNING: The services' startup state could not be set.");
    }
    # now, unlink * to delete the scripts that chkconfig
    # did not delete (halt, reboot, etc)
    my $scripts_dir = "$self->{config}{sw_root_path}/etc/init.d";
    for my $i (0..6) {
      unlink glob("\Q$scripts_dir/rc$i.d\E/*");
    }
    unlink glob("\Q$scripts_dir/boot.d\E/*");
    unlink glob("\Q$scripts_dir/rcS.d\E/*");
    # run dependency_checker.pl to delete the dependencies between the rc
    # scripts
    $self->run_command("$self->{config}{qt_root_path}/bin/dependency_checker.pl");
    # if there was a failure here the rc_monitor will correct it later
    $self->enable_services;
  } else {
    $self->log("WARNING: The list of services could not be retrieved.");
  }
}

=item B<check_login_defs>

See superclass. The RPM is pwdutils.

=cut

sub check_login_defs {
  my $self = shift;
  my $rpm = shift;
  if (defined $rpm && ($rpm =~ /pwdutils/)) {
    $self->modify_login_defs;
  }
}

1;
