#!/usr/bin/perl -w
#
# $Id$
#
# Gives the right driver to use for HP Smart Array Controllers for the corresponding Linux distribution
# Vased on the HP document hpsa migration at 
#
use strict;

my $drv = {
	"P420i"
}

# Handling Configuration files
my $tool = "";
my $ret = 0;
my $productname = undef;

die "ERROR: You need dmidecode for Hardware support\n" if (! -x "/usr/sbin/dmidecode");

mkdir $bkpdir,0755 if (! -d $bkpdir) ;
open(SYSTEM,"/usr/sbin/dmidecode -s 'system-product-name' 2> /dev/null |") || die "ERROR: You need /usr/sbin/dmidecode for mindi hardware support";
while (<SYSTEM>) {
	next if (/^#/);
	$productname = $_;
	chomp($productname);
	}
close(SYSTEM);

die "INFO: No product name found for Hardware support\n" if (not defined $productname);

if ($productname =~ /proliant/i) {
	print "Detected a $productname. Nice. Continue to support my job :-)\n";
	print "Activating ProLiant support for mindi\n";
	print "You can install SDR packages to benefit from mindi's enhanced ProLiant support\n";
	print "Get them from http://downloads.linux.hpe.com/SDR/\n";
	open(PROLIANT,"$confdir/deplist.d/ProLiant.conf") || die "ERROR: Unable to open $confdir/deplist.d/ProLiant.conf";
	# generate a list of what need to be put on the backup media
	open(TOOLS,"> $bkpdir/../tools.files") || die "ERROR: Unable to open $bkpdir/../tools.files";
	# generate a script that will be launched at rstore time to perform the HW setup
	open(SCRIPT,"> $bkpdir/../mindi-rsthw") || die "ERROR: Unable to open $bkpdir/../mindi-rsthw";
	print SCRIPT  << 'EOF';
#!/bin/bash
#
# Script generated by mindi
#
# This script will restore potentially your HW configuration
# on your system before partioning occurs.
#
# You may want to reboot after that step if you think that
# resetting BIOS parameters to the value restored
# may have an impact on your restoration process or if
# you want to benefit from any firmware update that could have happened.
#
EOF
	while($tool = <PROLIANT>) {
		my $hasfound = 0;
		next if ($tool =~ /^#/);
		chomp($tool);
		# skip non-executable/exising binaries
		if (! (-x $tool)) {
			next;
		} else {
			print "Found $tool, activating enhanced HP ProLiant support in mindi\n";
			print TOOLS "$tool\n";
		}
		if ($tool =~ /\/conrep$/) {
			my $xmlf = "/opt/hp/hp-scripting-tools/etc/conrep.xml";
			# From the package
			if (-f $xmlf) {
				$ret = system("$tool -s -x $xmlf -f$bkpdir/conrep.dat");
			# From the SSSTK
			} else {
				$xmlf = "/usr/share/conrep/conrep.xml";
				if (-f $xmlf) {
					$ret = system("$tool -s -x $xmlf -f$bkpdir/conrep.dat");
				} else {
					next;
				}
			}
			print SCRIPT "$tool -l -f$locbkpdir/conrep.dat\n";
			print TOOLS "$xmlf\n";
		}
		if ($tool =~ /\/hp-rcu$/) {
			$ret = system("$tool -s -f$bkpdir/conrep.dat");
			print SCRIPT "$tool -l -f$locbkpdir/conrep.dat\n";
		}
		if ($tool =~ /\/hpacuscripting$/) {
			$hasfound = 1;
			my $dir=basename($tool);
			# Just backup internal info for a DR
			$ret = system("$tool -c $bkpdir/hpacuscripting.dat -internal");
			# We could want to reset it before.
			print SCRIPT "# $tool -reset -i $locbkpdir/hpacusripting.dat\n";
			print SCRIPT "$tool -i $locbkpdir/hpacusripting.dat\n";
		}
		if ($tool =~ /\/hponcfg$/) {
			$ret = system("$tool -a -w $bkpdir/hponcfg.dat");
			print SCRIPT "$tool -f $locbkpdir/hponcfg.dat\n";
		}
		if ($tool =~ /\.scexe$/) {
			print "Found $tool, that firmware will be applied at restore time on your HP ProLiant\n";
			print SCRIPT "./$tool -s\n";
		}
		if ($tool =~ /\/hp-fm/) {
			print "Found $tool, firmware will be upgraded at restore time on your HP ProLiant\n";
			print SCRIPT "./$tool upgrade\n";
		}
		# Kept for compatibility with older version of tools
		if (($tool =~ /\/hpacucli$/) && ($hasfound == 0)) {
			my $dir=basename($tool);
			$ret = system("$tool -c $bkpdir/hpacucli.dat");
			print SCRIPT "$tool -i $locbkpdir/hpacucli.dat\n";
		}
		if ($ret != 0) {
			print "$tool returned an error. Hardware support may not be complete\n";
		}
	}
	close(PROLIANT);
	close(TOOLS);
	close(SCRIPT);
} else {
	print "INFO: No Hardware optimized support for your product $productname\n";
	print "      You may ask your manufacturer to contribute to the mindi project\n      for firmware Disaster Recovery support (harmless)\n";
}
rmdir $bkpdir if (-d $bkpdir) ;
