#! /bin/bash
#
# Copyright (C) 2011-15 Gym. Himmelsthuer, Frank Schuette, Germany
#
# Author: Frank Schuette, 2015
#
# /bin/linuxmuster-printer
#
# System startup script for automatic network printer installation
# depending on file linuxmuster/printers on server
#

# read ldap server ip
if test -s /etc/sysconfig/linuxmuster-client
then . /etc/sysconfig/linuxmuster-client
fi

if test "$1" = "-v"; then
    VERBOSE=true
fi

set_default_printer() {
    local STANDARDDRUCKER=$(/usr/bin/lpstat -h ${LDAP_SERVER} -d|head -n 1|cut -d:  -f2)
    if test -n "$VERBOSE"; then
	echo "Ich setze den Standarddrucker $STANDARDDRUCKER des Servers $LDAP_SERVER als lokalen Standarddrucker."
    fi
    if test -n "${STANDARDDRUCKER}"; then
	/usr/bin/lpadmin -d $STANDARDDRUCKER >/dev/null 2>&1
    fi
} # set_default_printer

install_printer() {
  local NAME=$1
  # Install accessible printers only.
  if ! echo -en "\r" | /usr/bin/lp -d "${NAME}" -h "${LDAP_SERVER}" >/dev/null 2>&1; then
	if /usr/bin/lpstat -p "${NAME}" >/dev/null 2>&1; then
		if test -n "$VERBOSE"; then
			echo -n "lokaler Drucker wird entfernt - "
		fi
		lpadmin -x "${NAME}";
	fi
	test -n "$VERBOSE" && echo "kein Zugriff auf den Netzwerkdrucker"
	return
  fi
  test -n "$VERBOSE" && echo "wird lokal installiert"
  RAUM=`lpoptions -h "${LDAP_SERVER}" -p "${NAME}"| sed "s# \([^ ]\+\)=#\n\1=#g" | sed "s#'##g"|grep "^printer-location="|cut -d\= -f2`
  INFO=`lpoptions -h "${LDAP_SERVER}" -p "${NAME}"| sed "s# \([^ ]\+\)=#\n\1=#g" | sed "s#'##g"|grep "^printer-info="|cut -d\= -f2`
  MEDIA=`lpoptions -h "${LDAP_SERVER}" -p "${NAME}"| sed "s# \([^ ]\+\)=#\n\1=#g" | sed "s#'##g"|grep "^media="|cut -d\= -f2`
  if [ "x${MEDIA}" != "x" ]; then
    MEDIA="-o media=${MEDIA}"
  fi
  MODELL=`lpoptions -h "${LDAP_SERVER}" -p "${NAME}"| sed "s# \([^ ]\+\)=#\n\1=#g" | sed "s#'##g"|grep "^printer-make-and-model="|cut -d\= -f2`
  PSOPTS=`lpoptions -h "${LDAP_SERVER}" -p "${NAME}" -l | while read zeile; do
    OPTION=${zeile%%/*}
    VALUE=${zeile#* \*}
    VALUE=${VALUE%% *}
    echo -n " -o ${OPTION}=${VALUE}"
done`
  if echo "${MODELL}"|grep -q "hpcups"; then
    PREFERHPCUPS=y
  else
    PREFERHPCUPS=n
  fi
  if echo "${MODELL}"|grep -q "hpijs"; then
    PREFERHPIJS=y
  else
    PREFERHPIJS=n
  fi
  MODELL="${MODELL% v*}";
  MODELL="${MODELL%, *}";
  MODELL="${MODELL% (recommended)*}";
  MODELL="${MODELL% (KPDL)*}";
  MODELL="${MODELL%(P)*}";
	TREIBER=
	if test -n "${MODELL}"; then
	    ZEILE1=
	    HPCUPSDRV=
	    HPIJSDRV=
	    RECOMMENDED=
	    ZEILEN=$(lpinfo --make-and-model "${MODELL}" -m)
	    while read ZEILE; do
		if test -z "${ZEILE1}"; then
		    ZEILE1="${ZEILE%% *}";
		fi
		if echo "${ZEILE}" | grep -q "hpcups"; then
		    HPCUPSDRV="${ZEILE%% *}";
		fi
		if echo "${ZEILE}" | grep -q "hpijs"; then
		    HPIJSDRV="${ZEILE%% *}";
		fi
		if echo "${ZEILE}" | grep -q "(recommended)"; then
		    RECOMMENDED="${ZEILE%% *}";
		fi
	    done<<<"$ZEILEN";
	    if test -n "$VERBOSE"; then
		echo "Treiberkandidaten: model($MODELL-hpcups($PREFERHPCUPS),hpijs($PREFERHPIJS))"
		echo "                   hpijs($HPIJSDRV),hpcups($HPCUPSDRV),rec($RECOMMENDED)"
	    fi
	    if test "${PREFERHPCUPS}" = "y" -a -n "${HPCUPSDRV}"; then
		TREIBER=${HPCUPSDRV}
	    elif test "${PREFERHPIJS}" = "y" -a -n "${HPIJSDRV}"; then
		TREIBER=${HPIJSDRV}
	    elif test -n "${RECOMMENDED}"; then
		TREIBER=${RECOMMENDED}
	    elif test -z "${TREIBER}"; then
		TREIBER=${ZEILE1}
	    fi
	fi
	if test -z "${TREIBER}"; then
	    TREIBER=OpenPrintingPPDs/postscript/Apple-LaserWriter_4_600.Postscript.ppd.gz
	fi
	if test -n "$VERBOSE"; then
	    echo "Installation mit: NAME($NAME) MODELL($MODELL) TREIBER($TREIBER) MEDIA($MEDIA) RAUM($RAUM) INFO($INFO)";
	    echo "                  -o auth-user-required=none -o printer-error-policy=abort-job ${MEDIA}";
	    echo "                  Optionen(${PSOPTS})";
	else
	    echo "Ich installiere $NAME($TREIBER)";
	fi
	lpadmin -p "${NAME}" -E -v "ipp://${LDAP_SERVER}/printers/${NAME}" -P "/usr/share/cups/model/${TREIBER}" \
	  -L "${RAUM}" -D "${INFO}" -o auth-user-required=none -o printer-error-policy=abort-job \
	  ${MEDIA} ${PSOPTS}
} # install_printer

