#! /bin/sh

clear

APS_BASEDIR=`pwd`
export APS_BASEDIR

if [ ! -f $APS_BASEDIR/SETUP ]; then
	echo "Please go into the directory, where SETUP resides !!!"
	echo "Start SETUP again out of that directory !!!"
	exit 1
fi

. $APS_BASEDIR/global/GLOBAL.sh 

clear
cat << !EOM

	---------------------------------------------------------------
	   A P S F I L T E R   V 4.8  *** The Unix Print Solution ***
	---------------------------------------------------------------

                     copyright Andreas Klemm 1993, 1994

                            andreas@knobel.gun.de

			Sun Sep 25 18:01:45 MET 1994

                   ---------------------------------------

			co-author Thomas Bueschgens 

			    sledge@hammer.oche.de

                         ---------------------------

			++++ INSTALLATION PROGRAM ++++
!EOM
echo ""; echo ""
echo "			 [ press return to continue ]"
read tmp

echo "------------------------------------------------------------------------"
echo "									      "
echo "This script automatically sets up a serial or parallel printer          "
echo "spool directories in $SPOOL will automatically be created as needed     "
echo "									      "
echo "------------------------------------------------------------------------"
echo "									      "

# check if we are root, otherwise exit ....
ME=`whoami`
if [ ! "$ME" = "root" ]; then
	echo "Must be root to install apsfilter !!!"
	exit 1
fi

# remove binaries a2ps and rewindstdin in $APS_BASEDIR/bin first
# some people told me, that FreeBSD binaries don't run on Linux ;-)
if [ -f $APS_BASEDIR/bin/a2ps ]; then
	rm -f $APS_BASEDIR/bin/a2ps
fi
if [ -f $APS_BASEDIR/bin/rewindstdin ]; then
	rm -f $APS_BASEDIR/bin/rewindstdin
fi

# moved filtersetup to the beginning to check if gs is installed
# on this site ... we need this later to warn user who want to install
# apsfilter for a non-PS printer ...

. $APS_BASEDIR/setup/filtersetup

# after that read the global/filters_found file
# automatically generated by filtersetup

. $FILTERS_FOUND

if [ ! "$HAVE_GS" = "True" ]; then
	echo ""
	echo "Huh ... I didn't find ghostscript on your system ..."
	sleep 2
	echo "I hope you are about to install apsfilter for a printer"
	echo "which is a real postscript printer, which doesn't need "
	echo "a postscript emulator like gs ... we'll see later ..."
	sleep 2
warn_about_missing_gs()
{
	# here we prepare the warning message :-)
	echo "As I see now, you are installing apsfilter for"
	echo "a printer, that needs gs as postscript emulator !!!"
	echo "Possibly we are in trouble here, since the "
	echo "filter scanning part of the installation didn't find"
	echo "gs on this system."
	echo "There are two way's out here:"
	echo "1.) You have installed gs on your system, but used an"
	echo "    extremely inofficial installation path for the"
	echo "    gs binary."
	echo "2.) You are about to install gs on your system later"
	echo ""
	echo "If 1.) is the case, then you need to edit"
	echo "$APS_BASEDIR/global/GLOBAL.sh. In this file you have"
	echo "to add your (one ore more) local paths to the PATH"
	echo "environment variable"
	echo "If 2.) is the case then compile and install gs"
	echo "In both cases you have to execute the utility"
	echo "$APS_BINDIR/setup/filtersetup again which creates"
	echo "a new database file $FILTERS_FOUND."
	echo "After that everything should be ok !"
	echo ""
}
fi


#
# 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 2>&1 )
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 ? $c"; 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

# make sure, that each line starts with a "#" so that old entries
# aren't active  - from line 1 to "# LABEL apsfilter"

cp /etc/printcap /tmp/printcap.$$
sed -e '1,/# LABEL apsfilter/s/^/#/' < /tmp/printcap.$$ > /etc/printcap
chmod 644 /etc/printcap
rm -f /tmp/printcap.$$

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 "? $c"; 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 "? $c"; read PRINTER
	;;

    2)	# deskjet

	# check if gs is installed ... otherwise warn warn warn !!!
	if [ ! "$HAVE_GS" = "True" ]; then
		warn_about_missing_gs
	fi

	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 ""
	echo $n "Press RETURN to continue ...$c"
	read tmp
	$PAGER $APS_BASEDIR/doc/deskjets 
	echo ""
	echo $n "Press RETURN to continue ...$c"
	read tmp
	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 ? $c"
	read PRINTER
	;;

    3)	# other, ps emul

	# check if gs is installed ... otherwise warn warn warn !!!
	if [ ! "$HAVE_GS" = "True" ]; then
		warn_about_missing_gs
	fi

	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 ljet4l 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 ? $c"; read answer
	case $answer in
		y|Y)	$PAGER $APS_BASEDIR/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 ljet4l 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 ? $c"
	read PRINTER
	;;
