#!/usr/bin/perl

########################################################
my $DISPLAYALERT		= $ARGV[0] . "/Contents/Resources/Alert.app/Contents/MacOS/Alert";
my $SYSTEM_VERS			= "/System/Library/CoreServices/SystemVersion.plist";
my $APPLICATION_LOC		= "/Applications/iWork '06/Pages.app";
my $APPLICATION_VERS	= $APPLICATION_LOC . "/Contents/version.plist";
my $EXIT_VALUE			= 0;
########################################################

DO_CHECKS: 
{
	########################################################
	# Must NOT be tier 3
	my $TIER_3_LANG = "/var/db/.AppleTier3Language";
	if (-e $TIER_3_LANG) {
		$EXIT_VALUE = ( ( 1 << 6 ) | ( 1 << 5 ) | 19 );
		last;
    }

	########################################################
    # Must have 10.3.9 or better BOOTED, please.
    if( (!-e $SYSTEM_VERS) ||
		CheckVersion( $SYSTEM_VERS, "10.3.9", "ProductVersion", "<" )) {
            $EXIT_VALUE = (( 1 << 6 ) | ( 1 << 5 ) | 16);
            last;
    }
	
	########################################################
	# App must be there ... (OK per EPM that the app cannot be moved)
	unless(-e $APPLICATION_LOC) {
		$EXIT_VALUE = (( 1 << 6 ) | ( 1 << 5 ) | 17);
		last;
	}
	# ... and must be version 2.0.1, 2.0.2
	unless( CheckVersion($APPLICATION_VERS, "2.0.1", "CFBundleShortVersionString", "==") ||
			CheckVersion($APPLICATION_VERS, "2.0.2", "CFBundleShortVersionString", "==")) {
				$EXIT_VALUE = (( 1 << 6 ) | ( 1 << 5 ) | 17);
				last;
	}
	
	if ( CheckVersion( $SYSTEM_VERS, "10.4", "ProductVersion", "<" )) {
		########################################################
		# Pages must not be running
		my $APP					= "Pages\.app";
		my $APP_IS_RUNNING 		= 0;
		my $SWU_APP				= "Software Update\.app";
		my $SWU_IS_RUNNING 		= 0;
		my $RUNNING_ERROR		= "AppRunning";

		########################################################
		open(PSOUT, "/bin/ps -awwx |");
		while( <PSOUT> ) {
			if( /$APP/ ) {
				$APP_IS_RUNNING = 1;
			}
		}
		close(PSOUT);
			
		open(PSOUT, "/bin/ps -awwx |");
		while( <PSOUT> ) {
			if( /$SWU_APP/ ) {
				$SWU_IS_RUNNING = 1;
			}
		}
		close(PSOUT);
			
		if (($APP_IS_RUNNING == 1) && ($SWU_IS_RUNNING == 0)) {
			$EXIT_VALUE = ((1 << 6) | ( 1 << 5 ) | 18 );
			last;
		} else {
			while (($APP_IS_RUNNING == 1) && ($SWU_IS_RUNNING == 1)) {
				$ENV{'ALERT_APP_ACTION'} = $RUNNING_ERROR;
				system($DISPLAYALERT,"");
				# per nat torkington
		
				$APP_IS_RUNNING = 0;
		
				open(PSOUT, "/bin/ps -awwx |");
				while( <PSOUT> ) {
					if( /$APP/ ) {
						$APP_IS_RUNNING = 1;
					}
				}
				close(PSOUT);
			} # while app is running, stay stuck here
		$EXIT_VALUE = 0;
		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;
    }

}
