#! /bin/sh

PATH=/bin:/usr/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/lib/apsfilter/bin

# setup.apsfilter - setup apsfilter / printcap / spooldirs

OLDCAP=/etc/printcap.old
SPOOL=/usr/spool
CRONTAB=/usr/spool/cron/crontabs/root
if [ -f /bin/less -o -f /usr/bin/less -o -f /usr/local/bin/less ]; then
	PAGER=less
else
	PAGER=more
fi

clear

echo "------------------------------------------------------------------------"
echo "**** Configure: setup apsfilter                         by Andreas Klemm"
echo "------------------------------------------------------------------------"
echo "									      "
echo "This script automatically sets up a serial or parallel printer          "
echo "spool directories in /usr/spool will be automatically created as needed "
echo "									      "
echo "------------------------------------------------------------------------"
echo "									      "

#
# 1st save printcap
#
if [ ! -f /etc/printcap.orig ]
then
    echo "copying original printcap to /etc/printcap.orig"
    cp /etc/printcap /etc/printcap.orig
else
    echo "good, you already saved your original printcap to /etc/printcap.orig"
    sleep 2
    echo "copying /etc/printcap to /etc/printcap.old"
    sleep 2
    cp /etc/printcap $OLDCAP
fi

if ( grep "# LABEL apsfilter" $OLDCAP >& /dev/null )
then
	echo ""
	echo "I found a label in you printcap, that indicates, that"
	echo "you already configured a printer with this script..."
	echo ""
	echo "Do you want to (a)dd another printer entry or"
	echo "            to (o)override the existing entries ?"
	echo ""; echo -n "a/o ? "; read answer
	if [ "$answer" = "o" -o "$answer" = "O" ]; then
	    echo "Ok, erasing old apsfilter entries..."
	    sed -e "/# LABEL apsfilter/,$ d" < $OLDCAP > /etc/printcap
	else
	    echo "Ok, adding another printer using apsfilter..."
	    ADD_PRINTER=1
	    sleep 2
	fi
fi

echo ""
echo "For which printer type do you want to setup apsfilter..."
echo ""
echo "	1 - for a Postscript printer"
echo "	2 - for HP Deskjets"
echo "	3 - for another printer who doesn't support the Postscript language"
echo ""; echo -n "? "; read answer

case $answer in
    1)	# PS printer
	echo "Very fine, you have a real PS printer...Please select a printer"
	echo "driver that fits your PS-printers resolution:"
	echo ""
	echo " [ PS_300dpi PS_400dpi PS_600dpi PS_800dpi ]"
	echo ""
	echo -n "? "; read PRINTER
	;;
    2)	# deskjet
	echo "You said, that you have a deskjet alike printer..."
	echo "Currently there are many deskjet drivers supported by"
	echo "ghostscript. Some of them are provided by Aladdin Enterprises"
	echo "with the help of users, who have the special printer hardware,"
	echo "and some drivers are contributions by users, which are"
	echo "not supported by Aladdin Enterprises, but included into gs(1)".
	echo "If you have a color deskjet printer then you also can select"
	echo "between different printing methods or special features."
	echo ""
	echo "Now you can browse through a tabular, that lists available"
	echo "drivers for deskjet printers (color and monochrome)."
	echo "  You can terminate browsing typing 'q' for quit ..."
	echo "  'Blank', 'j' or 'RETURN' scrolls forward, 'b' scrolls backward"
	echo "Keep the name of the driver in memory, you want to use"
	echo "you will be asked for that name, after you finished"
	echo "browsing through your document. "
	echo -n "				Press RETURN to continue"
	read tmp
	$PAGER /usr/lib/apsfilter/doc/deskjets 
	clear
	echo ""; echo ""
	echo "which printer driver do you want to use ?"
	echo "------------------------------------------------"
	echo "cdjmono           *1	300, 300,   1, 4, 0, 1"
	echo "cdeskjet          *1	300, 300,   3, 4, 2, 1"
	echo "cdjcolor          *1	300, 300,  24, 4, 2, 1"
	echo "cdj500            *1	300, 300, bpp, 4, 2, 1"
	echo "cdj550            *1	300, 300, bpp, 0, 2, 1"
	echo "deskjet           +	300, 300,   1, -  -  -"
	echo "djet500           +	300, 300,   1, -  -  -"
	echo "djet500c          *2	300, 300,   3, -, 1, 1"
	echo "------------------------------------------------"
	echo ""
	echo "Perhaps good choices are:"
	echo "For a HP Deskjet  (b&w):    deskjet"
	echo "For a Deskjet 500 (b&w):    djet500"
	echo "For a color Deskjet:        cdeskjet"
	echo ""
	echo -n "What driver do you want for your deskjet ? "
	read PRINTER
	;;
    3)	# other, ps emul
	echo "Select one of the following gs(1) printer driver ..."
	echo ""
	echo " [ appledmp bj10e bj200 declj250 eps9high epson epsonc escp2"
	echo "   ibmpro jetp3852 la50 la75 laserjet lbp8 lj250 ljet2p ljet3"
	echo "   ljet4 ljetplus ln03 m8510 necp6 nwp533 oki182 paintjet pj"
	echo "   pjxl pjxl300 r4081 sparc t4693d2 t4693d4 t4693d8 tek4696  ]"
	echo ""
	echo "Do you need a more verbose description of the printer "
	echo -n "drivers ? y/n ? "; read answer
	case $answer in
		y|Y)	$PAGER /usr/lib/apsfilter/doc/printer ;;
	esac
	clear
	echo " [ appledmp bj10e bj200 declj250 eps9high epson epsonc escp2"
	echo "   ibmpro jetp3852 la50 la75 laserjet lbp8 lj250 ljet2p ljet3"
	echo "   ljet4 ljetplus ln03 m8510 necp6 nwp533 oki182 paintjet pj"
	echo "   pjxl pjxl300 r4081 sparc t4693d2 t4693d4 t4693d8 tek4696  ]"
	echo ""
	echo -n "Which printer driver do you want ? "
	read PRINTER
	;;
