110120SGhee.Teo@Sun.COM#!/usr/perl5/bin/perl
210120SGhee.Teo@Sun.COM#
310120SGhee.Teo@Sun.COM# CDDL HEADER START
410120SGhee.Teo@Sun.COM#
510120SGhee.Teo@Sun.COM# The contents of this file are subject to the terms of the
610120SGhee.Teo@Sun.COM# Common Development and Distribution License (the "License").
710120SGhee.Teo@Sun.COM# You may not use this file except in compliance with the License.
810120SGhee.Teo@Sun.COM#
910120SGhee.Teo@Sun.COM# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
1010120SGhee.Teo@Sun.COM# or http://www.opensolaris.org/os/licensing.
1110120SGhee.Teo@Sun.COM# See the License for the specific language governing permissions
1210120SGhee.Teo@Sun.COM# and limitations under the License.
1310120SGhee.Teo@Sun.COM#
1410120SGhee.Teo@Sun.COM# When distributing Covered Code, include this CDDL HEADER in each
1510120SGhee.Teo@Sun.COM# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1610120SGhee.Teo@Sun.COM# If applicable, add the following below this CDDL HEADER, with the
1710120SGhee.Teo@Sun.COM# fields enclosed by brackets "[]" replaced with your own identifying
1810120SGhee.Teo@Sun.COM# information: Portions Copyright [yyyy] [name of copyright owner]
1910120SGhee.Teo@Sun.COM#
2010120SGhee.Teo@Sun.COM# CDDL HEADER END
2110120SGhee.Teo@Sun.COM#
2210120SGhee.Teo@Sun.COM# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
2310120SGhee.Teo@Sun.COM# Use is subject to license terms.
2410120SGhee.Teo@Sun.COM#
2510120SGhee.Teo@Sun.COM#
26*10485SGhee.Teo@Sun.COMuse Errno qw(EINTR :POSIX);
2710120SGhee.Teo@Sun.COM
2810120SGhee.Teo@Sun.COMmy $SVC_CUPS_SCHEDULER = 'cups/scheduler';
2910120SGhee.Teo@Sun.COMmy $SVCPROP = '/usr/bin/svcprop';
30*10485SGhee.Teo@Sun.COMmy $reexec = 0;
3110120SGhee.Teo@Sun.COM
3210120SGhee.Teo@Sun.COMsub svcprop {
3310120SGhee.Teo@Sun.COM        local ($fmri, $property) = @_;
3410120SGhee.Teo@Sun.COM        my $FH;
3510120SGhee.Teo@Sun.COM
3610120SGhee.Teo@Sun.COM        open($FH, "$SVCPROP -C -p $property $fmri 2>/dev/null |");
3710120SGhee.Teo@Sun.COM        $result = <$FH>;
3810120SGhee.Teo@Sun.COM        close($FH);
3910120SGhee.Teo@Sun.COM
4010120SGhee.Teo@Sun.COM        return ($result);
4110120SGhee.Teo@Sun.COM}
4210120SGhee.Teo@Sun.COM
4310120SGhee.Teo@Sun.COMsub print_service {
4410120SGhee.Teo@Sun.COM        my $service;
4510120SGhee.Teo@Sun.COM
4610120SGhee.Teo@Sun.COM        $service = svcprop("$SVC_CUPS_SCHEDULER:default", "general/active");
4710120SGhee.Teo@Sun.COM        ($service =~ /true/) && ($service = 'cups') || ($service = 'lp');
4810120SGhee.Teo@Sun.COM
4910120SGhee.Teo@Sun.COM        return ($service);
5010120SGhee.Teo@Sun.COM}
5110120SGhee.Teo@Sun.COM
5210120SGhee.Teo@Sun.COMsub kill_running_applets {
53*10485SGhee.Teo@Sun.COM	$reexec = 1;
5410120SGhee.Teo@Sun.COM        # cups applet daemon
5510120SGhee.Teo@Sun.COM        system("pkill -f '/system-config-printer/applet.py'");
5610120SGhee.Teo@Sun.COM        # lp applet daemon
5710120SGhee.Teo@Sun.COM        system("pkill ospm-applet");
5810120SGhee.Teo@Sun.COM}
5910120SGhee.Teo@Sun.COM
6010120SGhee.Teo@Sun.COMsub handle_signal {
6110120SGhee.Teo@Sun.COM        kill_running_applets ();
6210120SGhee.Teo@Sun.COM}
63*10485SGhee.Teo@Sun.COM
6410120SGhee.Teo@Sun.COM$SIG{USR1} = \&handle_signal;
6510120SGhee.Teo@Sun.COM
66*10485SGhee.Teo@Sun.COMmy $pid = fork();
67*10485SGhee.Teo@Sun.COMif (! defined($pid)) {
68*10485SGhee.Teo@Sun.COM	die "Error: fork() failed\n";
6910120SGhee.Teo@Sun.COM}
70*10485SGhee.Teo@Sun.COMelsif ($pid == 0) {
71*10485SGhee.Teo@Sun.COM	my $service =  print_service();
7210120SGhee.Teo@Sun.COM        if ($service eq 'lp') {
7310120SGhee.Teo@Sun.COM                exec('/usr/lib/lp/bin/desktop-print-management-applet');
7410120SGhee.Teo@Sun.COM        }
7510120SGhee.Teo@Sun.COM        else {
7610120SGhee.Teo@Sun.COM                exec('/usr/lib/cups/bin/desktop-print-management-applet');
7710120SGhee.Teo@Sun.COM        }
78*10485SGhee.Teo@Sun.COM
79*10485SGhee.Teo@Sun.COM}
80*10485SGhee.Teo@Sun.COMelse {
81*10485SGhee.Teo@Sun.COM	my $retid;
82*10485SGhee.Teo@Sun.COM	while ((($retid = waitpid($pid, 0)) < 0) && $!{EINTR}) {
83*10485SGhee.Teo@Sun.COM	}
84*10485SGhee.Teo@Sun.COM
85*10485SGhee.Teo@Sun.COM	if ($retid == $pid && $reexec) {
86*10485SGhee.Teo@Sun.COM        	exec('/usr/bin/desktop-print-management-applet');
87*10485SGhee.Teo@Sun.COM	}
8810120SGhee.Teo@Sun.COM}
8910120SGhee.Teo@Sun.COM
90*10485SGhee.Teo@Sun.COMexit(0);
91