#!/bin/sh

# Copyright (c) 2003 KONICA MINOLTA BUSINESS TECHNOLOGIES, INC. All rights reserved.


SU_TEST_FILE=/etc/passwd

cat banner
echo
echo "Enter "?" at any time to receive help."
echo

#
# Make sure user is root
#
if test ! -w $SU_TEST_FILE
then
    echo "Sorry, you must be root to use addprt"
    exit 2
fi

#
# Make sure user is in correct directory
#

if test ! -d BSD
then
     echo "Please change to the CrownSoft directory before you install the software."
     exit 0
fi

#
# Check whether program was invoked correctly
#
if [ $# -gt 1 ]
then
    echo "Usage: addprt [ printername ]" 1>&2
    exit 1
fi

#
# If the user invoked us with the printername arg, use it
#

NAME=$1
HOSTNAME=$NAME

#
# Option Defaults
#
COMM_OPTION=tcp/ip
BAUD=9600
DEV=
AB_PATH=/etc/printcap
# check for redhat Linux, which may not have /usr/spool
if [ $OSTYPE = Linux ]
then
SPOOLPATH=/var/spool
else
SPOOLPATH=/usr/spool
fi
JOBFILE=NONE
LOGFILE=NONE
BJOBFILE=NONE
BLOGFILE=NONE
VMODE=no
TIME=no
ALTERNATIVE=no
REQUEST_TIME=5
ACCOUNT=no
BCHAN=no
PORT=35
DOC_SUPPORTED=no
UFLAG=no
#
# Tell the user what we're doing
#
echo
echo "Configuring printer $NAME"

CONFIG_DONE=no

while [ $CONFIG_DONE != yes ]
do
    #
    # Prompt user for appropriate information
    #
    
    NAME_DONE=no
    while [ $NAME_DONE != yes ]
    do
        echo "Printer Name [$NAME]: "
	echo
        read REPLY
        case $REPLY in
	    \?)
		echo "Enter the name by which the printer will be referenced by the spooler."
		echo "The printer name is arbitrary, but it must be unique."
	        echo "There can't be any other printers with the same name."
		echo
		;;
	    ?*)
	        NAME=$REPLY
    	        HOSTNAME=$NAME
		NAME_DONE=yes
	        ;;
	    *)
		if [ "$NAME" ]
		then
		    NAME_DONE=yes
		fi
	        ;;
        esac
    done


    ANAME_DONE=no
    while [ $ANAME_DONE != yes ]
    do
        echo "Printer Alternate Name [$ALTNAME]: "
	echo
        read REPLY
        case $REPLY in
            \?)
		echo "Enter an alternate name by which the printer can be referenced."
                echo
                ;;
            ?*)
                ALTNAME=$REPLY
                ANAME_DONE=yes
                ;;
            *)
                ANAME_DONE=yes
                ;;
        esac
    done

    #
    # What communications option should we configure
    #
    GET_COMM_DONE=no
    while [ $GET_COMM_DONE != yes ]
    do
        echo "Printer Communications Protocol (tcp/ip | parallel | serial)[$COMM_OPTION]: "
	echo
        read REPLY
        case $REPLY in
	    s|S|serial|Serial|SERIAL)
	        COMM_OPTION=serial
	        GET_COMM_DONE=yes
	        ;;
	    p|P|parallel|Parallel|PARALLEL)
	        COMM_OPTION=parallel
	        GET_COMM_DONE=yes
	        ;;
	    t|T|tcp/ip|TCP/IP)
		DOC_SUPPORTED=yes
	        COMM_OPTION=tcp/ip
	        GET_COMM_DONE=yes
	        ;;
	    \?)
		echo "Enter the communications protocol used to send data to the printer."
		echo
		;;
	    ?*)
	        echo "$REPLY is not a valid response."
	        ;;
	    *)
		DOC_SUPPORTED=yes
	        GET_COMM_DONE=yes
	        ;;
        esac
    done
	
    #
    # Collect information on the chosen comm. option
    #
    case $COMM_OPTION in
	serial)
	    DEV_DONE=no
	    while [ $DEV_DONE != yes ]
	    do
	        echo "Printer Physical Device Name [$DEV]: "
		echo
	        read REPLY
	        case $REPLY in
		    \?)
			echo "Enter the name of the block/special file which represents the"
			echo "physical device to which the printer is attached."
			echo "Example: /dev/ttya"
			echo
			;;
	            ?*)
	                DEV=$REPLY
			DEV_DONE=yes
		        ;;
		    *)
			if [ "$DEV" ]
			then
			    DEV_DONE=yes
			fi
		        ;;
	        esac
	    done

	    GET_BAUD_DONE=no
	    while [ $GET_BAUD_DONE != yes ]
	    do
	        echo "Printer Baud Rate (9600 | 19200)[$BAUD]: "
		echo
	        read REPLY
	        case $REPLY in
		    19200)
		        BAUD=19200
			GET_BAUD_DONE=yes
		        ;;
		    9600)
		        BAUD=9600
			GET_BAUD_DONE=yes
		        ;;
		    \?)
			echo "Enter the baud rate for which the printer is configured."
			echo
			;;
		    ?*)
			echo "$REPLY is not supported."
			;;
		    *)
			GET_BAUD_DONE=yes
		        ;;
	        esac
	    done

	   GOT_REPLY=no
	   while [ $GOT_REPLY != yes ]
	   do
	   echo "Does your printer support DOC (yes|no) [yes]: "
	   echo
	   read REPLY
	   case $REPLY in
		y|Y|yes|YES)
			DOC_SUPPORTED=yes
			GOT_REPLY=yes
			;;
		n|N|no|NO)
                        DOC_SUPPORTED=no
                        GOT_REPLY=yes
                        ;;

		\?)
			echo "Check your printer owner's manual to determine whether"
			echo "your printer supports Document Option Commands"
			echo
			;;
		?*)
                        echo "Please answer Yes or No"
                        ;;
		*)
			DOC_SUPPORTED=yes
			GOT_REPLY=yes
			;;
	   esac
	   done
	;;
			
	parallel)
            DEV_DONE=no
            while [ $DEV_DONE != yes ]
            do
                echo "Printer Physical Device Name [$DEV]: "
		echo
                read REPLY
                case $REPLY in
                    \?)
                        echo "Enter the name of the block/special file which represents the"
                        echo "physical device to which the printer is attached."
                        echo "Example: /dev/pio1"
                        echo
                        ;;
                    ?*)
                        DEV=$REPLY
                        DEV_DONE=yes
                        ;;
                    *)
                        if [ "$DEV" ]
                        then
                            DEV_DONE=yes
                        fi
                        ;;
                esac
            done
	   GOT_REPLY=no
	   while [ $GOT_REPLY != yes ]
	   do
           echo "Does your printer support DOC (yes|no) [yes]: "     
	   echo
           read REPLY
           case $REPLY in
                y|Y|yes|YES)
                        DOC_SUPPORTED=yes
			GOT_REPLY=yes
                        ;;
		n|N|no|NO)
                        DOC_SUPPORTED=no
                        GOT_REPLY=yes
                        ;;
                \?)
                        echo "Check your printer owner's manual to determine whether"
                        echo "your printer supports Document Option Commands"
                        echo
                        ;;
		?*)
                        echo "Please answer Yes or No"
                        ;;
		*)
			DOC_SUPPORTED=yes
			GOT_REPLY=yes
			;;

           esac
	   done
        ;;

	tcp/ip|*)
	    ENET_DONE=no
	    while [ $ENET_DONE != yes ]
	    do
                echo "Printer Hostname (in /etc/hosts)[$HOSTNAME]: "
		echo
                read REPLY
                case $REPLY in
		    \?)
			echo "Enter the name from the table /etc/hosts that corresponds to the"
			echo "Internet address of the printer."
			echo
			;;
	            ?*)
	                HOSTNAME=$REPLY
			ENET_DONE=yes
	                ;;
	            *)
			ENET_DONE=yes
	                ;;
                esac
	    done
