#!/usr/bin/perl -w
#
# $Id$
#
# Backup the hardware configuration on machine supporting it
# (Bios configuration, Raid configuration, ...)
#
use strict;
use File::Basename;

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

die "No CACHE_DIR parameter" if ((not defined $ARGV[0]) || (! -d $ARGV[0]));
my $locbkpdir = "/bkphw";
my $bkpdir = "$ARGV[0]$locbkpdir";
die "No CONF_DIR parameter" if ((not defined $ARGV[1]) || (! -d $ARGV[1]));
my $confdir = "$ARGV[1]";

die "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 "You need /usr/sbin/dmidecode for mindi hardware support";
while (<SYSTEM>) {
	next if (/^#/);
	$productname = $_;
	chomp($productname);
	}
close(SYSTEM);

die "WARNING: 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 the SmartStart Scripting toolkit tool $tool\nto benefit from mindi's enhanced ProLiant support\n";
	print "Get it from http://www.hp.com/servers/sstoolkit\n";
	print "And use the SDR to get all other HP ProLiant packages\n";
	print "Get it from http://downloads.linux.hp.com/SDR/downloads/ProLiantSupportPack/\n";
	open(PROLIANT,"$confdir/deplist.d/ProLiant.conf") || die "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 "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 "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$/) {
			# From the package
			if (! -f "/opt/hp/hp-scripting-tools/etc/conrep.xml") {
				$ret = system("$tool -s -x /opt/hp/hp-scripting-tools/etc/conrep.xml -f$bkpdir/conrep.dat");
			# From the SSSTK
			} elsif (! -f "/usr/share/conrep/conrep.xml") {
				$ret = system("$tool -s -x /usr/share/conrep/conrep.xml -f$bkpdir/conrep.dat");
			} else {
				next;
			}
			print SCRIPT "$tool -l -f$locbkpdir/conrep.dat\n";
			print TOOLS "$tool.xml\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";
		}
		# 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 "\nWARNING: No Hardware support for $productname.\nNot a big issue, just less features and risks ;-)\n";
	print "You may ask your manufacturer to contribute to the mindi project (harmless)\n";
}
rmdir $bkpdir if (-d $bkpdir) ;
