#!/bin/sh

# cupsconfig - Start and configure cups

# Copyright (C) 2001, Klaus Knopper
# Copyright (C) 2004-2015, Stefan Lippers-Hollmann <s.l-h@gmx.de>

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2 of the
# License.
#
# 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.

# CUPS not installed
[ -x /etc/init.d/cups ] || exit 2

# CUPS not running, start it
if [ -z "$(pidof cupsd)" ]; then
	[ "$(id -u)" -ne 0 ] && pkexec /usr/sbin/service cups start
	sleep 4
fi

for i in $(seq 1 20); do
	if lpstat -r; then
		if [ -x /usr/bin/gnome-cups-manager ] ; then
			[ -n "$DISPLAY" ] && exec gnome-cups-manager
		elif [ -f /usr/share/applications/kcm_printer_manager.desktop ]; then
			[ -n "$DISPLAY" ] && exec systemsettings kcm_printer_manager
		else
			[ -n "$DISPLAY" ] && exec x-www-browser "http://localhost:631"
			[ -z "$DISPLAY" ] && exec   www-browser "http://localhost:631"
		fi 
		break
	else
		case "$LANG" in
			es*)
				[ -n "$DISPLAY" ] && [ -x /usr/bin/xmessage ] && exec xmessage '¡No puedo arrancar el servidor CUPS!'
				echo '¡No puedo arrancar el servidor CUPS!' 1>&2
				exit 3
				;;
			*)
				[ -n "$DISPLAY" ] && [ -x /usr/bin/xmessage ] && exec xmessage 'Cannot start CUPS server!'
				echo 'Cannot start CUPS server!' 1>&2
				exit 3
				;;
		esac
	fi
	sleep 2
done