#
#  Get the port number to be used to status1/transport1 connections to the
#  printer.
#
            ENET_DONE=no
            while [ $ENET_DONE != yes ]
            do
 
                echo "Enter the port number to be used when sending jobs to the printer."
		        echo "This value MUST match the value set for Port Assignment through"
		        echo "the printer's keypad. [$PORT]: "
		        echo

                read REPLY
                case $REPLY in
                    \?)
                        echo "By changing this number you can select a different"
                        echo "port number on which to transfer jobs from the"
						echo "host to the printer."
                        echo
                        ;;
                    ?*)
                        PORT=$REPLY
                        ENET_DONE=yes
                        if [ "$REPLY" -gt 9999 ]
                        then
							echo
                            echo "The port number \"$REPLY\" is not in the range 1 to 9999."
							echo
                            ENET_DONE=no
                        elif [ "$REPLY" -lt 1 ]
                        then
							echo
                            echo "The port number \"$REPLY\" is not in the range 1 to 9999."
							echo
                            ENET_DONE=no
                        fi 
                        ;;
                    *)
                        ENET_DONE=yes
                        ;;
                esac
	    done
#
#  Get responses related to the handling of backchannel messages from the
#  printer.
#
            BCHAN_DONE=no
            while [ $BCHAN_DONE != yes ]
            do

                echo "Would you like to receive backchannel information (yes|no) [no]? "
	            echo

            read REPLY
            case $REPLY in
            Yes|yes|Y|y)
                BCHAN=yes

            ENET_DONE=no
            while [ $ENET_DONE != yes ]
            do
                echo "Enter a name for the backchannel job file (do not specify"
                echo "a path), or press ENTER for no backchannel job file."
                echo "The file you specify will be placed in /usr/spool. [$BJOBFILE]:"
                echo

                read REPLY
                case $REPLY in
                    \?)
                        echo "By entering a file name, all the backchannel"
                        echo "messages for a single job will be saved."
			            echo "Press ENTER for no backchannel job file."
                        echo
                        ;;
		            status|qfilter|log)
			            echo "Please choose another name.  The name \"$REPLY\""
			            echo "is already used by the host software."
			            echo
			            ;;
                    ?*) 
						BJOBFILE=$REPLY
						ENET_DONE=yes
                        ;;
                    *)
                        ENET_DONE=yes
                        BCHAN=maybe ;;
                esac
	    done

            ENET_DONE=no
            while [ $ENET_DONE != yes ]
            do
 
                echo "Enter a name for the backchannel log file (do not specify"
                echo "a path), or press ENTER for no backchannel log file."
                echo "The file you specify will be placed in /usr/spool. [$BLOGFILE]:"
		echo
                read REPLY
                case $REPLY in
                    \?)
                        echo "By entering a file name all backchannel messages"
                        echo "will be saved for all jobs. Press ENTER for no "
                        echo "backchannel log file."
                        echo
                        ;;
                    status|qfilter|log)
                        echo "Please choose another name.  The name \"$REPLY\""
                        echo "is already used by the host software."
                        echo
                        ;;
                    ?*)
                        BLOGFILE=$REPLY
                        ENET_DONE=yes
					    BCHAN=yes
                        ;;
                    *)
                        ENET_DONE=yes
                        if [ $BCHAN != yes ]
						then
							BCHAN=no
                        fi
                        ;;
                esac
	    done
            ENET_DONE=no
            while [ $ENET_DONE != yes ]
            do
 
                echo "Would you like to enable Unison compatibility mode (yes|no) [no]? "
                echo
                read REPLY
                case $REPLY in
                    \?)
                        echo "Select yes to ensure proper pagecount reporting"
                        echo "for Unison host software."
                        echo
                        ;;
                    Y|Yes|y|yes)
			if [ $BCHAN != no ]
			then
                        	UFLAG=yes
			else
				echo "ERROR -- no backchannel log or job file specified!"
				echo "Unison compatibility mode NOT enabled.  If you would like"
				echo "to enable Unison compatibility, please run"
				echo "addprt again and specify a backchannel log"
				echo "or job file."
				echo
			fi
                        ENET_DONE=yes
                        ;;
                    *)
                        ENET_DONE=yes
                        ;;
                esac
            done
        BCHAN_DONE=yes
	            ;;
        \?)
            echo "This option enables the logging of backchannel messages to"
            echo "the backchannel log file.  Backchannel messages are any"
            echo "messages returned by the printer during the processing of"
            echo "a job.  These messages may be informational or describe"
            echo "errors which occurred during the processing of your job."
            echo "They are most applicable to the page description languages."
            echo
	            ;;
        *)
            BCHAN_DONE=yes
                ;;
        esac
        done
