#!/bin/sh
#  $Id: lpenable.sh,v 1.9 2005/07/28 15:31:57 billwe Exp $
#  Copyright (C) 1999-2005 Sun Microsystems, Inc.
#  This software code contains proprietary information 
#  and should be treated as confidential.
#
#  Component: Tarantella printing
#  Author: Bill Weir
#
#  Script called by the tarantella print command-line script to enable or
#  disable printing at the UNIX level.
#

# command execution tracing
case "$trace" in
x) set -x
esac

IsCups()
{
    # Is the installed print system CUPS? (Common UNIX Printing System)
    _yes=0 _no=1
    # Have we been told what to say?
    case "" in
    y) return ${_yes};;
    n) return ${_no};;
    esac
    [ ! -d /etc/cups ] && return ${_no}		# no cups directory
    # Looks like we might have CUPS - look for lpadmin to confirm
    PATH=/usr/sbin:/usr/bin:/etc:${PATH} export PATH
_lpadmin=''
for _dir in `echo "$PATH" | tr : ' '`; do
    case "${_dir}" in
    /*) [ -x "${_dir}/lpadmin" ] && { _lpadmin="${_dir}/lpadmin"; break; }
    esac
done

    [ -z "${_lpadmin}" ] && return ${_no}	# no lpadmin program
    # Finally, look for a running cupsd process
    case "${SCOARCH}" in
    i3li*|ppli*) _psflags="ajx";;
    *)     _psflags="-ef"
    esac
    _cupsd=`ps ${_psflags} 2>/dev/null | egrep '( |/)cupsd( |$)' | egrep -v egrep`
    [ -z "${_cupsd}" ] && return ${_no}		# cupsd not running
    return ${_yes}
}


CONF_FILE=/etc/ttaprinter.conf
[ -f "${TTADIR}/etc/ttaprinter.conf" ] && CONF_FILE="${TTADIR}/etc/ttaprinter.conf"

# are we enabling or disabling?
case "$0" in
*enable)  doenable='t';;
*disable) doenable='' ;;
*)	  exit 1
esac

# make sure we can find the necessary commands
PATH=${PATH}:/usr/sbin:/usr/bin:/sbin:/usr/lib:/etc export PATH

# We need this nonsense to run the "enable" command, to make sure
# we don't inadvertently call a shell builtin of the same name.
run_command()
{
    command="$1"
    shift
_command=''
for _dir in `echo "$PATH" | tr : ' '`; do
    case "${_dir}" in
    /*) [ -x "${_dir}/${command}" ] && { _command="${_dir}/${command}"; break; }
    esac
done

    [ -n "${_command}" ] && "${_command}" "$@"
}

doit()
{
  TTAPRINTER="$1"
  if IsCups; then
    if [ -n "$doenable" ]; then
      run_command enable $TTAPRINTER
      accept $TTAPRINTER
    else
      reject $TTAPRINTER
      disable $TTAPRINTER
    fi
  else
    case "$SCOARCH" in
    rsai*) # AIX
	if [ -n "$doenable" ]; then
	    qadm -U $TTAPRINTER
	else
	    qadm -D $TTAPRINTER
	fi
	;;
    i3li*|ppli*) # BSD-style printing: Linux
_lpc=''
for _dir in `echo "$PATH" | tr : ' '`; do
    case "${_dir}" in
    /*) [ -x "${_dir}/lpc" ] && { _lpc="${_dir}/lpc"; break; }
    esac
done

	[ -n "${_lpc}" ] && {
	    if [ -n "$doenable" ]; then
		${_lpc} enable $TTAPRINTER
		${_lpc} up $TTAPRINTER 2>/dev/null
	    else
		${_lpc} disable $TTAPRINTER
		${_lpc} down $TTAPRINTER 2>/dev/null
	    fi
	}
	;;
    *)  # other systems (not AIX or Tru64 or Linux)
	if [ -n "$doenable" ]; then
	run_command enable $TTAPRINTER
	accept $TTAPRINTER
    else
	reject $TTAPRINTER
	disable $TTAPRINTER
    fi
    esac
  fi
}

# get all the LPDEST printers from /etc/ttaprinter.conf, and select the ones
# we want
HostName=`${TTADIR}/bin/bin/ttahostname`
sed "/LPDEST=.*${HostName}/!d;s/.*LPDEST=//;s/[; 	].*//" "${CONF_FILE}" 2>/dev/null |
while read TTAPRINTER; do
    doit $TTAPRINTER
done

exit 0
