#!/usr/bin/perl

########################################################
my $SYSTEM_VERS 	= "/System/Library/CoreServices/SystemVersion.plist";
my $TIER_3_LANG 	= "/var/db/.AppleTier3Language";
my $SUAPP			= "Software Update\.app";
my $APP_IS_RUNNING	= 0;
my $EXIT_VALUE 		= 0;
########################################################

open(PSOUT, "/bin/ps -awwx |");
while( <PSOUT> ) {
	if( /$SUAPP/ ) {
		$APP_IS_RUNNING = 1;
	}
}
close(PSOUT);

DO_CHECKS: 
{
	# If system version is less than 10.1.5, quit without error
    if( !CheckVersion( "$SYSTEM_VERS", "10.1.5", "ProductVersion", "<" )) 
	{
		$EXIT_VALUE = 1;
		last;
	}
	
	# If system version is less than 10.3.9, quit with error
    if( !CheckVersion( "$SYSTEM_VERS", "10.3.9", "ProductVersion", "<" )) 
	{
    	$EXIT_VALUE = ((1 << 6) | ( 1 << 5 ) | 16 );
		last;
	}

	# Must NOT be tier 3
	if (-e $TIER_3_LANG) {
		# return custom error
		$EXIT_VALUE = ( ( 1 << 6 ) | ( 1 << 5 ) | 18 );
		last;
    }

	# Get the IOregistry to see if this computer could possibly use the update.
    my $IOREG = `/usr/sbin/ioreg -l -w 0`;

	# Must be a correct model
    unless ($IOREG =~ /model.*<\"PowerBook5,4\">/ ||
			$IOREG =~ /model.*<\"PowerBook5,6\">/ ||
			$IOREG =~ /model.*<\"PowerBook5,7\">/ ||
			$IOREG =~ /model.*<\"PowerBook6,8\">/ ||
			$IOREG =~ /model.*<\"PowerMac8,1\">/ ||
			$IOREG =~ /model.*<\"PowerMac10,1\">/ ||
			$IOREG =~ /model.*<\"PowerMac10,2\">/) {
		$EXIT_VALUE = ((1 << 6) | ( 1 << 5 ) | 17 );
		last;
    }
    
	# Must have the correct drive
	unless ($IOREG =~ /device\smodel.*\"MATSHITADVD-R\s+UJ-835F\s*\"/ ||
			$IOREG =~ /device\smodel.*\"MATSHITADVD-R\s+UJ-835E\s*\"/) {
		$EXIT_VALUE = ((1 << 6) | ( 1 << 5 ) | 17 );
		last;
    }

    # Only check firmware if SU is not running
    if ( !$APP_IS_RUNNING ) 
    {
        unless ($IOREG =~ /device\srevision.*\"GEM8.*\"/ ||
                $IOREG =~ /device\srevision.*\"GAN7.*\"/ ||
                $IOREG =~ /device\srevision.*\"GFN7.*\"/ ||
                $IOREG =~ /device\srevision.*\"GGN7.*\"/) {
            $EXIT_VALUE = ((1 << 6) | ( 1 << 5 ) | 17 );
            last;
        }
    }
	
}

exit($EXIT_VALUE);

########################################################
########################################################

sub CheckVersion
{
    my $path            = $_[0];
    my $version         = $_[1];
    my $keyName         = $_[2];
    my $operator        = $_[3];
    
    my $oldSeperator;
    my $plistData;
    my @items;
    my $item;
    my $versiondata;
    my $i;
    my @theVersionArray;
    my %versiondata;
    my @versionArray;

    # if there's no THERE there, we return FALSE
    if (! -e $path) {
        return 0;
    }

    if (!$operator) {
        $operator = "==";
    }

    $oldSeperator = $/;
    $/ = \0;

    open( PLIST, "$path") || do {
        return 0;
    };

    $plistData = <PLIST>;
    $plistData =~ /<dict>(.*?)<\/dict>/gis;

    @items = split(/<key>/, $plistData);

    shift @items;
    foreach $item (@items) {
        $item =~ /(.*?)<\/key>.*?<string>(.*?)<\/string>/gis;
        $versiondata{ $1 } = $2;
    }

    close(PLIST);

    $/ = $oldSeperator;

    @theVersionArray = split(/\./, $versiondata{$keyName});
    for ($i = 0; $i < 3; $i++) {
        if(!$theVersionArray[$i]) {
            $theVersionArray[$i] = '0';
        }
    }

    @versionArray = split(/\./, $version);
    
    my $actualVersion;

    for ($i = 0; $i < 3; $i++) {
        if (($theVersionArray[$i] != $versionArray[$i]) or ($i == 2)) {

            $actualVersion = $theVersionArray[$i];
            $version = $versionArray[$i];

            last;
        }
    }

    my $expression = '$actualVersion ' . $operator . ' $version';
    if( eval ($expression) )
    {
        return 1;
    }
    else
    {
        return 0;
    }

}