esac

SPOOLDIR=$SPOOL/$PRINTER

echo ""
echo "What paperformat do you use (i.e.: a3, a4, letter,...)"
echo "See /usr/lib/apsfilter/doc/paper for valid sizes..."
echo "Note: enter the papferpormat in lowercase letters: "
echo ""; echo -n "? "; read PAPER

echo "Do you have a (s)erial or a (p)arallel printer interface ?"
echo ""; echo -n "? "; read tmp
case $tmp in
	s|S|y|Y)	INTERFACE=serial	;;
	p|P|n|N)	INTERFACE=parallel	;;
	*)		echo "*** I guess you have a parallel printer ***"
			INTERFACE=parallel	;;
esac

echo "What's the device name for your $INTERFACE printer interface ?"
echo "For Linux Sytems (Unix on PC's):"
echo "     Device Name Major Minor   I/O address!"
case $INTERFACE in
    parallel)
	echo "     /dev/lp0    6     0       0x3bc	(parallel device)"
	echo "     /dev/lp1    6     1       0x378	(parallel device)"
	echo "     /dev/lp2    6     2       0x278	(parallel device)"
	echo ""
	echo "type in the full path name, i.e.: /dev/lp0"
	;;
    serial)
	echo "     /dev/ttyS0  4     64      0x278	(serial device COM1) [mouse]"
	echo "     /dev/ttyS1  4     65      0x278	(serial device COM2) [modem]"
	echo "     /dev/ttyS2  4     66      0x278	(serial device COM3) [printer]"
	echo ""
	echo "type in the full path name, i.e.: /dev/ttyS2"
	;;
esac
echo ""; echo -n "? "; read DEVICE

if [ "$INTERFACE" = "serial" ]; then
    echo "Enter the baudrate for your serial printer, i.e.: 9600"
    echo ""; echo -n "? "; read DEVICE
    echo ""
    echo ""
    echo "You said, you have a serial printer. Then its necessary to fine-tune"
    echo "the parameters of the serial interface in the /etc/printcap file"
    echo "after this 'rough' pre-installation (ms=....)."
    echo "A description of valid tty parameters can be found "
    echo "in the stty(1L) manual page, i.e.: man stty"
    echo "If you run into trouble with your interface, then please don't blame me ;-)"
    echo ""
    echo "Please read the printing howto, which explains everything in detail !!!!"
    echo ""
    echo "If you have the Linux Slackware distribution then look into:"
    echo ""
    echo "	/usr/doc/faq/howto/Printing-HOWTO"
    echo ""
    echo "press RETURN to continue"
    read tmp
