#!/bin/sh
### BEGIN INIT INFO
# Provides:          cloudprintd
# Required-Start:    $network $local_fs $remote_fs cups
# Required-Stop:     $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: A service for sharing printers on Google Cloud Print
# Description:       Share locally-defined CUPS printers with the Google
#                    Cloud Print service. The printers can be accessed
#                    locally or remotely by authorized users via multiple
#                    platforms.
### END INIT INFO

# Author: David Steele <dsteele@gmail.com>

# Copyright 2014 David Steele <dsteele@gmail.com>
# This file is part of cloudprint
# Available under the terms of the GNU General Public License version 2 or later


PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC=cloudprintd
NAME=cloudprint
DAEMON=/usr/sbin/cloudprintd

# Read configuration variable file if it is present
[ -r /etc/default/$DESC ] && . /etc/default/$DESC

INCLUDE_OPT=""

for str in $EXCLUDE_LIST ; do \
     INCLUDE_OPT="$INCLUDE_OPT -x $str";\
done

for str in $INCLUDE_LIST ; do \
     INCLUDE_OPT="$INCLUDE_OPT -i $str";\
done

AUTHFILE=/var/lib/cloudprintd/authfile
PIDFILE=/var/run/$NAME.pid
DAEMON_ARGS="-d -u -a $AUTHFILE -p $PIDFILE $FAST_POLL $INCLUDE_OPT"
SCRIPTNAME=/etc/init.d/$DESC

# Exit if the package is not installed
[ -x $DAEMON ] || exit 0

# Lintian says don't load this vars.sh that was provided with this skeleton
#
# Replace with default values for the variables used.
## Load the VERBOSE setting and other rcS variables
#. /lib/LINTIANIGNOREinit/vars.sh
#
VERBOSE=no



# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

#
# Function that starts the daemon/service
#
do_start()
{
	if [ ! -e $AUTHFILE ] ; then
		echo "No authentication found - run 'service $DESC login' as root"
		return 2
	fi

	start-stop-daemon --start --quiet --pidfile $PIDFILE --startas $DAEMON \
		--name $DESC --test > /dev/null \
		|| return 1
	start-stop-daemon --start --quiet --pidfile $PIDFILE --startas $DAEMON \
		--name $DESC -- $DAEMON_ARGS \
		|| return 2
}

do_login()
{
	rm -f $AUTHFILE || true
	rm -f $AUTHFILE.sasl || true

	echo "Accounts with 2 factor authentication require an application-specific password"

	$DAEMON -c -u -a $AUTHFILE $INCLUDE_OPT

}

do_logout()
{
	$DAEMON $DAEMON_ARGS -l
}

do_stop()
{
	start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $DESC
	RETVAL="$?"
	[ "$RETVAL" = 2 ] && return 2
	start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
	[ "$?" = 2 ] && return 2
	rm -f $PIDFILE || true
        rm -f $PIDFILE.lock || true
	return "$RETVAL"
}

case "$1" in
  start)
    [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC " "$NAME"
    do_start
    case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	esac
  ;;
  stop)
	[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
	do_stop
	case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	esac
	;;
  login)
        do_login
       ;;
  logout)
        do_logout
       ;;
  status)
       status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
       ;;
  restart|force-reload)
	log_daemon_msg "Restarting $DESC" "$NAME"
	do_stop
	case "$?" in
	  0|1)
		do_start
		case "$?" in
			0) log_end_msg 0 ;;
			1) log_end_msg 1 ;; # Old process is still running
			*) log_end_msg 1 ;; # Failed to start
		esac
		;;
	  *)
	  	# Failed to stop
		log_end_msg 1
		;;
	esac
	;;
  *)
	echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload|login|logout}" >&2
	exit 3
	;;
esac

:
