#! /bin/sh
# -*- sh -*-

#
# New SETUP tool for apsfilter by Andreas Klemm <andreas@klemm.gtn.com>
#

# $Id: SETUP,v 1.5 1999/03/01 16:09:32 andreas Exp $

#
# exit status 0 on successfull printer installation
# exit status 1 indicates failures or if the user simply quit the program
#

#
# History:
#
# - first introduced with apsfilter 4.9.4
#

#
# read old settings... ;-)
#
if [ -f SETUP.cfg ]; then
	. ./SETUP.cfg
fi

################################################################################
# Check if we have root permissions
################################################################################
do_check_root()
{
	case `whoami` in
		root)	# ok, we are root
			;;
		*)	# not root, exit
			echo "SETUP: error: must be root to install apsfilter"
			exit 1
			;;
	esac
}

################################################################################
# - SETUP script has to be called from within the apsfilter
#   installation directory
# - if wrong path, be tolerant and correct this little mistake
################################################################################
do_check_path()
{
	HERE=`pwd`
	if [ -f $HERE/SETUP ]; then
		# good, we stay in the apsfilter libdir
		APS_BASEDIR=$HERE
	else
		# not in apsfilter libdir, but actually called ;-)
		# - with complete (absolute) path ?
		#   then the first character of $0 is '/'
		# - with relative path ?
		#   then the first character of $0 something else
		FIRST_CHAR=`echo $0 | cut -c 1`
		case $FIRST_CHAR in
			/)	# SETUP was called with absolute path
				# then APS_BASEDIR is `dirname $0`
				APS_BASEDIR=`dirname $0`
				;;
			*)	# SETUP was called with relative path
				# then APS_BASEDIR is $HERE + '/' + `dirname $0`
				APS_BASEDIR=$HERE/`dirname $0`
				;;
		esac
	fi
	cd $APS_BASEDIR
	export APS_BASEDIR
}

################################################################################
# initialization
# - call filtersetup
# - read filters found
################################################################################
do_initialize()
{
	# moved filtersetup to the beginning to be able to check later
	# if gs is installed or not.

	. $APS_BASEDIR/setup/filtersetup

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

	. $FILTERS_FOUND

	# On Free-, Open-, NetBSD systems we have a ports collection
	# Some setup steps doesn't have to be performed there
        # This package ported to Linux, 3/1999 pjv

	case `uname -s` in
		*Linux*)
			# Binary Compatibility I hope
			PORTS_COLLECTION=True
			;;
	esac
}

################################################################################
# START SEQUENCE
# - introduce apsfilter, version, copyright
# - initialize global variables via $APS_BASEDIR/global/GLOBAL.sh script
################################################################################
do_start()
{
	#
	# read some paths ....
	#
	. $APS_BASEDIR/global/GLOBAL.sh 
	#
	# clear screen
	#
	clear
	cat << !EOM

        ================================================================
         A P S F I L T E R  V 5.0.1     *** The Unix Print Solution ***
        ================================================================

         copyright Andreas Klemm 1993, 1994, 1995, 1996, 1997, 1998 1999

                              andreas@klemm.gtn.com

                           Mo 1 Mr 1999 17:09:01 CET

                    ----------------------------------------
                      for Unix systems with BSD compatible 
                          line printer scheduler (lpd)
			      or with LPRng (new!)
                       FreeBSD / NetBSD / OpenBSD / BSDI
                        Linux and other flavours of Unix
                    ----------------------------------------

			++++ INSTALLATION PROGRAM ++++

                        [ press <RETURN> to continue ]

!EOM
	read answer
	clear
	cat << !EOM

        ================================================================
          A P S F I L T E R   S E T U P               -- PROGRAM INFO --
        ================================================================

        This SETUP script supports the user/administrator by

		- configuring serial and parallel printers
		- creating printer spool directories as needed
		- auto-creation of /etc/printcap config-file
		  for apsfilter (add / overwrite printer)
		- setting up apsfilter (which gs driver to use,
		  paper size, print resolution and color depth)
		- printing a test page (settings may be modified
		  until successfull ouput of a testpage)
		- saves some SETUP settings in /etc/apsfilterrc

        it does not:

		- complete printer management in /etc/printcap
		  (delete/modify printer)
        ----------------------------------------------------------------

                        [ press <RETURN> to continue ]
!EOM
	read answer
}

################################################################################
# if user selects a "not PS printer"
# and if ghostscript is not installed
# (needed by do_choose_driver)
################################################################################
warn_about_missing_gs()
{
clear
cat << !EOM

    ================================================================
    A P S F I L T E R   S E T U P                      -- WARNING --
    ================================================================

    Attention 
    =========
    As I see now, you are installing apsfilter for a printer, that needs
    gs (ghostscript) as postscript emulator, but apsfilter's SETUP was
    unable to locate the ghostscript emulator.

    One reason might be, that ghostscript is installed in an unusual
    directory and SETUP was unable to auto-detect this path.

    Then please add this path in
    $APS_BASEDIR/global/GLOBAL.sh and run SETUP again.

    Otherwise you have to get the ghostscript sources and fonts
    and install ghostscript by hand and run setup again afterwards.

!EOM
    echo "[ press <RETURN> to continue and exit SETUP ]"
    echo ""
    exit 1
}

################################################################################
# Ghostscipt Driver Selection or
# Postscript Printer Resolution Selection
# Loop Until Done !
################################################################################
do_printer_selection()
{
clear
cat << !EOM

        ----------------------------------------------------------------
        A P S F I L T E R   S E T U P     -- Printer Driver Selection --
        ----------------------------------------------------------------

	Please select a category that contains your printer driver

	1 - for Apple and Apple compatible printer
	2 - for Canon and Canon compatible printer
	3 - for Epson and ESC/2 compatible printer
	4 - for HP Deskjets and DeskJet compatible printer
	5 - for HP LaserJets and LaserJet compatible printer
	6 - for HP Paint-/DesignJets and compatible printer
	7 - for DEC, IBM, NEC, Okidata and compatible printer
	8 - for another printer who doesn't support the Postscript language
	9 - for a _true_ Postscript printer"

	Hint: feel free to browse through all categories, by simply not
	selecting any driver (i.e.: by hitting only the <RETURN> key).

!EOM
echo $n "Your choice ? $c"; read answer

case $answer in

    1)  # Apple's
	# check if gs is installed ... otherwise warn warn warn !!!
	if [ ! "$HAVE_GS" = "True" ]; then
		warn_about_missing_gs
	fi
cat << !EOM
	-----------------------------------------------------------------
	List of ghostscript5.03 APPLE printer driver
	-----------------------------------------------------------------
	(1)	appledmp  - Apple Dot Matrix Printer / Imagewriter
	(2)	bjc600    - Apple StyleWriter 2x00
	(3)	iwhi      - Apple ImageWriter, high resolution mode
	(4)	iwlo      - Apple ImageWriter, low resolution mode
	(5)	iwlq      - Apple ImageWriter LQ in 320x216 dpi mode
	-----------------------------------------------------------------
!EOM
	echo "Please enter the printer driver of your choice"
	echo "or type <RETURN> to choose another category/driver:"
	echo $n "Your choice ? $c"; read answer
	case $answer in
		1)	PRINTER=appledmp	;;
		2)	PRINTER=bjc600		;;
		3)	PRINTER=iwhi		;;
		4)	PRINTER=iwlo		;;
		5)	PRINTER=iwlq		;;
		*)	PRINTER=""		;;
	esac
	;;

    2)  # Canon
	# check if gs is installed ... otherwise warn warn warn !!!
	if [ ! "$HAVE_GS" = "True" ]; then
		warn_about_missing_gs
	fi
cat << !EOM
	-----------------------------------------------------------------
	List of ghostscript5.03 CANON printer driver
	-----------------------------------------------------------------
	(1)	bj10e    - Canon BubbleJet BJ10e
	(2)	bj200    - Canon BubbleJet BJ200
	(3)	bjc600   - Canon Color BubbleJet BJC-600/4000/70
	(4)	bjc800   - Canon Color BubbleJet BJC-800
	(5)	lbp8     - Canon LBP-8II laser printer
	(6)	lips3    - Canon LIPS II laser printer in English/CaPSL mode
	(7)	lq850    - Canon BubbleJet BJ300 in Epson LQ850 emulation
	-----------------------------------------------------------------