esac

SPOOLDIR=$SPOOL/$PRINTER

echo ""
echo "Is your printer a  (c)olor printer "
echo "             or a  (m)ono printer ?"
echo ""; echo $n "? $c"; read col

case $col in
	c|C)	COLOR="mono color" 	;;
	m|M)	COLOR="mono"		;;
	*)	echo "*** I assume you have a mono printer ***"
		COLOR="mono" 		;;
esac

echo ""
echo "What paper form do you use (i.e.: a3, a4, letter,...)"
echo "See $APS_BASEDIR/doc/paper for valid sizes..."
echo "Note: enter the paper form in lowercase letters: "
echo ""; echo $n "? $c"; read PAPER

echo "Do you have a (s)erial or a (p)arallel printer interface ?"
echo ""; echo $n "? $c"; 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 ?"
case $INTERFACE in
    parallel)
	case $SYSTEM in 
	    *BSD)
		echo "For FreeBSD Systems and possibly *BSD :)"
		echo "Device Name Major Minor  I/O address!"
	        echo "/dev/lpt0   16    0      0x3bc	(1. parallel device)"
	        echo "/dev/lpt1   16    1      0x378	(2. parallel device)"
	        echo "/dev/lpt2   16    2      0x278	(3. parallel device)"
		echo ""
		echo "type in the full path name, i.e.: /dev/lpt0"
		;;
	    *)
		echo "For Linux Systems with kernel <= 1.1.X :)"
		echo "Device Name Major Minor  I/O address!"
	        echo "/dev/lp0    6     0      0x3bc	(1. parallel device)"
	        echo "/dev/lp1    6     1      0x378	(2. parallel device)"
	        echo "/dev/lp2    6     2      0x278	(3. parallel device)"
		echo ""
		echo "type in the full path name, i.e.: /dev/lp0"
		;;
	esac
	;;
    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 "? $c"; read DEVICE

if [ "$INTERFACE" = "serial" ]; then
    echo "Enter the baudrate for your serial printer, i.e.: 9600"
    echo ""; echo $n "? $c"; read BAUD
    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

# create /usr/local/bin for a2ps and rewindstdin
mkdir -p /usr/local/bin > /dev/null 2>&1

echo $n "... setting up spooldir ... $c"
mkdir -p $SPOOLDIR > /dev/null
: > $SPOOLDIR/log
: > $SPOOLDIR/acct
chown -R $LP_OWNER $SPOOLDIR
chgrp -R $LP_GROUP $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", ...)
	i=1
	while ( grep lp${i} /etc/printcap > /dev/null 2>&1 )
	do
		echo "looking for printer names 'lpN' in use:"
		echo $n "lp$i $c"
		i=`expr $i + 1`
	done
	echo ""
	echo "Starting with lpN where N is $i"
	echo ""
	sleep 2
else
	# set auto printer name counter to 1
	i=1
	echo ""
	echo "Starting with lpN where N is $i"
	echo ""
	sleep 2
fi

METHOD="ascii auto"

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

#
# Important !!! Here is the global environment variable that
# gives apsfilter information, in which directory it resides
#
# But add this info only once for each system and not while adding a
# printer to a already running system.
#
if [ ! $ADD_PRINTER ]; then
	echo "# APS_BASEDIR:$APS_BASEDIR" >> /etc/printcap
	echo "#" >> /etc/printcap
fi

echo "** creating printcap entries in /etc/printcap..."
echo ""
sleep 2
for f in $COLOR
do
    for m in $METHOD
    do
	echo "#" >> /etc/printcap
	# default printer is no. 2
	if [ "$i" = "2" ]; then 
	    echo "lp|lp${i}|${PRINTER}-${PAPER}-$m-$f|${PRINTER} $m $f:\\" >> /etc/printcap
	    echo "--> lp|lp${i}|${PRINTER}-${PAPER}-$m-$f|${PRINTER} $m $f:\\"
	elif [ "$i" = "1" ]; then
	    echo "ascii|lp${i}|${PRINTER}-${PAPER}-$m-$f|${PRINTER} $m $f:\\" >> /etc/printcap
	    echo "--> ascii|lp${i}|${PRINTER}-${PAPER}-$m-$f|${PRINTER} $m $f:\\"
	else
	    echo "lp${i}|${PRINTER}-${PAPER}-$m-$f|${PRINTER} $m $f:\\" >> /etc/printcap
	    echo "--> lp${i}|${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=$APS_BASEDIR/filter/aps-$PRINTER-$PAPER-$m-$f:\\" >> /etc/printcap
	echo "	:mx#0:\\" >> /etc/printcap
	echo "	:sh:" >> /etc/printcap
	i=`expr $i + 1`
    done
