#!/bin/sh
#  Copyright (c) 1999, 2020, Oracle and/or its affiliates.
#
#  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

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 [ -n "$doenable" ]; then
    run_command enable $TTAPRINTER
    ${TTADIR}/bin/bin/cups/cupsaccept $TTAPRINTER
  else
    ${TTADIR}/bin/bin/cups/cupsreject $TTAPRINTER
    disable $TTAPRINTER
  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
