#!/usr/bin/perl

########################################################
my $SYSTEM_VERS 	= "/System/Library/CoreServices/SystemVersion.plist";
my $LOGIC_VERS		= "/Applications/Logic Express.app/Contents/version.plist";
my $Package_Path	= $ENV{PACKAGE_PATH};
my $EXIT_VALUE 		= 0;
########################################################

DO_CHECKS: {
    if( CheckVersion( $SYSTEM_VERS, "10.3", "ProductVersion", "<" )) 
    {
        $EXIT_VALUE = ((1 << 6) | ( 1 << 5 ) | 16 );
		exit($EXIT_VALUE);
    }
	# Must NOT be tier 3
	my $TIER_3_LANG = "/var/db/.AppleTier3Language";
	if (-e $TIER_3_LANG) {
		# return custom error
		$EXIT_VALUE = ( ( 1 << 6 ) | ( 1 << 5 ) | 20 );
		exit($EXIT_VALUE);
    }
	
	# This installer can only update an existing copy of Logic Express. Disabling these checks
	# results in a copy of Logic Express that does not work correctly.
	
	if ( (!-e $LOGIC_VERS) ||
		 CheckVersion($LOGIC_VERS, "7.0.0", "CFBundleShortVersionString", "<") ||
		 CheckVersion($LOGIC_VERS, "7.0.1", "CFBundleShortVersionString", ">")) {
		$EXIT_VALUE = ( ( 1 << 6 ) | ( 1 << 5 ) | 18 );
		exit($EXIT_VALUE);
	}

    ########################################################
    my $APP					= "Logic\ Express\.app";
    my $APP_IS_RUNNING 		= 0;
    my $SWU_APP				= "Software Update\.app";
    my $SWU_IS_RUNNING 		= 0;
    my $RUNNING_ERROR		= "Running";
	my $DISPLAYALERT		= $ARGV[0] . "/Contents/Resources/Alert.app/Contents/MacOS/Alert";
	my $CANCEL_COOKIE		= "/tmp/com.apple.pkg.LogicExpressUpdate.cancel";
    ########################################################
    open(PSOUT, "/bin/ps -awwx |");
    while( <PSOUT> ) {
        if( /\b$APP/ ) {
            $APP_IS_RUNNING = 1;
        }
    }
    close(PSOUT);
        
    open(PSOUT, "/bin/ps -awwx |");
    while( <PSOUT> ) {
        if( /\b$SWU_APP/ ) {
            $SWU_IS_RUNNING = 1;
        }
    }
    close(PSOUT);
        
	if ($SWU_IS_RUNNING == 1) {
		$RUNNING_ERROR		= "RunningOld";
	}
		
	while (($APP_IS_RUNNING == 1)) {
		unlink $CANCEL_COOKIE;

		$ENV{'ALERT_APP_ACTION'} = $RUNNING_ERROR;
		system($DISPLAYALERT,"");
		# per nat torkington
		
		$APP_IS_RUNNING = 0;

		open(PSOUT, "/bin/ps -awwx |");
		while( <PSOUT> ) {
			if( /\b$APP/ ) {
				$APP_IS_RUNNING = 1;
			}
		}
		close(PSOUT);
		
		# break out of the loop, and allow error to come up, if user hits cancel
		# note case above where we don't allow cancel
		if (-e $CANCEL_COOKIE) {
			unlink $CANCEL_COOKIE;
			$EXIT_VALUE = ((1 << 6) | ( 1 << 5 ) | 19 );
			exit($EXIT_VALUE);
		}
	} # while app is running, stay stuck here
}

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

}
