#!/usr/bin/perl -w
#
# test.pl - Test harness for common.pm
# 
# Synopsis:
# 
#	test.pl [ -d ]
# 

use Getopt::Std;
use FileHandle;
use Sys::Hostname;
use common;

#######################################################################
# Global Variables
#######################################################################

# set by command line args
$Debug = 1;

$NodeName = "unknown";
$DmesgCommand = "/bin/dmesg";			$DmesgArgs = "3>/dev/null";
$IfconfigCommand = "/sbin/ifconfig";
$IssuePath = "/etc/issue";
$MessageLogsPath = "/var/log/messages*";
$NetworkInterfaceName = "eth0";
$PrtconfCommand = "/usr/sbin/prtconf";		$PrtconfArgs = "-v";
$PsCommand = "/usr/bin/ps";			$PsArgs = "-aef";


#######################################################################
# Subroutines
#######################################################################

sub ProcessCommandLine
{
	if ( getopts('d') ) {
		if ( $opt_d ) { $Debug = 1; }
	}
	else {
		die "There were problems processing the command line.";
	}

	return;
}


sub TestSomething
{
	$Debug = 1;

	$NodeName = hostname();
	# RH Linux 6.0 tends to return the FQDN rather than just the node name
	$NodeName =~ /(\w+)\.*/;
	$main::NodeName = $1;
# 
# 	# test ReturnArray with a simple file
# 	my @IssueText = ReturnArray($IssuePath);
# 
# 	# test ReturnArray with a command with no command line arguments
# 	my @DmesgText = ReturnArray("$DmesgCommand");
# 
# 	# test ReturnArray with a command with command line arguments
# 	my @IfconfigText = ReturnArray("$IfconfigCommand $NetworkInterfaceName");
# 
# 	# test ReturnArray with a filename glob
# 	my @SyslogText = ReturnArray("$MessageLogsPath");

# 	my @DmesgText = ReturnArray("$DmesgCommand $DmesgArgs");

# 	my $Response = ReturnFstab("Category");

# 	my $Response = ReturnRunningSysvLp("Category", $PsCommand, $PsArgs);
# 	print "$Response\n";
# 	$Response = ReturnRunningBSDLpr("Category", $PsCommand, $PsArgs);
# 	print "$Response\n";
# 	$Response = ReturnRunningLPRng("Category");
# 	print "$Response\n";

	TestNICStuff();

	return;
}


sub TestNICStuff
{	my %Interfaces;

	if ( -x $PrtconfCommand ) {
		my @Prtconf = ReturnArray("$PrtconfCommand $PrtconfArgs");
		my $Iface;

		foreach $Line (@Prtconf) {
			next unless ( $Line =~ /([^,]*),\s+instance\s+\#(\d+)/ )
				and defined($IfList{$1});
			print "\tLine is $Line...\n" if $main::Debug;
			$Iface = "$1$2";
			$Iface =~ s/SUNW,//;
			$Interfaces{$Iface} = 1 unless defined $Interfaces{$Iface};
		}
	}

	foreach $Key (sort keys %Interfaces) {
		print "$Key\n";
	}

	return;
}


#######################################################################
# Mainline
#######################################################################

ProcessCommandLine();
TestSomething();

#######################################################################
# We're done!
#######################################################################
exit(0);