#
#  Get responses related to the handling of accounting information.
#
            ACCT_DONE=no
            while [ $ACCT_DONE != yes ]
            do

                echo "Would you like to enable accounting (yes|no) [no]? "
	            echo

            read REPLY
            case $REPLY in
            Yes|yes|Y|y)
                ACCOUNT=yes

            ENET_DONE=no
            while [ $ENET_DONE != yes ]
            do
 
                echo "Would you like to use the alternative status message format (yes|no) [no]? "
		echo
                read REPLY
                case $REPLY in
                    \?)
                        echo "The alternative format provides a more generic form"
                        echo "of status message.  Users who plan to use a separate"
			echo "utility to automatically scan the accounting log file"
			echo "and assemble the information may find it easier to"
			echo "work with than the standard message format."
			echo
			echo "Compare the following example of the standard message format:"
			echo "%%[status:interpreting; pagecount:1; interface:Ethernet:q33; user:Username; title:file.ps; time:Mon Feb 15 09:13:50 1993]%%"
			echo
			echo "with the alternative format:"
			echo "0215091749%%[status:busy; source:Ethernet:q33; pagecount:1; user:Username; title:file.ps]%%"
                        echo
			echo "The time information will be in a more standardized, compact"
			echo "form, and printer status messages will have a more standardized format."
			echo "In addition, common error messages, such as paper jam, "
			echo "paper out, no paper tray, etc., will be presented as a more "
			echo "standard phrase, in all lower case, and with a time stamp."
			echo
			echo "For example, the standard format of "
			echo "		%%[PrinterError:ADJUST INPUTBIN]%%"
			echo "would be given as"
			echo "		0402095454%%[PrinterError:no paper tray]%%"
			echo
			echo "Note: The option to display status messages with your system"
			echo "clock's time (i.e. in \"real time\") will automatically be"
			echo "enabled if you choose the alternative format."
			echo
                        ;;
                    y*|Y*)
                        ALTERNATIVE=yes 
			TIME=yes
                        ENET_DONE=yes
                        ;;
                    *)
			ALTERNATIVE=no
                        ENET_DONE=yes
                        ;;
                esac
            done


            ENET_DONE=no
            while [ $ENET_DONE != yes ]
            do
 
                echo "Enter a name for the job status file (do not specify"
		echo "a path), or press ENTER for no job status file."
		echo "The file you specify will be placed in /usr/spool. [$JOBFILE]:"
		echo
                read REPLY
                case $REPLY in
                    \?)
                        echo "By entering a file name all the status messages"
                        echo "for a single job will be saved. Press ENTER for no"
			echo "job status file."
                        echo
                        ;;
		    status|qfilter|log)
			echo "Please choose another name.  The name \"$REPLY\""
			echo "is already used by the host software."
			echo
			;;
                    ?*)
                        JOBFILE=$REPLY
                        ENET_DONE=yes
                        ;;
                    *)
                        ENET_DONE=yes
                        ;;
                esac
	    done

            ENET_DONE=no
            while [ $ENET_DONE != yes ]
            do
 
                echo "Enter a name for the log status file (do not specify"
                echo "a path), or press ENTER for no log status file."
                echo "The file you specify will be placed in /usr/spool. [$LOGFILE]:"
		echo
                read REPLY
                case $REPLY in
                    \?)
                        echo "By entering a file name all the status messages"
                        echo "will be saved for all jobs. Press ENTER for no "
			echo "log status file."
                        echo
                        ;;
                    status|qfilter|log)
                        echo "Please choose another name.  The name \"$REPLY\""
                        echo "is already used by the host software."
                        echo
                        ;;
                    ?*)
                        LOGFILE=$REPLY
                        ENET_DONE=yes
                        ;;
                    *)
                        ENET_DONE=yes
                        ;;
                esac
	    done

            ENET_DONE=no
            while [ $ENET_DONE != yes ]
            do
 
                echo "Enter the number of seconds between each status request.  This"
		echo "value MUST be less than the value set for Connect Timeout through"
		echo "the printer's keypad.  [$REQUEST_TIME]: "
		echo

                read REPLY
                case $REPLY in
                    \?)
                        echo "By changing this number you can increase or decrease"
                        echo "the frequency of status requests made to the printer."
                        echo
                        ;;
                    ?*)
                        REQUEST_TIME=$REPLY
                        ENET_DONE=yes
                        ;;
                    *)
                        ENET_DONE=yes
                        ;;
                esac
	    done
	
	    if [ $TIME != yes ]
	    then
            ENET_DONE=no
            while [ $ENET_DONE != yes ]
            do

                echo "Would you like the status time to be displayed in "
		echo "real time or delta time (real|delta) [delta]? "
		echo
                read REPLY
                case $REPLY in
                    \?)
                        echo "Real time uses your system's clock to record the time"
                        echo "a job begins and ends. Delta time is reported from"
                        echo "the printer and gives the elapsed time in seconds from the"
                        echo "beginning of the job until the time of the current status."
			echo
                        ;;
                    r*|R*)
                        TIME=yes
                        ENET_DONE=yes
                        ;;
                    *)
                        TIME=no
                        ENET_DONE=yes
                        ;;
                esac
            done
	    fi

            ENET_DONE=no
            while [ $ENET_DONE != yes ]
            do

                echo "Would you like to set verbose mode (yes|no) [$VMODE]? "
		echo
                read REPLY
                case $REPLY in
                    \?)
                        echo "If verbose mode is not used, only the final status "
                        echo "entry for each job is placed in the job and log files."
                        echo "If verbose mode is used, each different status message "
                        echo "will be stored in the job and log files."
                        echo "It should be noted that the log file can grow huge"
                        echo "if verbose mode is used and many or large files are"
                        echo "printed."
                        echo
                        ;;
                    Y|y|YES|yes)
                        VMODE=yes
                        ENET_DONE=yes
                        ;;
                    N|n|no|NO)
                        VMODE=no
                        ENET_DONE=yes
                        ;;
                    *)
                        ENET_DONE=yes
                        ;;

                esac
            done

            ACCT_DONE=yes
            ;;
            \?)
               echo "Enabling accounting allows the system administrator to"
               echo "keep up with information like page count, owner, and "
               echo "title of jobs that are printed over the TCP/IP Optional"
	       echo "Interface.  If \"no\" is answered to this question,"
               echo "then only printer error messages will be returned."
	       echo "Consult your printer owner's manual to determine whether"
	       echo "your printer supports TCP/IP accounting."
	       echo
            ;;
            *) 
                ACCOUNT=no
                ACCT_DONE=yes
            ;;


    esac
    done        
    ;;
    esac

