#!/usr/bin/perl

########################################################
# STUFF WE GET FROM OUTSIDE
my $DISPLAYALERT	= "\"" . $ARGV[0] . "/Contents/Resources/Alert.app/Contents/MacOS/Alert\"";
my $user = $ENV{USER};

exit 0 unless ($user);
exit 0 if ($ENV{COMMAND_LINE_INSTALL});
########################################################
my $APPLICATIONS = "GarageBand";
my $RUNNING_ERROR	= "Running";
my $OTHER_RUNNING_ERROR = "RunningOther";

DO_CHECKS: 
{
	# if we have other_running_error defined, see if some other user is running app
	my $someoneIsRunning = (`ps -awwwx -ouser,comm | egrep "$APPLICATIONS" | grep -v $user`);
	while ($someoneIsRunning)
	{
		$ENV{'ALERT_APP_ACTION'} = $OTHER_RUNNING_ERROR;
		system("/usr/bin/su", $user, "-c", $DISPLAYALERT);
		
		$someoneIsRunning = (`ps -awwwx -ouser,comm | egrep "$APPLICATIONS" | grep -v $user`);
	}
	
	# See if we're running app
	my $iAmRunning = (`ps -awwwx -ouser,comm | egrep "$APPLICATIONS" | grep $user`);
	while ($iAmRunning) 
	{
		$ENV{'ALERT_APP_ACTION'} = $RUNNING_ERROR;
		system("/usr/bin/su", $user, "-c", $DISPLAYALERT);

		$iAmRunning = (`ps -awwwx -ouser,comm | egrep "$APPLICATIONS" | grep $user`);
	}
}

exit(0);
