summaryrefslogtreecommitdiffstats
path: root/perl-install/standalone/autosetupprintqueues
blob: 50e906e87cf633e7876917877bad6ab0d4523a5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/perl
#
# Copyright (C) 2003-2005 Mandriva
#
# Till Kamppeter <till@mandrakesoft.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License Version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

use strict;
use lib qw(/usr/lib/libDrakX);
use standalone;
use printer::printerdrake;
use printer::main;

# Read mode in which we will run
my $commandline = join('', @ARGV);
$commandline =~ /-(auto|nogui|gui|waitforgui)\b/;
my $gui = $1;
$gui ||= "auto";

$gui = "nogui" if (! -x "/usr/X11R6/bin/X");

if ($gui ne "nogui") {
    # Auto-detect whether we go in non-X mode or not
    my $desktopuser = `cat /var/run/console.lock 2>/dev/null`;
    if (!$desktopuser) {
	if ($gui eq "auto") {
	    # Non-X mode
	    $gui = "nogui";
	} elsif ($gui eq "waitforgui") {
	    # Wait until a user logs in on the system's X console
	    while (!($desktopuser = `cat /var/run/console.lock 2>/dev/null`)) {
		sleep(5);
	    }
	    # Wait for the window manager to start
	    sleep(10);
	    $gui = "gui";
	} else {
	    # "gui" was requested but no user logged in
	    die "Cannot start in '--gui' mode, no user logged in!\n";
	}
    } else {
	# X mode (a user is logged in)
	$gui = "gui";
    }

    if ($gui eq "gui") {
	# Allow root's windows to be opened on the user's display and
	# start printerdrake then, in a mode to do nothing else than
	# automatically setting up print queues.
	my $userhome =
	    `getent passwd $desktopuser|awk -F: '{print \$6}' 2>&1`;
	chomp $userhome;
	my $errfile = "/dev/null";
	if (-r "$userhome/.Xauthority") {
	    system "export DISPLAY=\"localhost:0.0\"; export USER=$desktopuser; [ -r $userhome/.i18n ] >> $errfile 2>&1 && for l in `cat $userhome/.i18n`; do export \$l; done >> $errfile 2>&1; export XAUTHORITY=$userhome/.Xauthority; /usr/sbin/printerdrake --onlyautoqueue >> $errfile 2>&1;";
	} else {
	    system "export DISPLAY=\"localhost:0.0\"; export USER=$desktopuser; [ -r $userhome/.i18n ] >> $errfile 2>&1 && for l in `cat $userhome/.i18n`; do export \$l; done >> $errfile 2>&1; /bin/su $desktopuser -c \"/usr/X11R6/bin/xhost +localhost >> $errfile 2>&1\"; /usr/sbin/printerdrake --onlyautoqueue >> $errfile 2>&1; /bin/su $desktopuser -c \"/usr/X11R6/bin/xhost -localhost >> $errfile 2>&1\"";
	}
	exit 0;
    }
}

# Data structure for printer data
my $printer;

# Dummy variable needed to call the subroutines of the printerdrake.pm
# module. The real data structure is not needed for non-interactive
# non-X mode
my $in;

# Run the subroutines of printerdrake.pm in non-interactive non-X mode
$::noX = 1;

# Subroutines of printerdrake.pm used for automatic setup of print queues
$::autoqueue = 1;

# Do not let printerdrake ask for the spooler
$printer->{SPOOLER} ||= 'cups';

# Get info about already installed print queues
eval { $printer = printer::main::getinfo('') };

# Run the automatic, non-interactive print queue setup of printerdrake
printer::printerdrake::init($printer, $in);