#!/usr/bin/perl

my $SYSTEM_VERS = "$ARGV[0]"."/System/Library/CoreServices/SystemVersion.plist";
my $QT_VERS = "$ARGV[0]"."/System/Library/Frameworks/QuickTime.framework/Versions/A/Resources/Info.plist";
my $EXIT_VALUE = 0;
my $Package_Path = $ENV{PACKAGE_PATH};
my $hasG4 = 1;
my $badOS = 0;
my $badQT = 0;
my $foundApp = 0;

DO_CHECKS: {

        # Only G3s are not compatible
        if(CheckIOReg("PowerPC,750@", "PowerPC,755@", "PowerPC,740@",
                "PowerPC,745@", "PowerPC,750CX@", "PowerPC,750CXe@",
                "PowerPC,G3@", "PowerMac1,2")) {
                $hasG4 = 0;
        }

        #set the exit value dependant on the hardware present
        if(!$hasG4)
        {
                $EXIT_VALUE = (( 1 << 5 ) | 16 );
                last;
        }

	if( ! -e "$SYSTEM_VERS" ) {
		$badOS = 1;
        }

        if(CheckVersion("$SYSTEM_VERS", "10.3.9", "ProductVersion", "<")) {
                $badOS = 1;
        }

        if(CheckVersion("$QT_VERS", "7.0", "CFBundleShortVersionString", "<")) {
                $badQT = 1;
        }

   	if(CheckVersion("/Applications/Apple Qmaster.app/Contents/Info.plist", "2.0", "CFBundleShortVersionString", ">=") && CheckVersion("/Applications/Apple Qmaster.app/Contents/Info.plist", "2.0.2", "CFBundleShortVersionString", "<")) {
		$foundApp = 1;
        }

	if(!$foundApp) {
		$EXIT_VALUE = ((1 << 6) | ( 1 << 5 ) | 21 );
	}


        if ($badOS)
        {
                $EXIT_VALUE = ((1 << 6) | ( 1 << 5 ) | 17 );
        }

        if ($badQT)
        {
                $EXIT_VALUE = ((1 << 6) | ( 1 << 5 ) | 18 );
        }

        if ($badOS && $badQT)
        {
                $EXIT_VALUE = ((1 << 6) | ( 1 << 5 ) | 19 );
        }

        if ($ENV{'COMMAND_LINE_INSTALL'})
        {
                last;
        }

        if($ARGV[0] ne "/") {
                $EXIT_VALUE = ((1 << 6) | ( 1 << 5 ) | 20 );
                last;
        }
}

exit($EXIT_VALUE);

###

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

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

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

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

}

sub CheckIOReg
{               
        $RESULT = 0;

        open(IOREGOUT, "/usr/sbin/ioreg |");
    
        foreach $LINE (<IOREGOUT>) {
                $BUF .= $LINE;  
        }
        close(IOREGOUT);        
    
        foreach $ITEM (@_) {    
                if($BUF =~ /$ITEM/g) {
                        $RESULT = 1;
                        last;
                }
        }
            
        return($RESULT);
}

