#!/usr/bin/perl

###############################################################################
#                                                                             #
# Filename: bakAsm                                                            #
#                                                                             #
# Description:                                                                #
#                                                                             #
###############################################################################
###############################################################################
#                                                                             #
#  Copyright  2000 - 2001 Intel Corporation.                                 #
#  Intel Corporation All Rights Reserved.                                     #
#                                                                             #
#  The source code contained or described herein and all documents related to #
#  the source code ("Material") are owned by Intel Corporation or its         #
#  suppliers or licensors.  Title to the Material remains with Intel          #
#  Corporation or its suppliers and licensors.  The Material contains trade   #
#  secrets and proprietary and confidential information of Intel or its       #
#  suppliers and licensors.  The Material is protected by worldwide copyright #
#  and trade secret laws and treaty provisions.  No part of the Material may  #
#  be used, copied, reproduced, modified, published, uploaded, posted,        #
#  transmitted, distributed, or disclosed in any way without Intel's prior    #
#  express written permission.                                                #
#                                                                             #
#  No license under any patent, copyright, trade secret or other intellectual #
#  property right is granted to or conferred upon you by disclosure or        #
#  delivery of the Materials, either expressly, by implication, inducement,   #
#  estoppel or otherwise. Any license under such intellectual property rights #
#  must be express and approved by Intel in writing.                          #
#                                                                             #
###############################################################################

$commands{"get_temp"}               = "get_temp";
$commands{"get_volt_plus12"}        = "get_volt_plus12";
$commands{"get_volt_minus12"}       = "get_volt_minus12";
$commands{"get_volt_plus5"}         = "get_volt_plus5";
$commands{"get_volt_plus3point3"}   = "get_volt_plus3point3";
$commands{"get_volt_cpuio"}         = "get_volt_cpuio";
$commands{"get_volt_cpucore"}       = "get_volt_cpucore";
$commands{"get_chassis"}            = "get_chassis";
$commands{"get_eventlog"}           = "get_eventlog";
$commands{"get_archived_eventlog"}  = "get_archived_eventlog";
$commands{"get_info_baseboard"}     = "get_info_baseboard";
$commands{"get_info_processor"}     = "get_info_processor";
$commands{"get_info_bios"}          = "get_info_bios";
$commands{"get_lcd"}				= "get_lcd";
$commands{"get_lcd_chardonnay"}			= "get_lcd_chardonnay";
$commands{"get_lightbar"}			= "get_lightbar";
$commands{"get_fan_0"}				= "get_fan_0";
$commands{"get_fan_1"}				= "get_fan_1";
$commands{"get_fan_2"}				= "get_fan_2";
$commands{"get_fan_3"}				= "get_fan_3";
$commands{"get_fan_4"}				= "get_fan_4";
$commands{"set_password"}           = "set_password";
$commands{"set_upgrade"}            = "set_upgrade";
$commands{"set_shutdown"}           = "set_shutdown";
$commands{"set_reboot"}             = "set_reboot";
$commands{"set_era"}                = "set_era";
$commands{"set_snmp"} 			= "set_snmp";
$commands{"get_bbu"} 			= "get_bbu";
$commands{"get_info_bbu"} 		= "get_info_bbu";

# Set files
if( -d "/proc/sys/dev/sensors/ds1780-i2c-0-2d")
{
	$PROC_DIR                        = "/proc/sys/dev/sensors/ds1780-i2c-0-2d/";
}else{
	$PROC_DIR                        = "/proc/sys/dev/sensors/adm9240-i2c-0-2d/";
}
$ASMBIN_DIR	    				 = "/usr/ins/intel/bin/asm/";
$SNMP_DIR    					 = "/etc/snmp/";
$BIN_DIR    					 = "/bin/";
$USR_BIN_DIR					 = "/usr/bin/";
$STATUS_FAN_DIR					 = "/var/local/";

$STATUS_TEMP                     = $PROC_DIR."temp";
$STATUS_VOLT_PLUS12              = $PROC_DIR."12V";
$STATUS_VOLT_PLUS5               = $PROC_DIR."5V";
$STATUS_VOLT_PLUS3POINT3         = $PROC_DIR."3.3V";
$STATUS_VOLT_CPUIO               = $PROC_DIR."2.5V";
$STATUS_VOLT_CPUCORE             = $PROC_DIR."Vccp1";
$STATUS_VOLT_VCCP2          	 = $PROC_DIR."Vccp2";
$STATUS_CHASSIS                  = $PROC_DIR."alarms";
$STATUS_FAN1            		 = $PROC_DIR."fan1";
$STATUS_FAN2            		 = $PROC_DIR."fan2";

$BBU_PROC_DIR			 = "/proc/sys/dev/sensors/smart_battery-i2c-0-0b/";
$BBU_MANUFACTURER_NAME	 = $BBU_PROC_DIR."manufacturer";
$BBU_VERSION    		 = $BBU_PROC_DIR."specification";
$BBU_DEVICE_CHEMISTRY	 = $BBU_PROC_DIR."chemistry";
$BBU_SERIAL_NUMBER   	 = $BBU_PROC_DIR."serial_number";
$BBU_TIME_TO_FULL_CHARGE = $BBU_PROC_DIR."avg_time_full";
$BBU_DESIGN_VOLTAGE		 = $BBU_PROC_DIR."design_voltage";
$BBU_FULL_CHARGE_CAPACITY= $BBU_PROC_DIR."full_charge_capacity";
$BBU_DESIGN_CAPACITY	 = $BBU_PROC_DIR."design_capacity";
$BBU_ESTIMATED_CHARGE_REMAINING  = $BBU_PROC_DIR."relative_charge";
$BBU_ESTIMATED_RUN_TIME	 = $BBU_PROC_DIR."avg_time_empty";
$BBU_TIME_ON_BATTERY	 = $BBU_PROC_DIR."avg_time_empty";
$BBU_BATTERY_STATUS		 = $BBU_PROC_DIR."status";

$EVENTLOG				 = $ASMBIN_DIR."sysEventLog";
$SMBIOS							 = $ASMBIN_DIR."sysSMBIOS";
$BIOSDATA						 = $ASMBIN_DIR."sysBIOSData";
$SNAPSHOT						 = $ASMBIN_DIR."sysSnapshot";
$LIGHTBAR						 = $ASMBIN_DIR."sysLightbar";
$FANMUX							 = $ASMBIN_DIR."sysFanMUX";

$LS								 = $BIN_DIR."ls";
$MV								 = $BIN_DIR."mv";
$CHMOD							 = $BIN_DIR."chmod";
$CHOWN							 = $BIN_DIR."chown";
$KILLALL						 = $USR_BIN_DIR."killall";

$SNMPD_CONF						 = $SNMP_DIR."snmpd.conf";

if( exists($commands{$ARGV[0]} ) )
{
	eval $commands{$ARGV[0]}
}else{
 	printf "Unknown command\n";
}

## BBU Into
sub get_info_bbu
{
	my ($file);
	my @files = ( $BBU_MANUFACTURER_NAME, $BBU_VERSION, $BBU_DEVICE_CHEMISTRY, $BBU_SERIAL_NUMBER,
			$BBU_TIME_TO_FULL_CHARGE, $BBU_DESIGN_VOLTAGE, $BBU_FULL_CHARGE_CAPACITY,
			$BBU_DESIGN_CAPACITY, $BBU_ESTIMATED_CHARGE_REMAINING,
			$BBU_ESTIMATED_RUN_TIME, $BBU_TIME_ON_BATTERY);

	foreach $file (@files) {
		open( handle, "< $file");
		print <handle>;
		close(handle);
	}

	# Finally, for battery status
	get_bbu();
}

## BBU Data
sub get_bbu
{
	# possible values: INSERTED, REMOVED, POWEROUT, OVERHEAT, MISSING

	# (*sigh*) This is fake. I still can't reconcile the behavior of the battery with the
 	# specification. IT JUST MAKES NO SENSE!!!! AAUUUUGH!!!!!

	if (!(-e $BBU_BATTERY_STATUS)) {
		print "MISSING\n";
		return;
	}

	print "INSERTED\n";
}

## 	Get temperature values
sub get_temp
{
	open( handle, "< $STATUS_TEMP");
	print <handle>;
	close(handle);
}

## 	Voltage (+12) value.
sub get_volt_plus12
{
	open( handle, "< $STATUS_VOLT_PLUS12");
	print <handle>;
	close(handle);
}

## 	Voltage (-12) value.
sub get_volt_minus12
{
	open( handle, "< $STATUS_VOLT_VCCP2");
	($x, $x, $Vccp2) = split(" ", <handle>);
	close(handle);
	
	$minus12 = ((($Vccp2 * (192 / 270)) * (-1200 / 192)) + 23.12) * -1;
	
	print "0 0 $minus12";
}

## 	Voltage (+5) value.
sub get_volt_plus5
{
	open( handle, "< $STATUS_VOLT_PLUS5");
	print <handle>;
	close(handle);
}

## 	Voltage (+3.3) value.
sub get_volt_plus3point3
{
	open( handle, "< $STATUS_VOLT_PLUS3POINT3");
	print <handle>;
	close(handle);
}

## 	Voltage (CPU I/O) value.
sub get_volt_cpuio
{
	open( handle, "< $STATUS_VOLT_CPUIO");
	print <handle>;
	close(handle);
}

## 	Voltage (CPU core) value.
sub get_volt_cpucore
{
	open( handle, "< $STATUS_VOLT_CPUCORE");
	print <handle>;
	close(handle);
}