fi

echo "The name of your printers spool directory will be:"
echo ""
echo "		$SPOOLDIR"
echo ""
echo "The spool directory will be created automatically, if it's missing"
sleep 2

echo -n "... setting up spooldir ..."
mkdir -p $SPOOLDIR > /dev/null
: > $SPOOLDIR/log
: > $SPOOLDIR/acct
chown -R root.lp $SPOOLDIR
echo ""

if [ $ADD_PRINTER ]
then
	# we add a new printer, must compute, 
	# which number is the next unused
	# we start checking with n=1 (looking for "lp1", "lp2", ...)
	n=1
	while ( grep lp${n} /etc/printcap >& /dev/null )
	do
		echo "looking for printer names 'lpN' in use:"
		echo -n "lp$n "
		n=`expr $n + 1`
	done
	echo ""
	echo "Starting with lpN where N is $n"
	echo ""
	sleep 2
else
	# set auto printer name counter to 1
	n=1
	echo ""
	echo "Starting with lpN where N is $n"
	echo ""
	sleep 2
fi

METHOD="ascii auto"
FEATURE="1 1n 2 2n"

echo "** making apsfilter label in /etc/printcap..."
sleep 2
echo "# LABEL apsfilter" >> /etc/printcap
echo "# apsfilter setup `date`" >> /etc/printcap
echo "#" >> /etc/printcap

echo "** creating printcap entries in /etc/printcap..."
echo ""
sleep 2
for m in $METHOD
do
    for f in $FEATURE
    do
	echo "#" >> /etc/printcap
	# default printer is no. 7
	if [ "$n" = "7" ]; then 
	    echo "lp|lp${n}|${PRINTER}-${PAPER}-$m-$f|${PRINTER} $m $f:\\" >> /etc/printcap
	    echo "--> lp|lp${n}|${PRINTER}-${PAPER}-$m-$f|${PRINTER} $m $f:\\"
	elif [ "$n" = "3" ]; then
	    echo "ascii|lp${n}|${PRINTER}-${PAPER}-$m-$f|${PRINTER} $m $f:\\" >> /etc/printcap
	    echo "--> ascii|lp${n}|${PRINTER}-${PAPER}-$m-$f|${PRINTER} $m $f:\\"
	else
	    echo "lp${n}|${PRINTER}-${PAPER}-$m-$f|${PRINTER} $m $f:\\" >> /etc/printcap
	    echo "--> lp${n}|${PRINTER}-${PAPER}-$m-$f|${PRINTER} $m $f:\\"
	fi
	echo "	:lp=${DEVICE}:\\" >> /etc/printcap
	if [ "$INTERFACE" = "serial" ]; then
                echo "	:br#${BAUD}:\\" >> /etc/printcap
		echo "	:ms=ixon,-imaxbel,-ixany,-ixoff,-crtscts:\\" >> /etc/printcap
	fi
	echo "	:sd=$SPOOLDIR:\\" >> /etc/printcap
	echo "	:lf=$SPOOLDIR/log:\\" >> /etc/printcap
	echo "	:af=$SPOOLDIR/acct:\\" >> /etc/printcap
	echo "	:if=/usr/lib/apsfilter/filter/aps-$PRINTER-$PAPER-$m-$f:\\" >> /etc/printcap
	echo "	:mx#0:\\" >> /etc/printcap
	echo "	:sh:" >> /etc/printcap
	n=`expr $n + 1`
    done
done

# and now the last one, a raw filter
	echo "--> raw|lp${n}|${PRINTER}-${PAPER}-raw|${PRINTER} $m raw"
	echo "#" >> /etc/printcap
	echo "raw|lp${n}|${PRINTER}-${PAPER}-raw|${PRINTER} $m raw:\\" >> /etc/printcap
	echo "	:lp=${DEVICE}:\\" >> /etc/printcap
	if [ "$INTERFACE" = "serial" ]; then
                echo "	:br#${BAUD}:\\" >> /etc/printcap
		echo "	:ms=ixon,-imaxbel,-ixany,-ixoff,-crtscts:\\" >> /etc/printcap
	fi
	echo "	:sd=$SPOOLDIR:\\" >> /etc/printcap
	echo "	:lf=$SPOOLDIR/log:\\" >> /etc/printcap
	echo "	:af=$SPOOLDIR/acct:\\" >> /etc/printcap
	echo "	:if=/usr/lib/apsfilter/filter/aps-$PRINTER-$PAPER-raw:\\" >> /etc/printcap
	echo "	:mx#0:\\" >> /etc/printcap
	echo "	:sh:" >> /etc/printcap
	n=`expr $n + 1`

