#!/usr/bin/perl -w

my @required = (
"/sbin/rmmod",
"/sbin/modprobe",
"badblocks",
"basename",
"bash",
"cat",
"chattr",
"cp",
"cut",
"clear",
"date",
"df",
"dialog",
"diff",
"dmesg",
"egrep",
"gcc",
"grep",
"head",
"kill",
"ln",
"make",
"modprobe",
"mount",
"nice",
"perl",
"pstree",
"pwd",
"renice",
"rm",
"rmmod",
"setterm",
"sync",
"sed",
"sleep",
"tail",
"tee",
"umount",
"yes");


my @optional = (
"/usr/sbin/chat",
"sensors",
"smartctl",
"switchto",
"stty");

system("uname -a | grep -q ' ppc ' ");
if ($? == 0) {
	push(@required, "pdisk");
	push(@optional, "fdisk");
} else {
	push(@required, "fdisk");
}

my @kernelcheck = (
"/usr/src/linux/kernel/panic.c",
"/usr/src/linux/Makefile",
"/usr/src/linux/include/linux/kernel.h");

my $param = shift(@ARGV);
my $i;
my $j;
my $x;
my $y;
my $exitvalue = 0;

if ( defined($param) and $param eq "--withrpms" ) {
	# for this, set the path.  I want to be able to build RPMS
	# as non root, including the specfile.
	$ENV{PATH}="/usr/sbin:/sbin:$ENV{PATH}";
	my %packages;
	foreach $i (@required,@kernelcheck) {
		## Find the real path of $i, put in $j
		$j = $i;
		$j=`which $i 2>/dev/null` unless -e $i;
		$j =~ s/\s//g;
		die "$i not found" unless -e $j;
		## Is $j in an RPM, and if so which one
		$x=`rpm -qf $j`;
		$x =~ s/\s//g;
		die "$j not in an rpm" if (! $x);
		## What is the base name of that RPM
		$y=`rpm -qi $x | head -n 1 | awk \'{ print \$3;}\'`;
		$y =~ s/\s//g;
		## Put that basename in the $packages hash.
		$packages{$y} = 1;
	}
	print "Requires: ";
	$j = "";
	foreach $i (keys %packages) {
		if (!$j) {
			print ("$i");
			$j = 1;
		} else {
			print (", $i");
		}
	}
	print "\n";
	exit (0);
}

print "** Checking required executables **\n";

I: foreach $i (@required) {
	if (-x $i) { next I; }
	$j = $i;
	$j = `which $i 2>/dev/null`;
	$j =~ s/\s//g;
	do {	
		print "$i not found in path\n";
		$exitvalue = 1;
	} unless -x $j;
}

print "** Checking for compilable kernel source tree **\n";

I2: foreach $i (@kernelcheck) {
	if (-e $i) { next I2; }
	print "$i not found:  you do not have the kernel sources installed\n";
	$exitvalue = 1;
}

print "** Checking perl **\n";
system ("./selftest/perltest");
if ($? != 0) {
	print "You are missing some required perl libraries.  See selftest/perltest for\na list.\n";
	$exitvalue = 1;
};

print "** Checking for root access **\n";

if ($> != 0) {
        print "Effective UID must be 0 to run burn (is $>)\n";
        $exitvalue = 1;
}

print "** Checking optional executables **\n";
I: foreach $i (@optional) {
        if (-x $i) { next I; }
        $j = $i;
	$j = `which $i 2>/dev/null`;
        $j =~ s/\s//g;
        do {    
                print "optional: $i not found in path\n";
        } unless -x $j;
}

exit ($exitvalue);
