#!/usr/bin/perl

###############################################################################
#                                                                             #
# Filename: sysBiosUpgrade                                                    #
#                                                                             #
# 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.                          #
#                                                                             #
###############################################################################

###############################################################################
# FindPartition
###############################################################################

sub FindPartition 
{
	my ($MountPoint) = @_;
	my $Return = '';
	
	# Get the file system table...
	if (open(FSTAB, "/etc/fstab"))
	{
		my @FileSystemTable = <FSTAB>;
        
		# Close the file.
		close(FSTAB);
		
		# Look through the file...
		foreach $_ (@FileSystemTable) 
		{
			# See if there is a mountpoint match...
			if (/$MountPoint\s/) 
			{
				# Set the return value.
				($Return) = /(\S*)/ ;
				
				last;
			}
		}
   }
   
   return $Return;
}

###############################################################################
# main
###############################################################################

# Get the /boot partition device.
my $SourceDevice = FindPartition('/boot');

# Prep the /dos partition device.
chomp($SourceDevice);
chop($SourceDevice);
$SourceDevice .= '1';

$DIR_MNT = '/mnt';

if (!defined($ARGV[0])) {
	exit(5);
}

$file = $ARGV[0];
if (! -e "$file") {
	exit(1);
}

$rc = system("/bin/mount -t msdos $SourceDevice $DIR_MNT");
if ($rc > 0) {
	exit(2);
}

if (! -d "$DIR_MNT/biosupgr") {
	if (-e "$DIR_MNT/biosupgr") {
		unlink("$DIR_MNT/biosupgr");
	}
	mkdir("$DIR_MNT/biosupgr", 0755);
}
else {
	system("/bin/rm -f $DIR_MNT/biosupgr/*");
}

$rc = system("/bin/cp $file $DIR_MNT/biosupgr/bupgr.exe");
if ($rc > 0) {
	exit(3);
}

open(FH_BIOS_UPGRADE, ">$DIR_MNT/biosupgr/biosupgr.bat");
print FH_BIOS_UPGRADE "cd BIOSUPGR\r\n";
print FH_BIOS_UPGRADE "C:\\BIOSUPGR\\BUPGR.EXE < C:\\BIOSUPGR\\IN.TXT\r\n";
print FH_BIOS_UPGRADE "COPY P*.BIO XYZ.BIO\r\n";
print FH_BIOS_UPGRADE "C:\\BIOSUPGR\\IFLASH.EXE /P XYZ.BIO /R\r\n";
close(FH_BIOS_UPGRADE);

open(FH_BIOS_IN, ">$DIR_MNT/biosupgr/in.txt");
print FH_BIOS_IN "Y\r\nA\r\n";
close(FH_BIOS_IN);

system("/bin/umount $DIR_MNT");

exit(0);

