#!/usr/bin/perl

use strict;

###################################################################################
my $ALERT = "$ARGV[0]/Contents/Resources/Alert.app/Contents/MacOS/Alert";
my $PLISTBUDDY_SYS = "/usr/libexec/PlistBuddy";
my $PLISTBUDDY_INT = "$ARGV[0]/Contents/Resources/PlistBuddy";
my $SENDER = "$ARGV[0]/Contents/Resources/SlimSender";
my $APP = "iTunes";
my $APP_IS_RUNNING = 0;
my $APP_RUNNING_CURRENT_USER = 0;
my $APP_RUNNING_OTHER_USER = 0;
###################################################################################

my $PLISTBUDDY = $PLISTBUDDY_SYS;
if ( ! -e $PLISTBUDDY ) {
	$PLISTBUDDY = $PLISTBUDDY_INT;
}
my $version = `"$PLISTBUDDY" -c 'print CFBundleShortVersionString' "$ARGV[2]/Library/Application\ Support/iTunes/iPodVoiceOver.framework/Versions/A/Resources/version.plist"`;
chomp($version);
if ( $version eq '0.0' )
{
	print "$version foo";
	exit(0);
}

my @switchedPIDs = ();
my @activePIDs = ();

# abort if alert is unavailable
if (! -e $ALERT) {
	exit(1);
}

# retrieve iTunes PIDs, if any
my $PROC = `/usr/bin/killall -dc "$APP" 2> /dev/null`;
if (($? >> 8) != 0) {
	exit(0);
}

# get current user ID
my $currentUID = `/usr/bin/id -u "$ENV{USER}"`;
if (($? >> 8) != 0) {
	$currentUID = -1;
}
chomp $currentUID;

# try to close any switched iTunes PIDs
my @PROCS = split(/\n/, $PROC);
foreach (@PROCS) {
	if (! -e $SENDER) {
		last;
	}
	if ($_ =~ m/sig:.+cmd:$APP.+pid:([0-9]+).+dev:.+uid:([0-9]+)/) {
		my $pid = $1;
		my $processUID = $2;
		if ($processUID != $currentUID) {
			print "Attempting to close iTunes PID $pid";
			system("\"$SENDER\"", "-p", "$pid", "-e", "aevtquit");
			sleep(3);	
		}
	}
	else {
		next;
	}
}

# retrieve iTunes PIDs (again), if any
$PROC = `/usr/bin/killall -dc "$APP" 2> /dev/null`;
if (($? >> 8) != 0) {
	exit(0);
}
$APP_IS_RUNNING = 1;

while ($APP_IS_RUNNING == 1) {

	# classify iTunes PIDs as running under active user or switched user
	@switchedPIDs = ();
	@activePIDs = ();
	my @PROCS = split(/\n/, $PROC);
	foreach (@PROCS) {
		if ($_ =~ m/sig:.+cmd:$APP.+pid:([0-9]+).+dev:.+uid:([0-9]+)/) {
			my $pid = $1;
			my $processUID = $2;
			if ($processUID == $currentUID) {
				push(@activePIDs, $pid);
				$APP_RUNNING_CURRENT_USER = 1;
			}
			else {
				push(@switchedPIDs, $pid);
				$APP_RUNNING_OTHER_USER = 1;
			}
		}
		else {
			next;
		}
	}

	# current user message has priority over other user message
	if ($APP_RUNNING_CURRENT_USER) {
		$ENV{'ALERT_APP_ACTION'} = "AppRunning";
	}
	elsif ($APP_RUNNING_OTHER_USER) {
		$ENV{'ALERT_APP_ACTION'} = "AppRunningOtherUser";
	}
	else {
		$ENV{'ALERT_APP_ACTION'} = "AppRunning";
	}

	system ("/usr/bin/su", $ENV{USER}, "-c", "\"$ALERT\"");
	
	# terminate if there is no SWU or Installer process for the active user
	`/usr/bin/killall -u $ENV{USER} -s "Software Update" 2> /dev/null`;
	if (($? >> 8) != 0) {
		`/usr/bin/killall -u $ENV{USER} -s "Installer" 2> /dev/null`;
		if (($? >> 8) != 0) {
			exit(0);
		}
	}
	
	# reset for next loop iteration
	$APP_RUNNING_CURRENT_USER = 0;
	$APP_RUNNING_OTHER_USER = 0;
	
	$PROC = `/usr/bin/killall -dc "iTunes" 2> /dev/null`;
	$APP_IS_RUNNING = !($? >> 8);

}

exit(0);

