#!/usr/bin/perl
# 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


=head1 NAME

installer.pl - Install/upgrade/uninstall PowerVM Lx86 software

=head1 SYNOPSIS

  ./installer.pl
  ./installer.pl -i Lx86 -i x86world -d RHEL4U3AS_MIN -m /path/to/media
  ./installer.pl --help

=head1 OPTIONS

=over 4

=item --install, -i

Install the specified component, noninteractively. May be specified
multiple times.

Valid components are:

  Lx86
  x86world (must be specified in conjunction with --media or --archive)

=item --uninstall, -n

Uninstall the specified component, noninteractively. May be specified multiple times.

Valid components are:

  Lx86
  x86world

=item --show

Display which products are currently installed.

=item --config, -c

Use the specified config file, rather than the default,
/etc/opt/powervm-lx86/config

=item --distro, -d

Install the specified distribution of x86 world. (See --help for a list.)

=item --media, -m

Look in the specified location for x86world installation media. May be
specified multiple times.

=item --archive

Use the specified archive file for x86world installation.

=item --quiet, -q

Perform the selected operation silently. Only print anything to the
screen if there is an error. Most messages are redirected to the log
file, so you may find more useful information there in the event of an
error.

This option has no effect in interactive mode.

=back 4

=cut

require 5.008; # 'use encoding' and other things introduced in 5.8

use strict;
use warnings;
use Cwd ();
use Getopt::Long;
use Pod::Usage;
use FindBin '$RealBin';
use lib "$RealBin/lib";
use Maiken;

# In case we need $0 after we've chdired
my $script = Cwd::abs_path($0);

# Get any user options
my $parser = Getopt::Long::Parser->new(config => ['bundling']);
my %args;
my @argv_copy = @ARGV; # we need to remember argv
$parser->getoptions(
		    \%args,
		    'install|i:s@',
		    'upgrade|u:s@',
		    'uninstall|n:s@',
		    'show',
		    'configure:s@',
		    'distro|d=s',
		    'media|m=s@',
		    'archive=s',
		    'config|c=s',
		    'quiet|q',
		    'license-accepted',
		    'powerpack-config=s',
		    'help',
		    'version'
		   ) or pod2usage(-verbose => 0);
# Don't do --help until later, because we want to add the list of x86
# world distributions.

# Special behaviour if --version specified
if ($args{version}) {
  my $version = Maiken->version();
  print "Installer version: $version\n";
  exit 0;
}

# We need to do this before Maiken chdirs
if ($args{config}) {
  $args{config} = Cwd::abs_path($args{config});
}
if ($args{'powerpack-config'}) {
  $args{'powerpack-config'} = Cwd::abs_path($args{'powerpack-config'});
}

my $installer = Maiken->new(product => 'RO', quiet => $args{quiet},
			    argv => \@argv_copy);

# For --help, display the usage message followed by a list of
# supported x86 world distributions.
if ($args{help}) {
  pod2usage(-verbose => 1, -exitval => 'NOEXIT', -input => $script);
  print "Valid x86 world distributions (see --distro) on this platform:\n";
  print "    ".join(' ', $installer->supported_distros)."\n\n";
  exit 1;
}


# Is this a call to run in PowerPack mode? This is an RO-specific
# mode.
if ($args{'powerpack-config'}) {
  my $modifiers = {
		   'license-accepted' => $args{'license-accepted'},
		  };
  $installer->run_powerpack_mode($args{'powerpack-config'}, $modifiers);
  exit 255; # in case for some reason the main call doesn't exit
}

# Next, let's look for the actions that all behave similarly
for my $action (qw/install upgrade uninstall show configure/) {
  if ($args{$action}) {
    my $modifiers = {
		     products => $args{$action},
		     distro => $args{distro},
		     media => $args{media},
		     archive => $args{archive},
		     config => $args{config},
		     'license-accepted' => $args{'license-accepted'},
		    };
    $installer->run_noninteractively($action, $modifiers);
    exit 255; # in case for some reason the main call doesn't exit
  }
}

# If we've got here, it must be interactive mode.
$installer->run_interactively;
