#!/usr/bin/zsh
# (c) Copyright 1997, Apple Computer, Inc. All Rights Reserved
# This utility is supplied under the license shipped with the Rhapsody Developer Release.
# This software is unsupported. Use at your own risk.
echo "Type CTRL-c at any time to abort."
echo 
if ( test "`/usr/bin/whoami`" != "root" ) then
	echo This script must be run as root.
	exit 1
fi
if (test "`/usr/bin/uname`" != "Rhapsody") then
	echo This script must be run on a Rhapsody machine.
	exit 1
fi
echo This script assumes you have already configured your printer\'s IP address.
echo 
read PRINTER\?"Name of printer to add:  "
if ( niutil -read . /printers/$PRINTER > /dev/null)
then
	echo Printer $PRINTER already exists!!!
	echo 'Type CTRL-c to exit, <RETURN> to continue.'
	read
fi
echo 
echo Adding $PRINTER
echo 

echo Trying to find $PRINTER
echo 'This may take a minute (and generate some "funny" messages).'
if !(/sbin/ping -c 2 $PRINTER > /dev/null)
then
	echo 
	echo $PRINTER not found.
	read PRINTER_IP\?"IP address for $PRINTER:  "
	echo Creating NetInfo \'machines\' entry for $PRINTER with IP address $PRINTER_IP
	niutil -create . /machines/$PRINTER 
	niutil -createprop . /machines/$PRINTER ip_address $PRINTER_IP
	rm /tmp/findprinter.txt
fi

echo 
echo I need to know what type of printer this is. 
echo If you enter nothing, I will assume it is an Apple LaserWriter. 
echo That would probably be OK, but it will not accurately reflect your 
echo printers capabilities. 
echo 
echo An easy way to do this is to browse the directory
echo /NextLibrary/PrinterTypes/English.lproj.  If you find the ppd that
echo matches your printer type, just drag it to this window and the name
echo will be pasted here.  Do not worry about the path, the script will 
echo take care that.
open /NextLibrary/PrinterTypes/English.lproj
echo
echo Or you can just type the name of the PPD file if you know it already,
echo though I will not check it for you.
echo
read TYPE\?"Type for $PRINTER:  "
if (test -z $TYPE)
then
	TYPE=Apple_LaserWriter
else
	TYPE=$TYPE:r:t
fi

if (! test -e /NextLibrary/PrinterTypes/English.lproj/$TYPE.ppd)
then
	echo
	echo Type $TYPE not found.  It is probably not a good idea to proceed.
	echo
fi

echo
echo OK.  I will set up $PRINTER as a $TYPE.
echo

echo
read NOTE\?"Short note for printer (location):  "
echo

echo I will now create a NetInfo entry for $PRINTER in \'/printers\' 
echo Type CTRL-c if you are not sure you want to do this.
echo 'Type CTRL-c to exit, <RETURN> to continue.'
read

mkdir -p /var/spool/NeXT
niutil -create . /printers/$PRINTER 
niutil -createprop . /printers/$PRINTER name $PRINTER
niutil -createprop . /printers/$PRINTER lp
niutil -createprop . /printers/$PRINTER lo lock
niutil -createprop . /printers/$PRINTER rm $PRINTER
niutil -createprop . /printers/$PRINTER rp $PRINTER
niutil -createprop . /printers/$PRINTER sd /var/spool/NeXT/$PRINTER
niutil -createprop . /printers/$PRINTER ty $TYPE
niutil -createprop . /printers/$PRINTER _nxfinalform
niutil -createprop . /printers/$PRINTER note $NOTE