#
# Get the path of printcap and the spool directory.
#
PATH_NAME=no
while [ $PATH_NAME != yes ]
do
	#
	# Prompt the users for the correct path of printcap
	#
	echo "Path of printcap [$AB_PATH]:"
	echo
	read REPLY   
	case $REPLY in
	\?)
	  echo "Enter the absolute path of the file, printcap."
 	  echo
	;;
	?*)
	  AB_PATH=$REPLY
	  if test -f $AB_PATH
	  then
		PATH_NAME=yes
	  else
		echo "PATH given does not exist....try again."
	  fi
	  ;;
	*)
	  PATH_NAME=yes
	  ;;
          esac
	done	

PATH_NAME=no
while [ $PATH_NAME != yes ]
do
	#
	# Prompt the users for the correct path of spooler 
	#
	echo "Spooler path [$SPOOLPATH]:"
	echo
	read REPLY   
	case $REPLY in
	\?)
	  echo "Enter the absolute path of the spooler."
 	  echo
	;;
	?*)
	  SPOOLPATH=$REPLY
	  if test -d $SPOOLPATH 
	  then
		PATH_NAME=yes
	  else
		echo "PATH given does not exist....try again."
	  fi
	  ;;
	*)
	  PATH_NAME=yes
	  ;;
          esac
	done	
    #
    # Review and verify information
    #
    echo "You have entered the following configuration information:"
    echo
            echo "Printer Name:                 $NAME"
            echo "Printer Alternate Name:       $ALTNAME"
    case $COMM_OPTION in
	parallel)
	    echo "Physical Device:              $DEV"
	    ;;
	serial)
	    echo "Physical Device:              $DEV"
	    echo "Baud Rate:                    $BAUD"
	    ;;
	tcp/ip|*)
    	    echo "Printer Hostname:             $HOSTNAME"
            echo "Log file name:                $LOGFILE"
            echo "Job file name:                $JOBFILE"
            echo "Backchannel log file name:    $BLOGFILE"
            echo "Backchannel job file name:    $BJOBFILE"
	    echo "Unison compatibility:         $UFLAG"
            echo "Interval between requests:    $REQUEST_TIME"
	    echo "Alternative status mode:      $ALTERNATIVE"
            echo "Display Real Time:            $TIME"
	    echo "Verbose mode:                 $VMODE"
	    ;;
    esac
            echo "PRINTCAP path:                $AB_PATH"
            echo "Spooler path:                 $SPOOLPATH"
    echo
    FINISHED=no
    while [ $FINISHED != yes ]
    do
        echo "Is this information correct (y|n) [y]? "
        read REPLY
    
        case $REPLY in
	    \?)
		echo "Enter 'n' to review/change the configuration information"
		echo
		;;
            N|n|n*|N*|[A-XZa-xz0-9]*)
		FINISHED=yes
	        ;;
            Y|y|Y*|y*|*)
		FINISHED=yes
	        CONFIG_DONE=yes
	        ;;
        esac
    done