remove_obsolete_printers(){
    for p in `/usr/bin/lpstat -v|grep "ipp://${LDAP_SERVER}/"|cut -d\:  -f1|cut -d\  -f3`; do
	if /usr/bin/lpstat -h ${LDAP_SERVER} -p ${p} >/dev/null 2>&1; then
	    if test -n "$VERBOSE"; then
		echo "Der Netzwerkdrucker ${p} ist auf dem Server ${LDAP_SERVER} vorhanden und wird lokal beibehalten."
	    fi
	else
	    if test -n "$VERBOSE"; then
		echo "Der Netzwerkdrucker ${p} ist nicht auf dem Server ${LDAP_SERVER} vorhanden und wird lokal entfernt."
	    fi
	    lpadmin -x "${p}"
	fi
    done;
}

# Enforce default umask to avoid problems with wrong file permissions
# for example of /etc/printcap (see Novell/Suse Bugzilla bnc#31567).
umask 022

if test -n "${LDAP_SERVER}"; then
    remove_obsolete_printers
    for p in `/usr/bin/lpstat -h ${LDAP_SERVER} -p|grep "^printer"| cut -d\  -f2`; do
	if test -n "$VERBOSE"; then
	    echo -n "Netzwerkdrucker ${p}: "
	fi
	install_printer ${p}
    done;
    set_default_printer
fi

if systemctl is-enabled cups.service >/dev/null 2>&1; then
    systemctl restart cups.service
fi