!EOM
	echo "Please enter the printer driver of your choice"
	echo "or type <RETURN> to choose another category/driver:"
	echo $n "Your choice ? $c"; read answer
	case $answer in
		1)	PRINTER=bj10e		;;
		2)	PRINTER=bj200		;;
		3)	PRINTER=bjc600		;;
		4)	PRINTER=bjc800		;;
		5)	PRINTER=lbp8		;;
		6)	PRINTER=lips3		;;
		7)	PRINTER=lq850		;;
		*)	PRINTER=""		;;
	esac
	;;

    3)  # Epson and ESC/2 compatible printer
	# check if gs is installed ... otherwise warn warn warn !!!
	if [ ! "$HAVE_GS" = "True" ]; then
		warn_about_missing_gs
	fi
cat << !EOM
	-----------------------------------------------------------------
	List of ghostscript5.03 EPSON + ESC/2 printer driver
	-----------------------------------------------------------------
	(1)	ap3250   - Epson AP3250
	(2)	epson    - Epson-compatible dot matrix, 9/24-pin
	(3)	eps9mid  - Epson-compatible 9-pin, medium resol
	(4)	eps9high - Epson-compatible 9-pin, high resol
	(5)	epsonc   - Epson LQ-2550 and Jujitsu 3400/2400/1200 color
	(6)	lp8000   - Epson LP-8000 line printer
	(7)	lq850    - Epson LQ-850 (360x360), and Canon BJ300 in LQ850emul
	(8)	st800    - Epson Stylus 800
	(9)	stcolor  - Epson Stylus color (PRO)
	-----------------------------------------------------------------
!EOM
	echo "Please enter the printer driver of your choice"
	echo "or type <RETURN> to choose another category/driver:"
	echo $n "Your choice ? $c"; read answer
	case $answer in
		1)	PRINTER=ap3250		;;
		2)	PRINTER=epson		;;
		3)	PRINTER=eps9mid		;;
		4)	PRINTER=eps9high	;;
		5)	PRINTER=epsonc		;;
		6)	PRINTER=lp8000		;;
		7)	PRINTER=lq850		;;
		8)	PRINTER=st800		;;
		9)	PRINTER=stcolor		;;
		*)	PRINTER=""		;;
	esac
	;;

    4)	# HP DeskJet's
	# check if gs is installed ... otherwise warn warn warn !!!
	if [ ! "$HAVE_GS" = "True" ]; then
		warn_about_missing_gs
	fi
cat << !EOM
	-----------------------------------------------------------------
	List of ghostscript5.03 HP DeskJet printer driver
	-----------------------------------------------------------------
	(1)	deskjet  - HP DeskJet and DeskJet Plus
	(2)	djet500  - HP DeskJet 500
	(3)	djet500c - HP DeskJet 500C alternate driver (not for 550/560C)
	(4)	cdeskjet - HP DeskJet 500C with 3 bit/pixel color
	(5)	cdjcolor - HP DeskJet 500/540C with 24 bit/pixel color and
	        high-quality color (Floyd-Steinberg) dithering
	(6)	cdjmono  - HP DeskJet 500/510/520 + 540C (b&w only)
	(7)	cdj500   - HP DeskJet 500C (same as cdjcolor)
	(8)	cdj550   - HP DeskJet 550C/560C
	(9)	cdj670   - HP DeskJet 670/690
	(10)	cdj850   - HP DeskJet 850/855/870/1100
	(11)	cdj890   - HP DeskJet 890
	(12)	cdj1600  - HP DeskJet 1600
	-----------------------------------------------------------------
!EOM
	echo "Please enter the printer driver of your choice"
	echo "or type <RETURN> to choose another category/driver:"
	echo $n "Your choice ? $c"; read answer
	case $answer in
		1)	PRINTER=deskjet		;;
		2)	PRINTER=djet500		;;
		3)	PRINTER=djet500c	;;
		4)	PRINTER=cdeskjet	;;
		5)	PRINTER=cdjcolor	;;
		6)	PRINTER=cdjmono		;;
		7)	PRINTER=cdj500		;;
		8)	PRINTER=cdj550		;;
		9)	PRINTER=cdj670		;;
		10)	PRINTER=cdj850		;;
		11)	PRINTER=cdj890		;;
		12)	PRINTER=cdj1600		;;
		*)	PRINTER=""		;;
	esac
	;;

    5)  # HP LaserJets
	# check if gs is installed ... otherwise warn warn warn !!!
	if [ ! "$HAVE_GS" = "True" ]; then
		warn_about_missing_gs
	fi
cat << !EOM
	-----------------------------------------------------------------
	List of ghostscript5.03 HP LaserJet printer driver
	-----------------------------------------------------------------
	(1)	laserjet - HP LaserJet
	(2)	ljet2p   - HP LaserJet IId/IIp/III with TIFF compression
	(3)	ljet3    - HP LaserJet III with Delta Row compression
	(4)	ljet3d   - HP LaserJet IIID with duplex capability
	(5)	ljet4    - HP LaserJet 4 (defaults to 600dpi)
	(6)	lj4dith  - HP LaserJet 4 with Floyd-Steinberg dithering
	(7)	ljetplus - HP LaserJet Plus
	(8)	lj5mono  - HP LaserJet 5 and 6 family (PCL5 / PCL XL), bitmap
	(9)	lj5gray  - HP LaserJet 5 and 6 family, gray-scale bitmap
	-----------------------------------------------------------------
!EOM
	echo "Please enter the printer driver of your choice"
	echo "or type <RETURN> to choose another category/driver:"
	echo $n "Your choice ? $c"; read answer
	case $answer in
		1)	PRINTER=laserjet	;;
		2)	PRINTER=ljet2p		;;
		3)	PRINTER=ljet3		;;
		4)	PRINTER=ljet3d		;;
		5)	PRINTER=ljet4		;;
		6)	PRINTER=lj4dith		;;
		7)	PRINTER=ljetplus	;;
		8)	PRINTER=lj5mono		;;
		9)	PRINTER=lj5gray		;;
		*)	PRINTER=""		;;
	esac
	;;

    6)  # HP PaintJets
	# check if gs is installed ... otherwise warn warn warn !!!
	if [ ! "$HAVE_GS" = "True" ]; then
		warn_about_missing_gs
	fi
cat << !EOM
	-----------------------------------------------------------------
	List of ghostscript5.03 HP Paint-/DesignJet printer driver
	-----------------------------------------------------------------
	(1)	paintjet - alternate HP PaintJet color printer
	(2)	pj       - HP PaintJet XL driver
	(3)	pjetxl   - alternate HP PaintJet XL driver
        (4)	pjxl     - HP PaintJet XL color printer
	(5)	pjxl300  - HP PaintJet XL300 color printer (ok for P.Jet 1200C)
	(6)	dnj650c  - HP DesignJet 650C
	-----------------------------------------------------------------
!EOM
	echo "Please enter the printer driver of your choice"
	echo "or type <RETURN> to choose another category/driver:"
	echo $n "Your choice ? $c"; read answer
	case $answer in
		1)	PRINTER=paintjet	;;
		2)	PRINTER=pj		;;
		3)	PRINTER=pjetxl		;;
		4)	PRINTER=pjxl		;;
		5)	PRINTER=pjxl300		;;
		6)	PRINTER=dnj650c		;;
		*)	PRINTER=""		;;
	esac
	;;

    7)  # DEC, IBM, NEC, Okidata
	# check if gs is installed ... otherwise warn warn warn !!!
	if [ ! "$HAVE_GS" = "True" ]; then
		warn_about_missing_gs
	fi
