#!/bin/sh
#
# $Id: mit-lprng-config,v 1.5 2004/06/17 19:57:13 jdreed Exp jdreed $
#
# Script to set the MIT default printer

usage() {
    echo "Usage: $0 [-n | -a] [-p <printername> | -c]"
    exit 1
}

case `id` in
"uid=0("*)
  ;;
*)
  echo "You are not root.  This script must be run as root."
  exit 1
  ;;
esac

CONFIG=/etc/mit-lprng.conf

source $CONFIG

opt=$1

case "$opt" in
  -n)
    FORCEAUTH=false
    ;;
  -p)
    printer=$2
    if [ -z $printer ]; then
	usage
    fi
    pcap=""
    if [ -x /usr/bin/hesinfo ]; then
        pcap=`hesinfo $printer pcap 2>/dev/null`
    else
        PCAP=`host -t txt $printer.pcap.ns.athena.mit.edu | sed -e 's/.*\"\(.*\)\".*/\1/'`
        # Some versions of host(1) don't set their exit status correctly.
        echo $PCAP | grep -q NXDOMAIN
        if [ $? != 0 ]; then
	    pcap=$PCAP
	fi
    fi
    if [ -z $pcap ]; then
	echo "ERROR: $printer does not appear to have a Hesiod entry"
	exit 1
    else
	DEFPRINTER=$printer
	echo "Default printer set to $printer"
    fi
    ;;
  -a)
    FORCEAUTH=true
    ;;
  -c)
    DEFPRINTER=""
    ;;
  *)
    usage
    ;;
esac


mv $CONFIG $CONFIG.bak
cat > /etc/mit-lprng.conf <<EOF
# Config File for MIT extensions to LPRng
#
# Default MIT printer, if any
DEFPRINTER=$DEFPRINTER
# Force authentication? (off-campus users may need this)
FORCEAUTH=$FORCEAUTH
EOF


exit 0;

