#!/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 /etc/sysconfig/i18n ] >> $errfile 2>&1 && for l in `cat /etc/sysconfig/i18n`; do export \$l; done >> $errfile 2>&1; [ -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 /etc/sysconfig/i18n ] >> $errfile 2>&1 && for l in `cat /etc/sysconfig/i18n`; do export \$l; done >> $errfile 2>&1; [ -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);