cat << !EOM
	-----------------------------------------------------------------
	List of gs-5.03 DEC, IBM, NEC, Okidata printer driver
	-----------------------------------------------------------------
	declj250 - CalComp Raster Format
	ibmpro   - IBM 9-pin Proprinter
	jetp3852 - IBM JetPrinter ink-jet color (Model #3852)
	la50     - DEC LA50 printer
	la70     - DEC LA70 printer
	la70t    - DEC LA70 printer with low resol, text enhancement
	la75     - DEC LA75 printer
	la75plus - DEC LA75plus printer
	ln03     - DEC LN03 printer
	lj250    - DEC LJ250 Companion color printer
	necp6    - NEC P6/P6+/P60, 360x360 DPI resol
	oki182   - Okidata MicroLine 182
	okiibm   - Okidata MicroLine IBM-compatible printers
	-----------------------------------------------------------------
!EOM
	echo "Please enter the printer driver of your choice"
	echo "or type <RETURN> to choose another category/driver:"
	echo $n "Your choice ? $c"; read answer
	case $answer in
		1)	PRINTER=declj250	;;
		2)	PRINTER=ibmpro		;;
		3)	PRINTER=jetp3852	;;
		4)	PRINTER=la50		;;
		5)	PRINTER=la70		;;
		6)	PRINTER=la70t		;;
		7)	PRINTER=la75		;;
		8)	PRINTER=la75plus	;;
		9)	PRINTER=ln03		;;
		0)	PRINTER=lj250		;;
		a)	PRINTER=necp6		;;
		b)	PRINTER=oki182		;;
		c)	PRINTER=okiibm		;;
		*)	PRINTER=""		;;
	esac
	;;

    8)	# other, ps emul
	# check if gs is installed ... otherwise warn warn warn !!!
	if [ ! "$HAVE_GS" = "True" ]; then
		warn_about_missing_gs
	fi
cat << !EOM
	-----------------------------------------------------------------
	List of miscellaneous ghostscript printer driver
	-----------------------------------------------------------------
	(1)	ccr      - CalComp Raster Format
	(2)	cp50     - Mitsubishi CP50 color
	(3)	imagen   - Imagen ImPress printers
	(4)	m8510    - C.Itoh M8510 printer
	(5)	oce9050  - OCE 9050 printer
	(6)	r4081    - Ricoh 4081 laser printer
	(7)	sj48     - StarJet 48 inkjet printer
	(8)	t4693d2  - Tektronix 4693d color, 2 bits per R/G/B component
	(9)	t4693d4  - Tektronix 4693d color, 4 bits per R/G/B component
	(0)	t4693d8  - Tektronix 4693d color, 8 bits per R/G/B component
	(a)	tek4696  - Tektronix 4695/4696 inkjet plotter
	(b)	uniprint - configurable ESC/P,ESC/P2,HP-RTL/PCL mono/color drv.
	(c)	xes      - Xerox XES printers (2700, 3700, 4045, etc.)
	(d)	pxlmono  - Black & White PCL XL
	(e)	pxlcolor - Color PCL XL
	-----------------------------------------------------------------
!EOM
	echo "Please enter the printer driver of your choice"
	echo "or type <RETURN> to choose another category/driver:"
	echo $n "Your choice ? $c"; read answer
	case $answer in
		1)	PRINTER=ccr		;;
		2)	PRINTER=cp50		;;
		3)	PRINTER=imagen		;;
		4)	PRINTER=m8510		;;
		5)	PRINTER=oce9050		;;
		6)	PRINTER=r4081		;;
		7)	PRINTER=sj48		;;
		8)	PRINTER=t4693d2		;;
		9)	PRINTER=t4693d4		;;
		0)	PRINTER=t4693d8		;;
		a)	PRINTER=tek4696		;;
		b)	PRINTER=uniprint	
clear
cat << !EOM
------------------------------------------------------------------------------
List of uniprint printer models
------------------------------------------------------------------------------
		(1) Canon BJC 610
		(2) HP Deskjet 550c, 300x300DpI, Gamma
		(3) NEC Prinwriter 2X
		(4) SUN rasterfile
		(5) Epson Stylus Color (I, II, IIs, PRO, 500, 600, 800)
------------------------------------------------------------------------------
!EOM
        echo "Please enter the uniprint printer model of your choice"
        echo "or type <RETURN> to choose another category/driver:"
        echo $n "Your choice ? $c"; read answer
        case $answer in
		1) 
clear
cat << !EOM
-----------------------------------------------------------------------------
List of uniprint profiles for Canon BJC 610
-----------------------------------------------------------------------------
(a) bjc610a0  Canon BJC 610, 360x360DpI, plain paper high speed, color, rendered
(b) bjc610a1  Canon BJC 610, 360x360DpI, plain paper, color, rendered
(c) bjc610a2  Canon BJC 610, 360x360DpI, coated paper, color, rendered
(d) bjc610a3  Canon BJC 610, 360x360DpI, transparency film, color, rendered
(e) bjc610a4  Canon BJC 610, 360x360DpI, back print film, color, rendered
(f) bjc610a5  Canon BJC 610, 360x360DpI, fabric sheet, color, rendered
(g) bjc610a6  Canon BJC 610, 360x360DpI, glossy paper, color, rendered
(h) bjc610a7  Canon BJC 610, 360x360DpI, high gloss film, color, rendered
(i) bjc610a8  Canon BJC 610, 360x360DpI, high resolution paper, color, rendered
(j) bjc610b1  Canon BJC 610, 720x720DpI, plain paper, color, rendered
(k) bjc610b2  Canon BJC 610, 720x720DpI, coated paper, color, rendered
(l) bjc610b3  Canon BJC 610, 720x720DpI, transparency film, color, rendered
(m) bjc610b4  Canon BJC 610, 720x720DpI, back print film, color, rendered
(n) bjc610b6  Canon BJC 610, 720x720DpI, glossy paper, color, rendered
(o) bjc610b7  Canon BJC 610, 720x720DpI, high gloss paper, color, rendered
(p) bjc610b8  Canon BJC 610, 720x720DpI, high resolution paper, color, rendered
-----------------------------------------------------------------------------
!EOM
        echo "Please enter the uniprint profile of your choice"
        echo "or type <RETURN> to choose another category/driver:"
        echo $n "Your choice ? $c"; read answer
        case $answer in

		a)	UP_PROFILE=bjc610a0.upp	;;
		b)	UP_PROFILE=bjc610a1.upp	;;
		c)	UP_PROFILE=bjc610a2.upp	;;
		d)	UP_PROFILE=bjc610a3.upp	;;
		e)	UP_PROFILE=bjc610a4.upp	;;
		f)	UP_PROFILE=bjc610a5.upp	;;
		g)	UP_PROFILE=bjc610a6.upp	;;
		h)	UP_PROFILE=bjc610a7.upp	;;
		i)	UP_PROFILE=bjc610a8.upp	;;
		j)	UP_PROFILE=bjc610b1.upp	;;
		k)	UP_PROFILE=bjc610b2.upp	;;
		l)	UP_PROFILE=bjc610b3.upp	;;
		m)	UP_PROFILE=bjc610b4.upp	;;
		n)	UP_PROFILE=bjc610b6.upp	;;
		o)	UP_PROFILE=bjc610b7.upp	;;
		p)	UP_PROFILE=bjc610b8.upp	;;
		*)	UP_PROFILE=""		;;
	esac
	;;
		2)	UP_PROFILE=cdj550.upp	;;
		3)
clear
cat << !EOM
-----------------------------------------------------------------------------
List of NEC printers
-----------------------------------------------------------------------------
(1) necp2x    NEC Prinwriter 2X, 360x360DpI, Plain Paper
(2) necp2x6   NEC Prinwriter 2X, 360x360DpI, Plain Paper
-----------------------------------------------------------------------------
!EOM
        echo "Please enter the uniprint profile of your choice"
        echo "or type <RETURN> to choose another category/driver:"
        echo $n "Your choice ? $c"; read answer
        case $answer in
		1) UP_PROFILE=necp2x.upp   ;;
		2) UP_PROFILE=necp2x6.upp  ;;
		*) UP_PROFILE=""	   ;;
	esac
	;;
		4)
