#!/usr/bin/perl

use strict;

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

my $SYSTEM_VERS = "/System/Library/CoreServices/SystemVersion.plist";
my $EXIT_VALUE = 0;
my @machines = (
				'PowerMac7,2',
				'PowerMac7,3',
				);


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

DO_CHECKS: {
        # 10.3.5 or higher system must be active
        if(CheckVersion("$SYSTEM_VERS", "10.3.5", "ProductVersion", "<")) {
                $EXIT_VALUE = ((1 << 6) | ( 1 << 5 ) | 16 );
                last;
        }
        # Check the machine IOPlatformExpertDevice ID
        if(CheckMachine( \@machines )) {
                $EXIT_VALUE = ((1 << 6) | ( 1 << 5 ) | 17 );
                last;
        }

}
exit($EXIT_VALUE);

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

sub CheckMachine
{

	my $machineRef = shift;
		
	foreach my $line (`/usr/sbin/ioreg`) {
		if($line =~ /IOPlatformExpertDevice/) {
			foreach my $machine ( @$machineRef ) {
				if($line =~ /$machine/i) {
					return 0;
				} 
				else { next };
			}
		}
	}
	return 1;

}

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

sub CheckVersion
{
    my $path            = $_[0];
    my $version         = $_[1];
    my $keyName         = $_[2];
    my $operator        = $_[3];
    my %versiondata;

    if (! -e $path) {
        return 0;
    }

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

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

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

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

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

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

    close(PLIST);

    $/ = $oldSeperator;

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

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

    for (my $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;
    }

}