done

#
# Set location and printer-name dependent variables
#
CWD=`pwd`
QFILT_HEAD=$CWD/qfilter.header
QFILT_SUFFIX=$CWD/qfilter.suffix
QEF=$CWD/qef
PRINTCAP=$AB_PATH
SPOOLDIR=$SPOOLPATH/$NAME
LOG=$SPOOLDIR/log
STATFILE=$SPOOLDIR/status
SETUPFILE=$SPOOLDIR/setup
TARGET_FILT=$SPOOLDIR/qfilter
PREPROLOGUE=$SPOOLDIR/$NAME.pro
TARGET_QEF=$SPOOLDIR/qef
#
# Generate the parameter list for qef
#

if [ $BCHAN != yes ]
then
	BJOBFILE=/dev/null
        BLOGFILE=/dev/null
fi

    PARAMETERS="\" -s \$STATFILE -h \$HOSTNAME "

    if [ $PORT -ne 35 ]
    then
        PARAMETERS="$PARAMETERS -p \$PORT"
    fi

#    if [ $BJOBFILE != NONE ]
#    then 
#        PARAMETERS="$PARAMETERS -b \$BJOBFILE "
#    fi
     PARAMETERS="$PARAMETERS -b \$BJOBFILE "

#    if [ $BLOGFILE != NONE ]
#    then 
#        PARAMETERS="$PARAMETERS -B \$BLOGFILE "
#    fi
     PARAMETERS="$PARAMETERS -B \$BLOGFILE "

    if [ $JOBFILE != NONE ]
    then 
        PARAMETERS="$PARAMETERS -j \$JOBFILE "
    fi

    if [ $LOGFILE != NONE ]
    then 
        PARAMETERS="$PARAMETERS -l \$LOGFILE "
    fi
	
    if [ $ALTERNATIVE != no ]
    then
	PARAMETERS="$PARAMETERS -a "
    fi

    if [ $TIME != no ]
    then
        PARAMETERS="$PARAMETERS -r "
    fi

    if [ $VMODE != no ]
    then
        PARAMETERS="$PARAMETERS -v "
    fi

    if [ $UFLAG != no ]
    then
	 PARAMETERS="$PARAMETERS -u "
    fi

    if [ $ACCOUNT != yes ]
    then
		if [ $BCHAN != yes ]
        then
            PARAMETERS="$PARAMETERS -o "
        fi
    fi

    PARAMETERS="$PARAMETERS -g \$REQUEST_TIME"
    PARAMETERS="$PARAMETERS\" " 