clear
cat << !EOM
-----------------------------------------------------------------------------
List of SUN raster profiles
-----------------------------------------------------------------------------
(1) ras1      SUN rasterfile, 1 Bit, 2 Colors (Ghostscript-Rendering)
(2) ras24     SUN rasterfile, 24 Bit, 7 Colors (RGB-Error-Diffusion)
(3) ras3      SUN rasterfile, 3 Bit, 7 Colors (RGB-Ghostscript)
(4) ras32     SUN rasterfile, 32 Bit, 6+1 Colors (CMYK-Error-Diffusion)
(5) ras4      SUN rasterfile, 4 Bit, 6+1 Colors (CMYK-Ghostscript)
(6) ras8m     SUN rasterfile, 8 Bit, 2 Colors (Error-Diffusion)
-----------------------------------------------------------------------------
!EOM
        echo "Please enter the uniprint profile of your choice"
        echo "or type <RETURN> to choose another category/driver:"
        echo $n "Your choice ? $c"; read answer
        case $answer in
		1)	UP_PROFILE=ras1.upp     ;;
                2)      UP_PROFILE=ras24.upp    ;;
                3)      UP_PROFILE=ras3.upp     ;;
                4)      UP_PROFILE=ras32.upp    ;;
                5)      UP_PROFILE=ras4.upp     ;;
                6)      UP_PROFILE=ras8m.upp    ;;
		*)	UP_PROFILE=""		;;
	esac
	;;
		5)
clear
cat << !EOM 
-----------------------------------------------------------------------------
List of Epson printers
-----------------------------------------------------------------------------
(a) stc       Epson Stylus Color I (and PRO Series), 360x360DpI, Plain Paper
(b) stc1520h  Epson Stylus Color 1520, 1440x720DpI, Inkjet Paper
(c) stc2      Epson Stylus Color II / IIs, 360x360DpI, Plain Paper
(d) stc2_h    Epson Stylus Color II, 720x720DpI, Special Paper
(e) stc2s_h   Epson Stylus Color IIs, 720x720DpI, Special Paper
(f) stc500p   Epson Stylus Color 500, 360x360DpI, not Weaved, Plain Paper
(g) stc500ph  Epson Stylus Color 500, 720x720DpI, not Weaved, Plain Paper
(h) stc600ih  Epson Stylus Color 600, 1440x720DpI, Inkjet Paper
(i) stc600p   Epson Stylus Color 600, 720x720DpI, Plain Paper
(j) stc600pl  Epson Stylus Color 600, 360x360DpI, Plain Paper
(k) stc800ih  Epson Stylus Color 800, 1440x720DpI, Inkjet Paper
(l) stc800p   Epson Stylus Color 800, 720x720DpI, Plain Paper
(m) stc800pl  Epson Stylus Color 800, 360x360DpI, Plain Paper
(n) stc_h     Epson Stylus Color I (and PRO Series), 720x720DpI, Special Paper
(o) stc_l     Epson Stylus Color I (and PRO Series), 360x360DpI, noWeave
(p) stcany    Any Epson Stylus Color, 360x360DpI
------------------------------------------------------------------------------
!EOM
        echo "Please enter the uniprinter driver profile of your choice"
        echo "or type <RETURN> to choose another category/driver:"
        echo $n "Your choice ? $c"; read answer
        case $answer in
		a)	UP_PROFILE=stc.upp	;;
		b)	UP_PROFILE=stc1520h.upp	;;
		c)	UP_PROFILE=stc2.upp	;;
		d)	UP_PROFILE=stc2_h.upp	;;
		e)	UP_PROFILE=stc2s_h.upp	;;
		f)	UP_PROFILE=stc500p.upp	;;
		g)	UP_PROFILE=stc500ph.upp	;;
		h)	UP_PROFILE=stc600ih.upp	;;
		i)	UP_PROFILE=stc600p.upp	;;
		j)	UP_PROFILE=stc600pl.upp	;;
		k)	UP_PROFILE=stc800ih.upp	;;
		l)	UP_PROFILE=stc800p.upp	;;
		m)	UP_PROFILE=stc800pl.upp	;;
		n)	UP_PROFILE=stc_h.upp	;;
		o)	UP_PROFILE=stc_l.upp	;;
		p)	UP_PROFILE=stcany.upp	;;
		*)	UP_PROFILE=""		;;
	esac
	;;
		*)	UP_PROFILE=""		;;
esac
;;
		c)	PRINTER=xes		;;
		d)	PRINTER=pxlmono		;;
		e)	PRINTER=pxlcolor	;;
		*)	PRINTER=""		;;
	esac
	;;

    9)	# A true Postscript Printer
cat << !EOM
	-----------------------------------------------------------------
	Fine, you seem to have a true Postscript printer
	Please enter it's print resolution using the following scheme
	-----------------------------------------------------------------
	(1)	300  dpi Postscript printer
	(2)	360  dpi Postscript printer
	(3)	400  dpi Postscript printer
	(4)	600  dpi Postscript printer
	(5)	720  dpi Postscript printer
	(6)	800  dpi Postscript printer
	(7)	1000 dpi Postscript printer
	(8)	1200 dpi Postscript printer
	(9)	1440 dpi Postscript printer
	(0)	1600 dpi Postscript printer
	-----------------------------------------------------------------
!EOM
	echo "Please enter the printer driver of your choice"
	echo "or type <RETURN> to choose another category/driver:"
	echo $n "Your choice ? $c"; read answer
	case $answer in
		1)	PRINTER=PS_300dpi	;;
		2)	PRINTER=PS_360dpi	;;
		3)	PRINTER=PS_400dpi	;;
		4)	PRINTER=PS_600dpi	;;
		5)	PRINTER=PS_720dpi	;;
		6)	PRINTER=PS_800dpi	;;
		7)	PRINTER=PS_1000dpi	;;
		8)	PRINTER=PS_1200dpi	;;
		9)	PRINTER=PS_1440dpi	;;
		0)	PRINTER=PS_1600dpi	;;
		*)	PRINTER=""		;;
	esac
	;;
esac
}	# End of function printer selection

################################################################################
# New: Loop until suitable driver found and confirmed by the user
################################################################################
do_choose_driver()
{
while [ -z $PRINTER_OK ]
do
	do_printer_selection

	case $PRINTER in
	    ""|" "|n|N)	# accept garbage or nothing ;-)
			# -> new selection
			;;
	    q|Q|e|E)	# end, ende, quit, exit, raus ;-)
			exit 1
			;;
	    *)		# confirm and then finish while loop
			echo ""
			echo "Please confirm your selection [ $PRINTER ]"
			echo $n "OK y/n ? $c"
			read answer
			case $answer in
				n|N)	# do nothing, so repeat selection
					;;
				*)	# otherwise the selection is confirmed
					PRINTER_OK=true
			esac
			;;
	esac
done

#
# reset this variable, to allow user to choose another driver later
#
PRINTER_OK=

# After this step has been successfully performed
# we fill the main menue field "printer driver resolution"
# with the string default, indicating, that the drivers default
# resolution is active unless you change it.

RESOL=default
COLOR=default
# BPP is later used for the bpp value in symlinks
BPP=$COLOR

}

################################################################################
# called by do_setup_interface (parallel setup)
################################################################################
do_ask_parallel_device()
{
	clear
	cat << !EOM

        ----------------------------------------------------------------
        A P S F I L T E R   Parallel Interface Settings     -- Device --
        ----------------------------------------------------------------

	Depending on your flavour of Unix, you have to enter the
	*full path* to your parallel Interface. In case of trouble please
	contact your local system administrator or read your operating
	systems technical documentation, FreeBSD handbook or Linux HowTo.
	Some examples:

	FreeBSD, NetBSD, OpenBSD:   LPT1: /dev/lpt0   LPT2: /dev/lpt1
	Linux:                      LPT1: /dev/lp0    LPT2: /dev/lp1
	SunOS4:                           /dev/bpp0         /dev/bpp1

	currently selected:               Interface:  [$INTERFACE]
                                          Device:     [$DEVICE]	

!EOM

	echo $n "Full path of parallel print device: $c"; read answer

	if [ ! -z $answer ]; then
		DEVICE=$answer
	fi
}

