#!/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 $tooldir = "";
my $ret = 0;

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";
my $productname = <SYSTEM>;
close(SYSTEM);

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

chomp($productname);
if ($productname =~ /proliant/i) {
	print "Detected a $productname. Nice. Continue to support my job :-)\n";
	print "Activating Proliant support for mindi\n";
	open(PROLIANT,"$confdir/proliant.files") || die "Unable to open $confdir/proliant.files";
	open(TOOLS,"> $bkpdir/../tools.files") || die "Unable to open $bkpdir/../tools.files";
	open(SCRIPT,"> $bkpdir/../mindi-rsthw") || die "Unable to open $bkpdir/../mindi-rsthw";
	print SCRIPT  << 'EOF';
#!/bin/bash
#
# 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 you restoration process
#
# put dynamic libraries at an accessible place
for l in /usr/local/lib/*; do
	ln -sf $l /usr/lib
done
EOF
	while($tool = <PROLIANT>) {
		next if ($tool =~ /^#/);
		chomp($tool);
		$tooldir = dirname($tool);
		if (! (-e $tool)) {
			print "You should install the SmartStart Scripting toolkit tool $tool\nto benefit from mindi's enhanced hardware support\n";
			print "Get it from http://www.hp.com/servers/sstoolkit\n";
			next;
		} else {
			print "Found $tool, activating enhanced HP Proliant support in mindi\n";
			print TOOLS "$tool\n";
		}
		if ($tool =~ /\/conrep$/) {
			$ret = system("cd $tooldir ; $tool -s -f$bkpdir/conrep.dat");
			print SCRIPT "(cd $tooldir ; $tool -l -f$locbkpdir/conrep.dat)\n";
			print TOOLS "$tool.xml\n";
		}
		if ($tool =~ /\/.acuxebin$/) {
			my $dir=basename($tool);
			$ret = system("export ACUXE_BIN_INSTALLATION_DIR=$dir ; export IM_CFGFILE_PATH=$dir ; export ACUXE_LOCK_FILES_DIR=$dir/locks ; $tool -c $bkpdir/cpqacuxe.dat");
			print SCRIPT "export ACUXE_BIN_INSTALLATION_DIR=$dir ; export IM_CFGFILE_PATH=$dir ; export ACUXE_LOCK_FILES_DIR=$dir/locks ; $tool -i $locbkpdir/cpqacuxe.dat\n";
			print TOOLS "$tooldir/bld\n";
		}
		if ($tool =~ /\/hponcfg$/) {
			$ret = system("$tool -w $bkpdir/hponcfg.dat");
			print SCRIPT "$tool -r $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\n";
		}
		if ($ret != 0) {
			print "$tool returned an error. Hardware support may not be complete\n";
		}
	}
	close(PROLIANT);
	close(TOOLS);
	close(SCRIPT);
} else {
	print "WARNING: No Hardware support for $productname\n";
	print "You may ask your manufacturer to contribute to the mindi project\n";
}
rmdir $bkpdir if (-d $bkpdir) ;