#
# Create printcap entry
#
if [ $ALTNAME ]
then
    echo "$NAME|$ALTNAME:\\" >> $PRINTCAP
else
    echo "$NAME:\\" >> $PRINTCAP
fi
case $COMM_OPTION in
    parallel)
	echo "	:lp=$DEV:\\" >> $PRINTCAP
	;;
    serial)
	echo "	:lp=$DEV:\\" >> $PRINTCAP
	echo "	:br#$BAUD:\\" >> $PRINTCAP
	echo "	:ms=-parity,raw,ixon,ixoff:\\" >> $PRINTCAP
	;;
    tcp/ip|*)
	echo "	:lp=$SPOOLDIR/devnull:\\" >> $PRINTCAP
	;;
esac
echo "        :lf=$LOG:\\" >> $PRINTCAP
echo "        :sd=$SPOOLDIR:\\" >> $PRINTCAP
#echo "        :lo=lock:\\" >> $PRINTCAP
echo "        :mx#0:\\" >> $PRINTCAP
echo "        :rw:\\" >> $PRINTCAP
echo "        :sh:\\" >> $PRINTCAP
echo "        :if=$TARGET_FILT:" >> $PRINTCAP

#
# Create the Spool Directory,and open a log and a devnull in it
# (avoiding embarassing error messages)
#
mkdir $SPOOLDIR
cp $QEF $SPOOLDIR
touch $LOG
touch $STATFILE
chown daemon $STATFILE
if [ $COMM_OPTION = "tcp/ip" ]
then
    touch $SPOOLDIR/devnull