################################################################################
# Some routines for serial interface options, line discipline...
################################################################################
do_read_serial_device()
{
	clear
	cat << !EOM

        ----------------------------------------------------------------
        A P S F I L T E R   Serial Interface Settings       -- Device --
        ----------------------------------------------------------------

	Depending on your flavour of Unix, you have to enter the
	*full path* to your serial Interface. In case of trouble please
	contact your local system administrator or read your operating
	systems technical documentation, FreeBSD handbook or Linux HowTo.
	Some examples:

	FreeBSD, NetBSD, OpenBSD:   COM1: /dev/cuaa0  COM2: /dev/cuaa1
	Linux:                      COM1: /dev/ttyS0  COM2: /dev/ttyS1
	SunOS4:                           /dev/ttya         /dev/ttyb

	currently selected:                    Interface: [$INTERFACE]
                                               Device   : [$DEVICE]
!EOM

	echo $n "Your choice ? $c"; read answer

	if [ ! -z $answer ]; then
		DEVICE=$answer
	fi
}

do_read_serial_baud()
{
	clear
	cat << !EOM

        ----------------------------------------------------------------
        A P S F I L T E R   Serial Interface Settings     -- Baudrate --
        ----------------------------------------------------------------

	The Baud Rate is the transfer rate at which the asynchronous
	serial interface is sending bits from the computer to the printer.

	The higher the Baudrate the better and shorter your cable should
	be. I recommend 5m cable as a maximum for 38400 Baud and perhaps
	2-3m for speeds higher than that. It depends heavily on the
	quality of your cables, interfaces, handshaking and printer.

	Another rule of thumb is, that hardware handshaking works best
	with higher Baudrates, but needs a special cable depending on
	your machine and the printer.

	Valid and useable Baudrates are (a good starting point is 9600)

	1) 4800		2) 9600		3) 19200
	4) 38400	5) 57600	6) 115200

	Please note: you have to configure your printer as well for that
	speed and don't forget to power off and on the printer after that.

!EOM

	echo $n "Your choice ? $c"; read answer

	case $answer in
		1)	BAUDRATE=4800	;;
		2)	BAUDRATE=9600	;;
		3)	BAUDRATE=19200	;;
		4)	BAUDRATE=38400	;;
		5)	BAUDRATE=57600	;;
		6)	BAUDRATE=115200	;;
		*)	# ignore everything other
	esac

}
do_read_serial_handshake()
{
	clear
	cat << !EOM

        ----------------------------------------------------------------
        A P S F I L T E R   Serial Interface Settings  -- Handshaking --
        ----------------------------------------------------------------

	There are two possible handshaking methods, often called
	Hardware and Software handshaking. This determines, how
	the printer tells your computers start or stop sending data
	to avoid data lossage.

	Directly forget about using ghostscript as Postscript Emulator
	with the software handshaking (xon/xoff protocol), because gs
	(ghostscript) sends a binary data stream to the printer containing
	these start/stop characters. But I will allow you to select this
	as an option, you'll certainly know better than I what you want.

	Hardware handshaking isn't as standardized as it could. So
	different computer/printers use different pins/signals of the
	serial interface, to stop/start the data transfer. You'll
	certainly need a special printer cable !

	1 - software handshaking (xon/xoff, 3 wires, avoid this)
	2 - hardware handshaking (crtscts, RTS/CTS flow control)

!EOM

	echo $n "Your choice ? $c"; read answer

	case $answer in
		1)	HANDSHAKE="ixon,ixoff,ixany,-crtscts"
			HANDSHAKE_FOR_COMMAND="ixon ixoff ixany -crtscts"
			;;
		2)	HANDSHAKE="-ixon,-ixoff,-ixany,crtscts"
			HANDSHAKE_FOR_COMMAND="-ixon -ixoff -ixany crtscts"
			;;
	esac
}
do_read_serial_wordlength()
{
	clear
	cat << !EOM

        ----------------------------------------------------------------
        A P S F I L T E R   Serial Interface Settings   -- Wordlength --
        ----------------------------------------------------------------

	Computer and Printer must have the same settings how many
	"bits" long a "data word" is.

	We have to use "8 Bit" long data words, otherwise it isn't
	possible to transmit characters above a value of 127.
	We need 8 Bit here !!!

	1)	8 Bit		(a must !)
	2)	7 Bit		(please do not use this !)

!EOM
	echo $n "Your choice ? $c"; read answer

	case $answer in
		1)	WORDLENGTH=8
			WORDLENGTH_COMMAND=cs8
			;;
		2)	WORDLENGTH=7
			WORDLENGTH_COMMAND=cs7
			;;
		*)	# ignore the rest
			;;
	esac

}
do_read_serial_parity()
{
	clear
	cat << !EOM

        ----------------------------------------------------------------
        A P S F I L T E R   Serial Interface Settings       -- Parity --
        ----------------------------------------------------------------

	Computer and Printer must have the same settings if they
	use parity or not. Parity is a kind of a checksum to detect
	garbage on the line.

	Usually we use no parity. If there is a need for parity,
	you have to decide if you use even, odd or mark parity.

	1)	no parity	(take this)
	2)	even
	3)	odd

!EOM

	echo $n "Your choice ? $c"; read answer

	case $answer in
		1)	PARITY="no parity"
			PARITY_COMMAND=-parenb
			;;
		2)	PARITY="even"
			PARITY_COMMAND="parenb -parodd"
			;;
		3)	PARITY="odd"
			PARITY_COMMAND="parenb parodd"
			;;
		*)	# ignore the rest
			;;
	esac

}

do_read_serial_stopbits()
{
	clear
	cat << !EOM

        ----------------------------------------------------------------
        A P S F I L T E R   Serial Interface Settings    -- Stop Bits --
        ----------------------------------------------------------------

	Your computer and printer must have the same amount of stopbits
	configured that are sent/accepted after transmitting a "Data
	word" - and parity information, if you selected one -.

	Standard is to use one stopbit. Please configure your printer's
	serial interface to use one stopbit, if possible, otherwise use
	two stopbits.

	1)	1 Stopbit
	2)	2 Stopbits

!EOM

	echo $n "Your choice ? $c"; read answer

	case $answer in
		1)	STOPBITS=1
			STOPBIT_COMMAND="-cstopb"
			;;
		2)	STOPBITS=2
			STOPBIT_COMMAND="cstopb"
			;;
		*)	# ignore the rest
			;;
	esac

}

################################################################################
# called by do_setup_interface (serial setup)
################################################################################
do_ask_serial_settings()
{
	clear
	cat << !EOM

        ----------------------------------------------------------------
        A P S F I L T E R   S E T U P    -- Serial Interface Settings --
        ----------------------------------------------------------------

	Important for a successfull configuration is, that the computers
	_and_ the printers interface do have exactly the same settings !

	Operating system dependend
	   1) Printer device			[$DEVICE]

	General operating characteristics
	   2) Baud rate				[$BAUDRATE]
	   3) Handshaking			[$HANDSHAKE]

	Data format
	   4) Data Word Length (in bits)	[$WORDLENGTH]
	   5) Parity				[$PARITY]
	   6) Startbits				[$STARTBITS]
	   7) Stopbits				[$STOPBITS]

	T) Test Page, by sending some characters to the print device

!EOM
	echo $n "Your choice ? $c"; read answer

	case $answer in
		1)	do_read_serial_device		;;
		2)	do_read_serial_baud		;;
		3)	do_read_serial_handshake	;;
		4)	do_read_serial_wordlength	;;
		5)	do_read_serial_parity		;;
		6)	do_read_serial_stopbits		;;
		*)	# ignore the rest		;;
	esac
}

################################################################################
# Print Device serial or parallel ???
################################################################################
do_setup_interface()
{
	clear
	cat << !EOM

        ----------------------------------------------------------------
        A P S F I L T E R   S E T U P              -- Interface Setup --
        ----------------------------------------------------------------

	The easiest way, to connect a printer to your computer is by
	using the parallel interface, because it's usually *faster*,
	more standardized and therefore much easier to configure.

	When configuring a serial printer, the installation dialogue
	asks you many questions about how to configure the serial
	interface of your computer, so that it works well with your
	printers current settings.

	When using the serial interface, then you have to choose special
	cables, depending on the communication protocol between computer
	and printer (hardware/software handshaking). Many pitfalls here !

	currently selected:               Interface:  [$INTERFACE]
                                          Device:     [$DEVICE]	

	1)	configure a parallel printer (best choice !)
	2)	configure a serial printer   (more work, but possible)

