#!/usr/bin/perl

# set to 1 if you want some extra spew
$debug=0;

# Interfaces of CORBA servers that need to die.
@idls = ("IDL:GNOME/Evolution/ShellComponent:1.0",
	 "IDL:GNOME/Evolution/CalFactory:1.0",
	 "IDL:GNOME/Evolution/BookFactory:1.0",
         "IDL:GNOME/Evolution/Importer:1.0",
	 "IDL:GNOME/Evolution/IntelligentImporter:1.0",
	 "IDL:GNOME/Evolution/Shell:1.0",
	 "IDL:GNOME/Spell/Checker:0.1");


# IIDs of specific CORBA servers that need to die that don't implement
# useful interfaces (for querying, anyway)
@iids = ("OAFIID:GNOME_Evolution_Calendar_AlarmNotify_Factory",
	 "OAFIID:GNOME_GtkHTML_Editor_Factory:1.1",
	 "OAFIID:Bonobo_Moniker_xmldb_Factory");

##
## You shouldn't have to change anything below this point
##

$sysname=`uname -s`;
chop $sysname;

if ($sysname eq "SunOS") {
	$killcmd="pkill";
}
else {
	$killcmd="killall";
}

sub kill_exe {
	my ($exe_name) = @_;
	my $lt_name = "lt-$exe_name";
	my $sub_exe_name = substr ($exe_name, 0, 16);
	my $sub_lt_name = substr ($lt_name, 0, 16);

	printf ("killing $exe_name\n");

	$redirect = "2> /dev/null";

	print "$killcmd -9 $exe_name\n" if ($debug);
	`$killcmd -9 $exe_name $redirect`;

	print "$killcmd -9 $lt_name\n" if ($debug);
	`$killcmd -9 $lt_name $redirect`;

	print "$killcmd -9 $sub_exe_name\n" if ($debug);
	`$killcmd -9 $sub_exe_name $redirect`;

	print "$killcmd -9 $sub_lt_name\n" if ($debug);
	`$killcmd -9 $sub_lt_name $redirect`;
}

sub kill_exes {
	while (($key, $value) = each %things_to_kill) {
		&kill_exe($key);
	}
}

sub add_exe {
	my ($exe_name) = @_;
	$things_to_kill{$exe_name} = $exe_name;
}

sub add_factory {
	my ($factory_iid) = @_;

	open (FACTORY_QUERY, "oaf-client -q -s \"iid == '$factory_iid'\"|");
	while (<FACTORY_QUERY>) {
		if (/type exe, location (.*)$/) {
			&add_exe ($1);
		}
	}
	close (FACTORY_QUERY);
}

# we need to do separate queries for iids because cvs OAF loses when
# you do more than one 'iid ==' separated by OR's.  It returns a list
# of all CORBA servers.  Cool, eh?

sub run_iid_query {
	my $iid_query;

	for ($i = 0; $i < @iids; $i++) {
		$iid_query = "iid == '$iids[$i]'";

		#printf ("$iid_query\n");

		open (QUERY, "oaf-client -q -s \"$iid_query\"|");
		while (<QUERY>) {
			if (/type exe, location (.*)$/) {
				&add_exe ($1);
			}
			elsif (/type factory, location (.*)$/) {
				&add_factory ($1);
			}
		}
		close (QUERY);
        }
}

sub run_query {
	my ($idl_query, $iid_query, $oaf_query);

	$idl_query = "";
	for ($i = 0; $i < @idls; $i++) {
		$idl_query .= "repo_ids.has('$idls[$i]')";
		$idl_query .= " OR " if ($i < @idls - 1);
        }

	#$iid_query = "";
	#for ($i = 0; $i < @iids; $i++) {
	#	$iid_query .= "iid == '$iids[$i]'";
	#	$iid_query .= " OR " if ($i < @iids - 1);
        #}

	$oaf_query = $idl_query;
	#$oaf_query .= " OR $iid_query" if (@iids > 0);

	#printf ("$oaf_query\n");
	
	open (QUERY, "oaf-client -q -s \"$oaf_query\"|");

	while (<QUERY>) {
		if (/type exe, location (.*)$/) {
			&add_exe ($1);
		}
		elsif (/type factory, location (.*)$/) {
			&add_factory ($1);
		}
	}
	close (QUERY);
}

&run_query ();
&run_iid_query ();
&kill_exes();