fi

#
# Build and install qfilter
#

cat $QFILT_HEAD > $TARGET_FILT

#
# Communication Protocol Specifics
#

echo "DOC_SUPPORTED=$DOC_SUPPORTED" >> $TARGET_FILT
echo "COMM_OPTION=$COMM_OPTION" >> $TARGET_FILT
echo "HOSTNAME=$HOSTNAME" >> $TARGET_FILT
echo "QEF=$TARGET_QEF" >> $TARGET_FILT
echo "LOG=$LOG" >> $TARGET_FILT
echo "PORT=$PORT" >> $TARGET_FILT
echo "STATFILE=$STATFILE" >> $TARGET_FILT
echo "SETUPFILE=$SETUPFILE" >> $TARGET_FILT
echo "PREPROLOGUE=$PREPROLOGUE" >> $TARGET_FILT
if [ $BCHAN != yes ]
then
echo "BJOBFILE=/dev/null" >> $TARGET_FILT
echo "BLOGFILE=/dev/null" >> $TARGET_FILT
else
echo "BJOBFILE=$SPOOLDIR/$BJOBFILE" >> $TARGET_FILT
echo "BLOGFILE=$SPOOLDIR/$BLOGFILE" >> $TARGET_FILT
fi
echo "JOBFILE=$SPOOLDIR/$JOBFILE" >> $TARGET_FILT
echo "LOGFILE=$SPOOLDIR/$LOGFILE" >> $TARGET_FILT
echo "REQUEST_TIME=$REQUEST_TIME" >> $TARGET_FILT
echo "PARAMETERS=$PARAMETERS" >> $TARGET_FILT
echo "Would you like to filter your output? (yes|no) [no] "
echo "For example: prtpost -L -2  would produce two column, landscape"
echo "listings of all files sent to the queue."
read REPLI
	case $REPLI in
	YES|yes|Y|y)
		echo
		echo
		echo "Please key in the exact command-line syntax to invoke the"
                echo " filter (including all necessary flags and the absolute path)."
		read FILTER
                LOG_FILTER=$SPOOLDIR/log_filter
                echo "FILQEF=$LOG_FILTER" >> $TARGET_FILT
                touch $LOG_FILTER
		echo "if test -r $PREPROLOGUE" > $LOG_FILTER
                echo "then" >> $LOG_FILTER
                echo "cat $PREPROLOGUE" >> $LOG_FILTER
                echo "fi" >> $LOG_FILTER
		echo "cat - | $FILTER" >> $LOG_FILTER
                chmod 755 $LOG_FILTER
        ;;
        *)
		echo "FILQEF=\"cat -\"" >> $TARGET_FILT
		echo "CATQEF=\$PROLOGUE" >> $TARGET_FILT
	;;
	esac

#
# ... and the rest
#

cat $QFILT_SUFFIX >> $TARGET_FILT
chmod 775 $TARGET_FILT

#
# Set correct permissions,ownership, and group so lpd won't complain
#
chown daemon $SPOOLDIR $LOG
chgrp daemon $SPOOLDIR $LOG
chmod 775 $SPOOLDIR
chmod 774 $LOG
if [ $COMM_OPTION = "tcp/ip" ]
then
    chmod 664 $SPOOLDIR/devnull
fi

if [ $DOC_SUPPORTED = yes ]
then
#
# Configure the logical to have user specified characteristics.
#
     export PREPROLOGUE
     ./addlog
fi

#
# Change permissions for log and status file because of Linux distribution
chmod 777 $LOG
chmod 777 $STATFILE

#
# All done
#
echo
echo "Printer $NAME configured"
echo
exit 0
