#!/usr/bin/perl

my $EXIT_VALUE		= 0;
my $TARGET_VOLUME	= $ARGV[0];
my $SYSTEM_VERS		= $TARGET_VOLUME."/System/Library/CoreServices/SystemVersion.plist";
my $SERVER_VERS		= $TARGET_VOLUME."/System/Library/CoreServices/ServerVersion.plist";
my $QT_VERS			= $TARGET_VOLUME."/System/Library/Frameworks/QuickTime.framework/Versions/Current/Resources/version.plist";
my $TIER3		= $TARGET_VOLUME."/var/db/.AppleTier3Language";

DO_CHECKS: 
{
    if(! -e $SYSTEM_VERS) {
        $EXIT_VALUE = (( 1 << 5 ) | 16);
    }
	
	if(! -e $QT_VERS) {
		$EXIT_VALUE = (( 1 << 5 ) | 16);
	}
	
	if(CheckVersion ("$QT_VERS", "7.1.6", "CFBundleShortVersionString", "!=")) {
		$EXIT_VALUE = ((1 << 5) | 16);
	}

    if(-e $TIER3) {
        $EXIT_VALUE = (( 1 << 5 ) | (1 << 6) | 16);
    }
    if(CheckVersion ( "$SYSTEM_VERS", "10.3.9", "ProductVersion", "!=" ))
    {
		$EXIT_VALUE = (( 1 << 5 ) | 16);
    }

}

exit( $EXIT_VALUE );

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

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

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

}
