#!/usr/bin/perl

########################################################
# STUFF WE GET FROM THE COMMAND LINE
my $DISPLAYALERT	= $ARGV[0] . "/Contents/Resources/Alert.app/Contents/MacOS/Alert";
my $APP				= $ARGV[1];
my $RUNNING_ERROR	= "Running";
if (($#ARGV + 1) >= 3)
{
	$RUNNING_ERROR		= $ARGV[2];
}

# If the OS is 10.4, we check in preflight
my $SYSTEM_VERS = "/System/Library/CoreServices/SystemVersion.plist";
exit 0 if (CheckVersion( $SYSTEM_VERS, "10.4", "ProductVersion", ">=" ));

# Don't do these checks if we're a command line install
exit 0 if ( $ENV{COMMAND_LINE_INSTALL} );

# Is my app running?
my $APP_IS_RUNNING = !system("/usr/bin/killall -s '$APP' >&/dev/null");

# If not, we're done
exit 0 unless ($APP_IS_RUNNING);

#Is Software Update running?
my $SWU_IS_RUNNING = !system("/usr/bin/killall -s 'Software Update' >&/dev/null");

# No, just return an error.
exit 1 unless ($SWU_IS_RUNNING);

# OK, so if we're <10.4, not command line, the app is running, and software update is running,
# we need to present an alert to the user and hang out until they quit the app
$ENV{'ALERT_APP_ACTION'} = $RUNNING_ERROR;
while ($APP_IS_RUNNING) {
	system($DISPLAYALERT,"");
	# per nat torkington

	# wait for Alert to return, then check for app running...
	$APP_IS_RUNNING = !system("/usr/bin/killall -s '$APP' >&/dev/null");
} # while app is running, stay stuck here

exit 0;

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

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

}