## 	Chassis value.
sub get_chassis
{
	open( handle, "< $STATUS_CHASSIS");
	print "". (<handle> & (1 << 4) ) ? 1 : 0;
	close(handle);
}

## 	Event log.
sub get_eventlog
{
	open( handle, "$EVENTLOG |");
	print <handle>;
	close(handle);
}

## 	Archived Event logs.
sub get_archived_eventlog
{
	open( handle, "$LS /var/log/sel*gz -1 |");
	while( <handle> ) 
	{
		open( handle2, "$EVENTLOG $_ |");
		print <handle2>;
		close(handle2);
	}
	close(handle);
}

## 	Baseboard information.
sub get_info_baseboard
{
	open( handle, "$SMBIOS dump baseboard |");
	print <handle>;
	close(handle);
}

## 	Processor information.
sub get_info_processor
{
	open( handle, "$SMBIOS dump processor |");
	print <handle>;
	close(handle);
}

## 	BIOS information.
sub get_info_bios
{
	open( handle, "$SMBIOS dump bios |");
	print <handle>;
	close(handle);
}

## 	LCD status
sub get_lcd
{
	open( handle, "$SNAPSHOT |");
	print <handle>;
	close(handle);
}
sub get_lcd_chardonnay
{
        open( handle, "$SNAPSHOT |");
        print <handle>;
        close(handle);
}


## 	Light status
sub get_lightbar
{
	if( -e $LIGHTBAR )
	{
		open( handle, "$LIGHTBAR |");
		print <handle>;
		close(handle);
	}
	else
	{
		print "0\n";
	}
}

## 	Fan 0 Status
sub get_fan_0
{
	open( handle, "<" . $STATUS_FAN_DIR . "fan1");
	print <handle>;
	close(handle);
}

## 	Fan 1 Status
sub get_fan_1
{
	open( handle, "<" . $STATUS_FAN_DIR . "fan2");
	print <handle>;
	close(handle);
}

## 	Fan 2 Status
sub get_fan_2
{
	open( handle, "<" . $STATUS_FAN_DIR . "fan3");
	print <handle>;
	close(handle);
}

## 	Fan 3 Status
sub get_fan_3
{
	open( handle, "<" . $STATUS_FAN_DIR . "fan4");
	print <handle>;
	close(handle);
}

## 	Fan 4 Status
sub get_fan_4
{
	open( handle, "<" . $STATUS_FAN_DIR . "fan5");
	print <handle>;
	close(handle);
}

## 	Set password.
sub set_password
{
	print "You have password";
}

## 	Set Upgrade.
sub set_upgrade
{
	print "You have upgrade";
}

## 	Set shutdown.
sub set_shutdown
{
	system "/sbin/init 0  > /dev/null";
}

## 	Set reboot.
sub set_reboot
{
	system "/sbin/init 6  > /dev/null";
}

## 	Set era.
sub set_era
{
	system "$BIOSDATA update_ram";
	
	## Restart era.
#	system "/etc/rc.d/init.d/era stop > /dev/null";
#	system "/etc/rc.d/init.d/era start > /dev/null";
}

## 	Set SNMP.
sub set_snmp
{
	my ($community, @newlines, @lines);

	$community = $ARGV[1];

	@newLines = @lines = ();

	ReadFile($SNMPD_CONF,\@lines);
	while (defined($line = shift(@lines)))
	{
		if ($line =~ /^com2sec.*$/)
		{
			## Community name.
			push( @newLines, "com2sec User default $community");
		}else{
			## Preserve othr lines.
			push( @newLines, $line);
		}
	}
	WriteFile($SNMPD_CONF, 644, "root", "root", @newLines);
	@newLines = @lines = ();

	## causes SNMP to re-read it's configuration file
	system("$KILLALL -HUP snmpd > /dev/null ");
}

### Shared functions

sub ReadFile
{
	my ($file,$lines) = @_;
	my ($line);

	## open the file
	if (open(FILE,"$file") == 0)
	{                
		return 0;
	} 
	else
	{
		while (defined($line = <FILE>))
		{
			chomp($line);
			push(@$lines,$line);
		}
		close(FILE);
	}
	return(0);
}

sub WriteFile
{
	## Takes an array as an argument, The array elements should
	## not have \n on them

	my ($file, $mod, $owner, $group, @lines) = @_;
	my ($line, $time);

	$time = time();

	## open the file for writing
	if (open(FILE,">$file.$time") == 0)
	{
		return 0;
	}else{
		while (defined($line = shift(@lines)))
		{
			## This will leave no blank lines in the file
			if ($line eq "\n") {}
			else { print FILE $line."\n"; }
			## print FILE $line."\n"; }
		}
		close(FILE);
	}
	## Write and set permissions.
	system("$MV -f $file.$time $file");
 	system($CHMOD, $mod, $file);
	system($CHOWN, $owner.'.'.$group, $file);
	return(0);
}