done

# and now the last one, a raw filter
	echo "--> raw|lp${i}|${PRINTER}-${PAPER}-raw|${PRINTER} $m raw"
	echo "#" >> /etc/printcap
	echo "raw|lp${i}|${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=$APS_BASEDIR/filter/aps-$PRINTER-$PAPER-raw:\\" >> /etc/printcap
	echo "	:mx#0:\\" >> /etc/printcap
	echo "	:sh:" >> /etc/printcap
	i=`expr $i + 1`

$APS_BASEDIR/setup/mksymlinks $PRINTER $PAPER $col

sleep 2
cat - << !EOM
------------------------------------------------------------------------------
Fine, here we are ;-)

To complete apsfilter's installation, we have to perform some additional
tasks. Two - by apsfilter - heavily used and urgently needed utilities
have to be compiled and installed.

I'll try to do that task automatically for you ... in the background !
So look after the installation, if you can find the following programs
in:	- bin/rewindstdin
	- bin/a2ps

If the programs are not there, then wait just a momentm perhaps the
compile is still working ;-) You can check this using ps -ax.

If anything fails then change into apsfilter's source tree
and compile and install the utilities manually ... 
not much efforts are needed ... i.e.: type something like:

    cd src/a2ps-4.2-akl-new;	make clean install clean
    cd ../rewind-stdin;		make clean install clean
------------------------------------------------------------------------------
Press Return to continue ...
------------------------------------------------------------------------------
!EOM
read tmp

echo "Starting compilation in background ..."
setup/compile &
sleep 2

cat - << !EOM
------------------------------------------------------------------------------
Note: apsfilter is now customizeable, by a apsfilterrc file !

There is a *global* apsfilterrc file that will be installed 
by this script automatically:		/etc/apsfilterrc

This file may be copied by each individual user into his 
HOME directory under the name:		.apsfilterrc

Note the "." (dot) in front of the filename ! It's a good idea 
to browse through the global /etc/apsfilterrc file after installation !
------------------------------------------------------------------------------
Press Return to continue ...
------------------------------------------------------------------------------
!EOM
read tmp

echo "creating system default apsfilterrc file in /etc/apsfilter..."
cp template/apsfilterrc /etc/apsfilterrc

sleep 1
cat - << !EOM
------------------------------------------------------------------------------
My part of installing apsfilter should be finished now.... ;-)

Here an IMPORTANT LAST NOTE about REMOTE PRINTING, a thing that caused 
trouble for several people, that aren't familiar with lpd, input filters 
and remote printing.

Apsfilter is designed as a "lpd-input filter". This means - and this 
is really bad news - that apsfilter (as input filter) works only, if 
the printer, on that you want to print, is locally on that machine,
where you installed apsfilter !

So don't install apsfilter on a machine, where no printer is directly
connected !!!! This means also, that apsfilter won't work with those
magic print boxes (Net Cube, or such), that are used to connect a
printer somewhere in your office to the network.
------------------------------------------------------------------------------
Press Return to continue ...
------------------------------------------------------------------------------
!EOM
read tmp

sleep 1
cat - << !EOM
------------------------------------------------------------------------------
Design your network print environment so, that the machines with locally
connected printers are those machines, where you are able to install 
apsfilter....

Again the prerequisites are:
	- C-Compiler

And if you dosn't have a Postscript Printer:
	- ghostscript emulator and it's fonts (gs)

Note, that the newest ghostscript version 3.0 is a bit broken,
use 3.0.1 instead. BTW: ghostscript 2.6.1 Patchlevel 4 works
like a charm ... You might test your gs's Version by typing gs -h.
------------------------------------------------------------------------------
!EOM
sleep 2
cat - << !EOM
------------------------------------------------------------------------------
The very last step ... I'm looking again for installed filters ...
Important is, that a2ps  and rewindstdin are compiled and installed
in $APS_BASEDIR/bin...

Since compilation and installation of these programs runs in
background I possibly have to enter a while loop to wait, until
the binaries are compiled and installed succesfully ....

I'm waiting up to 60 seconds ...
!EOM
COUNT=0
while [ ! $COUNT -gt 60 \
	-a ! -f $APS_BASEDIR/bin/a2ps \
	-o ! -f $APS_BASEDIR/bin/rewindstdin ];
do
	echo $n ".$c"
	COUNT=`expr $COUNT + 1`
	sleep 1
done
echo $n "done.$c"
echo ""; echo ""

echo "by the way ... we are now ready to look for filters again ..."
echo "now we are able to find a2ps and rewindstdin, too ... ;-)"

. $APS_BASEDIR/setup/filtersetup

echo ""
echo "We made it !"