!EOM
	echo $n "Your choice ? $c"; read answer

	case $answer in
		2)	INTERFACE=serial	;;
		*|1)	INTERFACE=parallel	;;
	esac

	# it makes sense to directly go into the serial interface config menue
	if [ $INTERFACE = "serial" ]; then
		do_ask_serial_settings
	else
		do_ask_parallel_device
	fi
}

################################################################################
# Choose the print resolution
################################################################################
do_choose_resolution()
{
clear
cat << !EOM

        ----------------------------------------------------------------
        A P S F I L T E R   S E T U P             -- Print Resolution --
        ----------------------------------------------------------------

	Please note: this setup affects only the print resoulution for
	printing the ,Test Page' from the main menue.

	You should always start with the drivers default resolution.

	Hint: for some Laser Printer (i.e. HP LaserJet II) you have
	to choose a smaller print resolution than the drivers default
	resolution, because of a lack of memory !!!

	Some typical settings to play with:

	1) 150dpi	2) 300dpi	3) 600dpi	4) 1200dpi
	5) 180dpi	6) 360dpi	7) 360x180dpi	8) 720dpi

	c) custom settings

	d) back to default resolution of the printer driver

!EOM

	echo $n "Your choice ? $c"; read answer

	case $answer in
		1)	XRESOL=150;	YRESOL=150	;;
		2)	XRESOL=300;	YRESOL=300	;;
		3)	XRESOL=600;	YRESOL=600	;;
		4)	XRESOL=1200;	YRESOL=1200	;;
		5)	XRESOL=180;	YRESOL=180	;;
		6)	XRESOL=360;	YRESOL=360	;;
		7)	XRESOL=360;	YRESOL=180	;;
		8)	XRESOL=720;	YRESOL=720	;;
		c|C)	# custom settings
			echo $n "Please input x-resolution in dpi: $c"
			read XRESOL
			echo $n "Please input y-resolution in dpi: $c"
			read YRESOL
			;;
		d|D)	RESOL=default
			;;
		*)	# invalid input, currently simply ignore it
			;;
	esac

	RESOL="${XRESOL}x${YRESOL}"
}

################################################################################
# Toggle between color and monochrome mode for test page
################################################################################
do_toggle_color()
{
clear
cat << !EOM

        ----------------------------------------------------------------
        A P S F I L T E R   S E T U P             -- Mono/Color Setup --
        ----------------------------------------------------------------

	If your printer/driver support color output, then the driver
	has certain defaults compiled in.

	Some of the drivers support printing in high quality color mode.
	The number of "bits per pixel" (bpp) determine the number of
	colors and so the quality of color prints.

	This is kind of "expert mode", because you have to know, what
	your printer and the ghostscript printer driver supports !
	If you have no better idea, then please choose "default".

	d)	default (highly recommended !!!)

	m)	monochrome (1 bpp)

	1)	4 bpp  (color)
	2)	8 bpp  (color)
	3)	24 bpp (color)
	4)	32 bpp (color)

!EOM
	echo $n "Your choice ? $c"; read answer

	case $answer in
		1)	COLOR=4;	BPP=$COLOR	;;
		2)	COLOR=8;	BPP=$COLOR	;;
		3)	COLOR=24;	BPP=$COLOR	;;
		4)	COLOR=32;	BPP=$COLOR	;;
		m)	COLOR=1;	BPP=$COLOR	;;
		d)	COLOR=default;	BPP=$COLOR	;;
		*)	# ignore rest	;;
	esac
}

do_setup_paper_format()
{
clear
cat << !EOM

        ----------------------------------------------------------------
        A P S F I L T E R   S E T U P                 -- Paper Format --
        ----------------------------------------------------------------

	What paper format do you want to use for printing ?

	Hint: that's only important for printing the testpage.

	1) DIN A4
	2) letter
	3) DIN A3

!EOM
	echo $n "Your choice ? $c"; read answer

	case $answer in
		1)	PAPER=a4	;;
		2)	PAPER=letter	;;
		3)	PAPER=a3	;;
		*)	# ignore rest	;;
	esac
}

do_print_test_page()
{
clear
cat << !EOM

        ----------------------------------------------------------------
        A P S F I L T E R   S E T U P                    -- Test Page --
        ----------------------------------------------------------------

	In most cases it's relatively easy to choose a suitable
	ghostscript driver from the list of supported printers.

	If you're printer is not in the list of supported printers:
	    - guess a suitable driver by ,trial and error'
	    - consult your printers handbook to what ghostscript printer
	      driver your printer "claims" to be compatible

	If this test produces unacceptable results, then please choose
	another driver or try another (perhaps lower) print resolution,
	because it might be a memory problem. Only use print resolution
	that are supported by your printer, consult the printers manual !

	Creating the test output might last some time, please be patient.

	T)	Print a test page
	*)	Back to main menue

!EOM

	echo $n "Your choice ? $c"; read answer

	case $answer in
		t|T)
			if [ ! -z $RESOL -a "$RESOL" != "default" ]
			then
				RESOLUTION="-r$RESOL"
			else
				RESOLUTION=""
			fi

			if [ ! -z $BPP -a "$BPP" != "default" ]
			then
				BITSPERPIXEL="-dBitsPerPixel=$BPP"
			else
				BITSPERPIXEL=""
			fi
			if [ $PRINTER = "uniprint" ]
			then
			CREATE_TESTPAGE_COMMAND="		\
				cat $APS_BASEDIR/setup/tiger.ps \
				| gs				\
				-q				\
				@${UP_PROFILE}			\
				-sOutputFile=/tmp/tiger.out	\
				-"
			else

			CREATE_TESTPAGE_COMMAND="		\
				cat $APS_BASEDIR/setup/tiger.ps	\
				| gs				\
				-q				\
				$RESOLUTION			\
				$BITSPERPIXEL			\
				-sDEVICE=${PRINTER}		\
				-sPAPERSIZE=${PAPER}		\
				-dNOPAUSE			\
				-dSAFER				\
				-sOutputFile=/tmp/tiger.out	\
				$PS_INIT			\
				-"
			fi

			# Tell the magic command ...
			echo $n "Printing Test page using: $c"
			echo ${CREATE_TESTPAGE_COMMAND}

			echo $n "Ok, to print testpage ? [y/n] $c"
			read answer
			case $answer in
				N|n)	# do nothing
					;;
				*)	# print....
					echo $n "Creating test page... $c"
					eval ${CREATE_TESTPAGE_COMMAND}
					# check if command was successful
					# only then print
					EXIT_STATUS=$?
					if [ "$EXIT_STATUS" = "0" ]; then
					    echo $n "Printing test page... $c"
					    cat /tmp/tiger.out > ${DEVICE}
					else
					    echo "last command terminated with"
					    echo "exit status: $EXIT_STATUS"
					    echo "Error creating testpage !!!"
					    echo "[ press RETURN to continue ]"
					    read answer
					fi
					;;
			esac
			;;
		*)	# ignore the rest back to main()
			;;
	esac
}

do_save_environment()
{
	#
	# Save some settings for another SETUP attempt ...
	# Gooood for testing ;-))
	#
	echo "PRINTER=$PRINTER" > SETUP.cfg
	echo "XRESOL=$XRESOL" >> SETUP.cfg
	echo "YRESOL=$YRESOL" >> SETUP.cfg
	echo "RESOL=$RESOL" >> SETUP.cfg
	echo "RESOLUTION=$RESOLUTION" >> SETUP.cfg
	echo "BITSPERPIXEL=$BITSPERPIXEL" >> SETUP.cfg
	echo "COLOR=$COLOR" >> SETUP.cfg
	echo "PAPER=$PAPER" >> SETUP.cfg
	echo "BPP=$BPP" >> SETUP.cfg
	echo "INTERFACE=$INTERFACE" >> SETUP.cfg
	echo "DEVICE=$DEVICE" >> SETUP.cfg
	echo "BAUDRATE=$BAUDRATE" >> SETUP.cfg
	echo "HANDSHAKE=$HANDSHAKE" >> SETUP.cfg
	echo "WORDLENGTH=$WORDLENGTH" >> SETUP.cfg
	echo "PARITY=$PARITY" >> SETUP.cfg
	echo "STARTBITS=$STARTBITS" >> SETUP.cfg
	echo "STOPBITS=$STOPBITS" >> SETUP.cfg
	echo "DISPLAY_RESOL=$DISPLAY_RESOL" >> SETUP.cfg
	echo "DISPLAY_BPP=$DISPLAY_BPP" >> SETUP.cfg
}