/usr/lib/apsfilter/bin/mksymlinks $PRINTER $PAPER

# Create the search-database for dvips now and try to install it into
# root's crontab file.

echo ""
echo "Search-database for DVIPS"
echo "-------------------------"
echo ""
echo "You should now be able to print dvi-files which include screenshots"
echo "or other ps-files, if the files are in dvips'/apsfilters search-"
echo "database (/usr/lib/apsfilter/bin/database.*)..."
sleep 2
echo ""
echo "When apsfilter is doing it's job printing dvi files, then the"
echo "TEXINPUTS variable is automatically set using the directory"
echo "entries in the search database ;-)"
sleep 2
echo ""
echo -n "Creating dvips' search database /usr/lib/apsfilter/bin/database.var..."
sleep 2

# Call the mkdatabase-shellscript to create an initial database
# for dvips' special feature to include ps files when printing tex dvi files
/usr/lib/apsfilter/bin/mkdatabase

# The regular update is VERY important, especially while creating new
# TeX-Documents containing ps-files (from xfig e.g.)
if ( ! grep mkdatabase $CRONTAB > /dev/null); then
    echo ""
    echo "It's very important to update dvips'/apsfilters search database"
    echo "regulary via cron(8) otherwise your printed dvi file may be"
    echo "incomplete when using dvips' special features to include ps"
    echo "pictures or such files ... !"
    echo ""
    echo "Am I allowed to append a line to root's crontab file"
    echo "that manages the automatic update of the search database ?"
    echo ""
    echo "[if y: you can select between different update intervals later !]"
    echo ""
    echo "y/n ?"; read answer
    echo ""
    if [ "$answer" = "y" ]; then
        echo "ok, setting up roots crontab file..."
        sleep 2
	echo "What update interval do you need for your dvips database ?"
	echo ""
	echo "	...1...  every 10 minutes"
	echo "	...2...  every 20 minutes"
	echo "	...3...  every 30 minutes"
	echo "	...4...  hourly"
	echo ""
	echo "choose 1/2/3 ?"
	read answer
	case $answer in
	  1) echo "# --- apsfilter / mkdatabase ---" \
		>> $CRONTAB
	     echo "0,10,20,30,40,50 * * * * /usr/lib/apsfilter/bin/mkdatabase > /dev/null" \
		>> $CRONTAB
	     ;;
	  2) echo "# --- apsfilter / mkdatabase ---" \
		>> $CRONTAB
	     echo "0,20,40 * * * * /usr/lib/apsfilter/bin/mkdatabase > /dev/null" \
		>> $CRONTAB
	     ;;
	  3) echo "# --- apsfilter / mkdatabase ---" \
		>> $CRONTAB
	     echo "0,30 * * * * /usr/lib/apsfilter/bin/mkdatabase > /dev/null" \
		>> $CRONTAB
	     ;;
	  4|*) echo "# --- apsfilter / mkdatabase ---" \
		>> $CRONTAB
	     echo "0 * * * * /usr/lib/apsfilter/bin/mkdatabase > /dev/null" \
		>> $CRONTAB
	     ;;
	esac
        echo ""
        echo "You have to kill and re- start cron to make the new "
        echo "entries active or simply reboot your system"
    else
        echo "Ok, if you don't want me to fiddle around in your"
        echo "systems files, then you're on your own ;-)"
        echo "Examine the file mkdatabase.cron in apsfilter's bin-dir,"
        echo "what has to be included into root's crontab file ..."
    fi
    echo ""; sleep 2
else
    echo ""
    echo "Excellent !"
    echo "mkdatabase is already installed in root's crontab file..."
fi

echo ""
echo "Installation of apsfilter done."
