#!/usr/bin/perl
#
# OSF1_procspd.pl - display the processor speed
# 
# This Perl script is derived from a Digital provided
# Korn shell script called sys_check.
# 
# Synopsis:
# 
#	OSF1_procspd.pl [ -d ]
# 

use Getopt::Std;
use FileHandle;
use IO::File;
use IPC::Open2;
use POSIX qw(tmpnam);

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

# set by command line args
$Debug = 1;

$DbxCommand = "/usr/bin/dbx";
$SizerCommand = "/usr/sbin/sizer";


#######################################################################
# 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;

	my $ProcessorSpeed = GetKVar("rpb->rpb_counter/1000000");
	print "speed=$ProcessorSpeed\n";

	return;
}


sub GetKVar
{	my $ArgToDbx = shift;
	my $TempFilename = "";		# set below

	# are we root - if not, don't bother...
	if ( $< != 0 ) {
		# $main::RunAsRoot = 1;
		return "Unknown";
	}

	# where is the kernel?
	my $Kernel = `$SizerCommand -b`;
	chomp($Kernel);

	# get a temporary dummy command file
	do { $TempFilename = tmpnam() }
		until $TempFH = IO::File->new($TempFilename, O_RDWR|O_CREAT|O_EXCL);

	# create command line for dbx...
	my $CommandFile = "-c $TempFilename";
	my $CommandLine = "$DbxCommand $CommandFile -k /$Kernel /dev/mem 2>/dev/null";

	# open up pipes to dbx...
	open2(*README, *WRITEME, $CommandLine);

	# start dialog with dbx...
	# can we get prompt?
	my $Version = <README>;
	my $HelpLine = <README>;
	my $BlankLine = <README>;
	my $StoppedLine = <README>;
	# send request to print value
	print WRITEME "p $ArgToDbx\n";
	my $ProcessorSpeed = <README>;
	chomp($ProcessorSpeed);
	# send "quit"
	print WRITEME "quit\n";

	close WRITEME;
	close README;

	# clean up
	unlink $TempFilename or
		die "Couldn't remove temp file: $TempFilename: $!";

	return $ProcessorSpeed;
}


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

ProcessCommandLine();
TestSomething();

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