#!/usr/bin/perl -w
use strict;
use File::Basename;

my $result;
my $progname = basename $0;
my $all_tools = "bin2aout cli_parser ewc imageinfo-view isos-module-sizes isos-size mkhfbootpatch mkinitrd syslink vcomp bin2c comp flash2uart isos-addr2line isos-gcc isos-nm isos-strings mkhfcheck mkproduct vexpand aconfig bintohex decodebacktrace flashrd isos-ar isos-gdb isos-objcopy isos-strip mkhfconfig mksrom trajan-bin2c virata-tools-ver apigen bstrip digest gdbserver isos-as isos-ld isos-objdump isos-ver mkhfflash readarchive uncoff arm-asm catobj doxygen gdbterm isos-coverage isos-ranlib metamk mkhfflashfs simulator_make_cat atmos-mkobj ccfix elf2aout iceboot isos-cxxfilt isos-readelf mkhfboot mkhfimage simulator_syslink perl make sed awk sh";

# Print out the version of this scripts.
print "$progname version 1.1:\n\n";

# Check all tools are in path.
foreach my $tool (split ' ', $all_tools) {
    $result = try_cmd("which $tool");

    # Verify command was found.
    if ($result =~ /no $tool in/) {
	print STDERR "*** Error: $tool wasn't found. ***\n";
    }
}

# Produce a list of tools that should have documentation.
my @doc_tools = (split ' ', $all_tools . " aconfig-syntax arm-binutils arm-cpp arm-gcc arm-objcopy atmos atmos_cache_check atmos-cache-check atmos-module-sizes awk bin2c coff2aout comp debugging gnu-cpp isos-cpp mkhfboot perl sed syslink treenode");
my $skip_list = "isos-gasp isos-module-sizes mkhfbootpatch vexpand decodebacktrace trajan-bin2c gdbserver doxygen isos-coverage metamk simulator_make_cat atmos-mkobj simulator_syslink sh";
my @skip_list = (split ' ', $skip_list);

# Check that man pages exist for all the tools.
foreach my $tool (@doc_tools) {
    # It is ok for certain tools not to have a man page, so we skip
    # the test for these tools.
    my $test = grep /$tool/, @skip_list;

    if ($test eq 1) {
	print STDOUT "man $tool: Skipped.\n";
    } else {
	$result = `man $tool 2>&1`;
	if ($? eq 0) {
	    print STDOUT "man $tool: OK.\n";
	} else {
	    print STDERR "*** Error: man $tool returned $?.\n";
	}
    }
}
print STDOUT "\n";

# Verify apropos can find the man pages.
foreach my $tool (@doc_tools) {
    # It is ok for certain tools not to have a man page, so we skip
    # the test for these tools.
    my $test = grep /$tool/, @skip_list;

    if ($test eq 1) {
	print STDOUT "apropos $tool: Skipped.\n";
    } else {
	$result = `man -k $tool 2>&1`;
	if ($? eq 0) {
	    print STDOUT "apropos $tool: OK.\n";
	} else {
	    print STDERR "*** Error: apropos $tool returned $?.\n";
	}
    }
}
print STDOUT "\n";

# Try the virata-tools-ver command.
try_cmd("virata-tools-ver");

# Discover perl version(s).
try_cmd("perl --version");
$result = try_cmd("/usr/bin/perl --version");
if (!($result =~ /v5.6.0/)) {
    print STDERR "*** Error: Wrong version of perl found. ***\n";
}

# Discover make version.
$result = try_cmd("make --version");
if (!($result =~ /3.79.1/)) {
    print STDERR "*** Error: Wrong version of make found. ***\n";
}

# Discover sed version.
try_cmd("sed --version");

# Discover awk version.
try_cmd("awk --version");

# Discover shell versions.
try_cmd("sh --version");

# Output certain bits of the users environment.
try_cmd("env | grep \"^VIRATA_TOOLS\"");
try_cmd("env | grep \"^ATMOSTOOLS\"");
try_cmd("env | grep \"^PATH\"");
try_cmd("env | grep \"^MANPATH\"");
try_cmd("env | grep \"^GCC_EXEC_PREFIX\"");

# MD5 the currently selected tools.
try_cmd("find $ENV{ATMOSTOOLS} -not -type d -exec md5sum {} \\;");

exit 0;

sub try_cmd {
    my $cmd = shift;

    print STDOUT "### ${cmd}:\n";
    my $result = `$cmd 2>&1`;
    print STDOUT "$result\n";
    return $result;
}