do_main_menue()
{

	#
	# save settings every time ;-)
	#
	do_save_environment

	# nicer display of print resolution...
	case $RESOL in
		"")		DISPLAY_RESOL=""
				;;
		default)	DISPLAY_RESOL="default"
				;;
		*)		DISPLAY_RESOL="${RESOL}dpi"
				;;
	esac

	# nice display of monochrome and color printing
	case $COLOR in
		"")		DISPLAY_BPP=""
				;;
		default)	DISPLAY_BPP="default"
				;;
		*)		DISPLAY_BPP=${COLOR}bpp
				;;
	esac

	#
	# PRINTER that need a special PS init file in GS_LIBDIR
	#
	case $PRINTER in
		stcolor)	PS_INIT=stcolor.ps	;;
		*)		PS_INIT=""		;;
	esac

	clear
	cat << !EOM

        ==================================================================
          A P S F I L T E R   S E T U P                   -- MAIN MENUE --
        ==================================================================

                                                        currently selected
        ------------------------------------------------------------------
	(D)	Available Device Drivers in your gs binary   (gs -h)
	(R)	Read ghostscript's docu about device drivers (devices.txt)
	(1)	Printer Driver Selection		[${PRINTER}]
	(2)	Interface Setup				[${INTERFACE}]

	For printing the test page:
	(3)	Print Resolution in "dots per inch"	[${DISPLAY_RESOL}]
	(4)	Toggle Monochrom/Color (1bpp=b&w)	[${DISPLAY_BPP}]
	(5)	Paper Format				[${PAPER}]
	(T)	Print Test Page (after step 1-5)

	(C)	==> Continue printer setup with values shown above

	(Q)	Quit Setup

!EOM
	echo $n "        Your choice ? $c"
	read answer
	case $answer in
		d|D)	gs -h | more -e 2>&1
			;;
		r|R)	more -e setup/devices.txt
			;;
		1)	do_choose_driver
			;;
		2)	do_setup_interface
			;;
		3)	do_choose_resolution
			;;
		4)	do_toggle_color
			;;
		5)	do_setup_paper_format
			;;
		t|T)	do_print_test_page
			;;
		c|C)	do_finish_setup
			FINISH_SETUP=true
			;;
		q|Q)	# go out of here with an error, exit stat 0 is only
			# for a complete and successfull installation
			exit 1
			;;
		*)	# simply ignore wrong input
			do_main_menue
			;;
	esac
}

#############################################
####                                     ####
#### FROM HERE ON PERFORM REAL WORK !!!  ####
####                                     ####
#############################################

################################################################################
# - usually the apsfilter script is being executed with certain system
#   privileges during runtime
# - so we have to make sure, that ordinary users aren't allowed to modify
#   the scripts
# - fix things if user allows it, otherwise exit ...
################################################################################
do_check_permissions()
{
	case `uname -n` in
		*klemm.gtn.com)
			# don't check permissions on my system
			;;
		*)	# fix permissions on a production machine 
			FIX_PERMISSIONS=true
			;;
	esac

	#
	# on a production machine always fix permissions !
	#
	if [ ! -z $FIX_PERMISSIONS ]; then

		echo "Checking permissions of $APS_BASEDIR"

		DIR_OWNER=`ls -lgd $APS_BASEDIR | awk '{ print $3 }'`
		DIR_GROUP=`ls -lgd $APS_BASEDIR | awk '{ print $4 }'`
		case $DIR_OWNER in
			root|daemon|bin)
				# ok, secure directory owner found
				echo "found dir owner=${DIR_OWNER}, ok!"
				;;
			*)
				# set secure directory owner
				echo "found dir owner=${DIR_OWNER}, not ok !"
				echo "setting dir owner=bin !"
				DIR_OWNER=bin
				;;
		esac
		case $DIR_GROUP in
			wheel|other|bin)
				# ok, secure directory group found
				echo "found dir group=${DIR_GROUP}, ok!"
				;;
			*)
				# set secure directory group
				echo "found dir group=${DIR_OWNER}, not ok !"
				echo "setting dir group=bin !"
				DIR_GROUP=bin
				;;
		esac

		# after having set sane defaults, fix permissions
		echo $n "changing permissions of $APS_BASEDIR...$c"
		chmod 555 $APS_BASEDIR
		find $APS_BASEDIR -print | xargs chown $DIR_OWNER
		find $APS_BASEDIR -print | xargs chgrp $DIR_GROUP
		find $APS_BASEDIR -type d -print | xargs chmod 555
		find $APS_BASEDIR -type f -print | xargs chmod ugo+r
		find $APS_BASEDIR -type f -print | xargs chmod ugo-w
		echo "done."
	fi
}

################################################################################
# - For Linux: remove FreeBSD binary versions of a2ps in $APS_BASEDIR/bin
################################################################################
do_cleanup_for_linux()
{
	case `uname -s` in
		*FreeBSD*|*NetBSD*|*OpenBSD*)
			;;
		*)
			echo "removing FreeBSD binaries on this platform..."
			if [ -f $APS_BASEDIR/bin/a2ps ]; then
				rm -f $APS_BASEDIR/bin/a2ps
			fi
			# leave this in case there is an old binary
			if [ -f $APS_BASEDIR/bin/rewindstdin ]; then
				rm -f $APS_BASEDIR/bin/rewindstdin
			fi
			;;
	esac
}

################################################################################
# Save /etc/printcap
#	- save the original printcap		-> /etc/printcap.orig
#	- make a work copy of the printcap file -> /etc/printcap.old
################################################################################
do_save_printcaps()
{
	if [ ! -f /etc/printcap.orig ]; then
		echo "saving original printcap -> /etc/printcap.orig"
		cp /etc/printcap /etc/printcap.orig
	fi
	echo "creating a working copy of printcap -> /etc/printcap.old"
	cp /etc/printcap /etc/printcap.old
}

################################################################################
# Was printcap already created by apsfilter ?
# Yes: keep entries or overwrite ?
# No:  append after existing entries or overwrite ?
################################################################################
do_check_for_old_apsfilter_installation()
{
	if ( grep "# LABEL apsfilter" /etc/printcap > /dev/null 2>&1 )
	then
		echo ""
		echo "I found a label in your 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
		case $answer in
			o|O)
				echo "Ok, erasing old apsfilter entries..."
				sed -e "/# LABEL apsfilter/,$ d" \
				< /etc/printcap.old \
				> /etc/printcap
				;;
			*)
				echo "Ok, adding another apsfilter printer..."
				ADD_PRINTER=1
				;;
		esac
	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.$$
}

#####################################################
#####################################################
#####################################################
####                                             ####
####                 M A I N                     ####
####                                             ####
#####################################################
#####################################################
#####################################################

case `uname -n` in
	*klemm.gtn.com)
		# on my developement platforms don't check for
		# root permissions ;-)
		;;
	*)	# only continue with root privileges
		# since normal users are unable to
		# - change /etc/printcap
		# - write to the printer device
		# - setup spool directories as needed
		do_check_root
		;;
esac

do_check_path
do_initialize
do_start
while [ -z $FINISH_SETUP ]
do
	do_main_menue
done

#
# real work is done after user confirms setup process !!!
#
	clear
	cat << !EOM

        ================================================================
          A P S F I L T E R   I N S T A L L A T I O N
        ================================================================

	apsfilter installation....

!EOM

# save the environment settings first ;-)
do_save_environment

do_check_permissions
do_cleanup_for_linux
do_save_printcaps
do_check_for_old_apsfilter_installation

#####################################################
####                                             ####
####         END OF SCRIPT                       ####
####                                             ####
#####################################################

#
# The spooldir's location
#
SPOOLDIR=$SPOOL/$PRINTER

#
# Does the user have a color printer ? Depending on what has been
# configured it's still not clear...
#

case $COLOR in
	"")	# This shouldn't be the case
		echo "error: variable COLOR is empty, strange !!!"
		exit 1
		;;
	default)
		ASK_FOR_COLOR=true
		;;
	*)
		if [ $COLOR -eq 1 ]; then
			COLOR=mono
			DEFAULT_PRINTER=2
		else
			COLOR="mono color"
			DEFAULT_PRINTER=4
		fi
		;;
esac

if [ ! -z $ASK_FOR_COLOR ]
then
	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"
			DEFAULT_PRINTER=4
			;;
		m|M)	COLOR="mono"
			DEFAULT_PRINTER=2
			;;
		*)	echo "*** I assume you have a mono printer ***"
			COLOR="mono"
			DEFAULT_PRINTER=2
			;;
	esac
fi

#
# if user choosed *append* instead of overwrite /etc/printcap
#
if [ ! -z $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 "Starting with lpN where N is $i"
else
	# set auto printer name counter to 1
	i=1
	echo "Starting with lpN where N is $i"
fi

METHOD="ascii auto"

#
# XXX: It may be cleaner only to make an apsfilter label in printcap
# if we completely overwrite the printcap .... maybe later ...
#
echo "making apsfilter label in /etc/printcap..."
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 [ -z "$ADD_PRINTER" ]; then
	echo "# APS_BASEDIR:$APS_BASEDIR" >> /etc/printcap
	echo "#" >> /etc/printcap
fi

#
# Ask for owner and group of lp system files
# GLOBAL.sh already did some guessing
#

echo ""
echo "Now we try to determine the correct permissions for files in spooldir"
echo ""

while [ ! $PERM_OK ]
do

echo ""
echo "Your line printer schedulers spooldir seems to be: $SPOOL"
echo ""
ls -ld $SPOOL
echo ""
echo "The Owner of your spooldir seems to be: $LP_OWNER"
echo "The Group of your spooldir seems to be: $LP_GROUP"
echo ""
echo $n "Is this correct ??? [y/n] ? $c"
read answer
case $answer in
	y|Y|j|J)
		# o.k., do nothing
		PERM_OK=1
		;;
	*)	# ask the user

		echo $n "Path of Spool Dir ? [default: ${SPOOL}] $c"
		read answer
		if [ "$answer" != "" ]; then
			SPOOL=$answer
		fi
		mkdir -p $SPOOL

		echo $n "Owner of Spool Dir ? [default: ${LP_OWNER}] $c"
		read answer
		if [ "$answer" != "" ]; then
			LP_OWNER=$answer
		fi

		echo $n "Group of Spool Dir ? [default: ${LP_GROUP}] $c"
		read answer
		if [ "$answer" != "" ]; then
			LP_GROUP=$answer
		fi

		;;
esac
done

#
# creating printcap entries in /etc/printcap
# 
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
	# 	mono	: no. 2
	# 	color	: no. 4
	if [ "$i" = "$DEFAULT_PRINTER" ]; 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}-${PAPER}-${m}-${f}:\\" >> /etc/printcap
	#
	# setup the needed spooldir 4.8.1.2 (AKL)
	echo $n "setting up spooldir for ${SPOOLDIR}-${PAPER}-${m}-${f} $c"
	mkdir -p ${SPOOLDIR}-${PAPER}-${m}-${f} > /dev/null
	: > ${SPOOLDIR}-${PAPER}-${m}-${f}/log
	: > ${SPOOLDIR}-${PAPER}-${m}-${f}/acct
	# for Linux Slackware and others ;-)
	: > ${SPOOLDIR}-${PAPER}-${m}-${f}/lock
	chmod 664 ${SPOOLDIR}-${PAPER}-${m}-${f}/lock
	chown -R $LP_OWNER ${SPOOLDIR}-${PAPER}-${m}-${f}
	chgrp -R $LP_GROUP ${SPOOLDIR}-${PAPER}-${m}-${f}
	#
	#
	echo "	:lf=${SPOOLDIR}-${PAPER}-${m}-${f}/log:\\" >> /etc/printcap
	echo "	:af=${SPOOLDIR}-${PAPER}-${m}-${f}/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}-raw:\\" >> /etc/printcap
	#
	# setup the needed spooldir 4.8.1.2 (AKL)
	echo $n "setting up spooldir for ${SPOOLDIR}-raw $c"
	mkdir -p ${SPOOLDIR}-raw > /dev/null
	: > ${SPOOLDIR}-raw/log
	: > ${SPOOLDIR}-raw/acct
	# for Linux Slackware and others ;-)
	: > ${SPOOLDIR}-raw/lock
	chmod 664 ${SPOOLDIR}-raw/lock
	chown -R $LP_OWNER ${SPOOLDIR}-raw
	chgrp -R $LP_GROUP ${SPOOLDIR}-raw
	#
	#
	echo "	:lf=${SPOOLDIR}-raw/log:\\" >> /etc/printcap
	echo "	:af=${SPOOLDIR}-raw/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`

# last argument can be "mono" or "color mono"
$APS_BASEDIR/setup/mksymlinks $PRINTER $PAPER $COLOR

if [ -z "$PORTS_COLLECTION" ]; then
	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!
	Please check after installation, if you can find:
		$APS_BASEDIR/bin/a2ps

	If the programs are not there, then wait just a moment perhaps
	the compilation is still in progress ;-) If anything fails then
	change into apsfilter's source tree and compile and install the
	utilities manually: i.e.: type something like:
		cd src/a2ps-4.2-akl;	make clean install clean
	------------------------------------------------------------------
!EOM
	echo "[ press <RETURN> to continue ]"
	read answer

	echo $n "Starting compilation...please wait...$c"
	setup/compile
fi	# if not PORTS_COLLECTION !!!
#####################################

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

	This script installs a global apsfilter config file:
		/etc/apsfilterrc

	If defining INSECURE in /etc/apsfilterrc, then it's possible,
	to copy it into each users home directory \$HOME/.apsfilterrc
	Then each user can maintain his favourite default settings.
	--------------------------------------------------------------------
!EOM
echo "[ press <RETURN> to continue ]"
read answer

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

################################################################################
# Save some defaults at the end of /etc/apsfilterrc
################################################################################
#

echo "#" >> /etc/apsfilterrc
echo "# Some defaults from SETUP..." >> /etc/apsfilterrc
echo "#" >> /etc/apsfilterrc

if [ ! -z $BPP -a "$BPP" != "default" ]; then
	BITSPERPIXEL="-dBitsPerPixel=$BPP"
	echo "Saving color depth in /etc/apsfilterrc !"
	echo "GS_FEATURES=${BITSPERPIXEL}" >> /etc/apsfilterrc
fi

if [ "$RESOL" != "default" ]; then
	echo "Saving print resolution in /etc/apsfilterrc !"
	echo "GS_RESOL=$RESOL" >> /etc/apsfilterrc
fi

if [ ! -z $PS_INIT ]; then
	echo "Saving additional gs init file in /etc/apsfilterrc !"
	echo "PS_INIT=${PS_INIT}" >> /etc/apsfilterrc
fi

if [ ! -z $UP_PROFILE ]; then
	echo "Saving uniprint profile in /etc/apsfilterrc !"
	echo "UP_PROFILE=${UP_PROFILE}" >> /etc/apsfilterrc
fi

#
################################################################################

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, you want to print on, 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.
------------------------------------------------------------------------------
!EOM
echo "[ press <RETURN> to continue ]"
read answer

sleep 1
cat << !EOM
------------------------------------------------------------------------------
Some words about apsfilter design:

Apsfilter should be installed on a Server or Desktop machine, where the
printer(s) is(are) connected to.

User on other machines (Unix or Windows) in the network are then relatively
easy able to print onto printers of your "apsfilter print server".
Windows 95 needs samba on the apsfilter print server.
Windows NT machines are able to print with Berkeley LPD protocol, if
you installed the driver for this.

If you have a FreeBSD system (2.2.x or greater), it's also
possible to install apsfilter on a machine without printers
and to print onto network printers. FreeBSD doesn't have the
typical limitation, that line printer input filters (like apsfilter)
doesn't work on remote printers.

Another possibility is a hack in apsfilter. See my comments in the
apsfilter shellscript.

Have fun !
------------------------------------------------------------------------------
!EOM
echo "[ press <RETURN> to continue ]"
read answer

#
# Checking again for filters...
#
. $APS_BASEDIR/setup/filtersetup

echo $n "done."
