:
# @(#) WordPerfect Printer Editor - version 6.0
# Copyright (C) 1990 Corel Corporation Limited, All Rights Reserved

if [ "x$1" = x-h -o "x$1" = x-H -o "x$1" = x-help -o "x$1" = x-Help ]; then
	echo "wpped"
	echo "Corel WordPerfect 8.1"
	echo "Copyright Corel Corp. 1999"
	echo "This utility allows fully formatted WordPerfect documents to be"
	echo "sent directly to the printer."
	echo ""
	echo "Usage: wpped [-h -help]"
	echo "Where:"
	echo "	-h, -help : This message. Type \"man wpped\" for more detail."
	exit;
fi

trap 'echo ""; echo "wpped aborted"; echo ""; exit 2' 1 2 3 15

# determine clear screen method
CLS='clear'
if [ -z "`eval 'clear' 2>/dev/null`" ]
then
	CLS='tput clear'
	if [ -z  "`eval 'tput clear' 2>/dev/null`" ]
	then
		CLS='echo "";echo "";echo "";echo "";echo "";echo "";echo "";echo "";echo "";echo "";echo "";echo "";echo "";echo "";echo "";echo "";echo "";echo "";echo "";echo "";echo "";echo "";echo "";echo "";echo "";'
	fi
fi

eval $CLS
echo ""
echo ""
echo ""
echo "                            ***********************"
echo "                            *                     *"
echo "                            *        Wpped        *"
echo "                            *                     *"
echo "                            ***********************"
echo ""
echo "                               (C)Copyright 1990"
echo "                              All Rights Reserved"
echo "                            Corel Corporation Limited"
echo ""
echo ""

sleep 3
eval $CLS

# Initialize some variables
BSD=
SYSV=
DUAL=
WP=
P_DEST=
P_OPTION=
BAUD=
PARITY=
DATABITS=
HANDSHAKE=
SLEEP=
LOCALSTR='# Please Do Not Remove or Move This Line, wpdest uses it'
LOCALSTROLD='# Please Do Not Remove or Move This Line, wpport uses it'
OTHERSTR='# Please do not remove or move this line, WPPORT uses it'
NL1="-n"
NL2=
CF=
DF=
GF=
NF=
RF=
TF=
VF=
REPLACE=
WRITE=1
FILE=

# Determine spooler type
if [ -f /etc/printcap ]
then
	BSD=/etc/printcap
fi

if [ -d /etc/lp/interfaces ]
then
	SYSV=/etc/lp/interfaces
elif [ -d /usr/spool/lp/admins/lp/interfaces ]
then
	SYSV=/usr/spool/lp/admins/lp/interfaces
elif [ -d /usr/spool/lp/interface ]
then
	SYSV=/usr/spool/lp/interface
fi

if [ "$BSD" -a "$SYSV" ]
then
	DUAL=yes
elif [ ! "$BSD" -a ! "$SYSV" ]
then
	echo "Error: Could not identify the type of print spooler on this system."
	echo ""
	echo ""
	exit 1
fi

if [ -n "`echo -n`" ]
then
	NL1=
	NL2="\c"
fi

echo "  __________________________________________________________________________"
echo " |      This program will modify the necessary print spooler files to allow |"
echo " | print jobs to be printed without character translation.  After running   |"
echo " | wpped, fully formatted WordPerfect documents are able to pass            |"
echo " | untranslated by the local spooling system to the printer.                |"
echo " |                                                                          |"
if [ "$BSD" ]
then
	echo " |      The /etc/printcap file will be backed up as \"printcap.wbk\" should   |"
	echo " | you wish to restore the original copy.                                   |"
	if [ "$SYSV" ]
	then
		echo " |                                                                          |"
	fi
fi
if [ "$SYSV" ]
then
	echo " |      The print filter edited will be backed up as \"<printer name>.wbk\"   |"
	echo " | should you wish to restore the original copy.                            |"
fi

echo "  __________________________________________________________________________"
echo ""
echo $NL1 " Do you wish to continue? (Y/N) $NL2"
read ans

while [ ans ]
do
	case $ans in
		n|N)
			exit 0
		;;
		y|Y)
			break
		;;
	esac
	read ans
done

eval $CLS

echo ""
echo $NL1 " What is the printer destination name? $NL2"
read P_DEST
echo ""

if [ "$DUAL" ]
then
	echo "Which spooling system is used for this printer?"
	echo ""
	echo "     1. Lp Spooler  2. Lpr Spooler"
	echo ""
	echo $NL1 "Selection: $NL2"
	read spool
	while [ spool ]
	do
		case $spool in
			1|a|A) BSD=
				 break
			;;
			2|b|B) SYSV=
				 break
			;;
		esac
		echo $NL1 "$spool is not an option, please choose again: $NL2"
		read spool
	done
	echo ""
fi

# Check for write permissions on the file we want to change
if [ "$BSD" ]
then
	if [ ! -w /etc/printcap ]
	then
		FILE=$BSD
		WRITE=
	fi
elif [ ! -w $SYSV/$P_DEST ]
then
	FILE=$SYSV/$P_DEST
	WRITE=
fi
if [ ! "$WRITE" ]
then
	echo ""
	echo "Error: Wpped cannot modify printer $P_DEST because you do not have"
	echo "       permissions to write to the $FILE file."
	echo ""
	exit 0
fi

if [ "$BSD" ]
then
	echo ""
	echo "* Please wait *"
	echo ""

# Get the starting line number for this entry
	startline=`egrep -n "(^|\|)$P_DEST(:|\|)" /etc/printcap | awk -F':' '{ print $1 }'`
	for testline in $startline
	do
		comment=`sed -n "$testline p" /etc/printcap | egrep '(^#)'`
		if [ ! "$comment" ]
		then
			break
		fi
		testline=
	done
	startline=$testline

	if [ ! "$startline" ]
	then
		eval $CLS
		echo ""
		echo "Error: Unable to locate the $P_DEST entry in the printcap file.  $P_DEST does"
		echo "       not exist or has been commented out with the pound (#) sign."
		echo ""
		exit 1
	fi

# Get the ending line number for this entry
	endline=$startline
	while [ 1 ]
	do
		slash=`sed -n "$endline p" /etc/printcap | awk '$1 ~ /:$/ { print $1 }'`
		if [ "$slash" ]
		then
			break
		fi
		endline=`expr $endline + 1`
	done

# Get the filter option string for each type
	getoption()
	{
		OPTSTR=`sed -n "$startline,$endline p" /etc/printcap | fgrep -n ":$OPTVAL=" | awk -F':' '{ print $1 }'`
		if [ "$OPTSTR" ]
		then
			num=1
			line=`expr $startline + $OPTSTR - 1`
			while [ 1 ]
			do
				OPTSTR=`sed -n "$line p" /etc/printcap | awk -F':' "$"$num" ~ /^"$OPTVAL"=/ { print $"$num" }"`
				if [ "$OPTSTR" ]
				then
					break
				fi
				num=`expr $num + 1`
			done
		fi
	}
	OPTVAL=cf
	getoption
	CF=$OPTSTR
	OPTVAL=nf
	getoption
	NF=$OPTSTR
	OPTVAL=rf
	getoption
	RF=$OPTSTR
	OPTVAL=gf
	getoption
	GF=$OPTSTR
	OPTVAL=vf
	getoption
	VF=$OPTSTR
	OPTVAL=df
	getoption
	DF=$OPTSTR
	OPTVAL=tf
	getoption
	TF=$OPTSTR

	eval $CLS
	echo ""
	echo "Please select one of the following filter options for this printer."
	echo ""
	echo "     Filter Option                  Current Filter Option String"
	echo "     -------------                  ----------------------------"
	echo ""
	echo "     1. Cifplot filter (-c)         $CF"
	echo "     2. Ditroff filter (-n)         $NF"
	echo "     3. Fortran filter (-r)         $RF"
	echo "     4. Graph Data filter (-g)      $GF"
	echo "     5. Raster Image filter (-v)    $VF"
	echo "     6. Text filter (-d)            $DF"
	echo "     7. Troff filter (-t)           $TF"
	echo ""
	echo "     0. Not sure, exit for now."
	echo ""
	echo $NL1 "Selection: $NL2"
	read opt
	echo ""
	while [ opt ]
	do
		case $opt in
			1|c|C)
				opt=c
				P_OPTION=cf
				opt_str=$CF
				break
			;;
			2|n|N)
				opt=n
				P_OPTION=nf
				opt_str=$NF
				break
			;;
			3|f|F|r|R)
				opt=r
				P_OPTION=rf
				opt_str=$RF
				break
			;;
			4|g|G)
				opt=g
				P_OPTION=gf
				opt_str=$GF
				break
			;;
			5|v|V)
				opt=v
				P_OPTION=vf
				opt_str=$VF
				break
			;;
			6|d|D)
				opt=d
				P_OPTION=df
				opt_str=$DF
				break
			;;
			7|t|T)
				opt=t
				P_OPTION=tf
				opt_str=$TF
				break
			;;
			0|x|X|e|E)
				echo ""
				exit 0
			;;
		esac
		echo $NL1 "$opt is not an option, please choose again: $NL2"
		read opt
		echo ""
	done

# If filter option is used, prompt for replacement.
# Is it a system filter or wppscript? If wppscript, is it a 5.0 or 5.1/6.0 version?
	if [ $opt_str ]
	then
		localwpp=`echo "$opt_str" | sed 's/^.*\///p'`
		if [ "$localwpp" = wppscript ]
		then
			WPPS=`echo "$opt_str" | awk -F'=' '{ print $2 }'`
			WPVER='5.1'
			if [ -f $WPPS ]
			then
				WPPS=`grep -c wpp50 $WPPS`
				if [ $WPPS -gt 0 ]
				then
					WPVER='5.0'
				fi
			fi
			echo "Warning: $P_OPTION is an option currently being used by the $P_DEST"
			echo "         printcap entry for local WordPerfect $WPVER printing."
			echo ""
		fi
		echo $NL1 "Replace $opt_str? (Y/N) $NL2"
		read ans
		while [ ans ]
		do
			case $ans in
				n|N)
					eval $CLS
					echo ""
					echo "     You will need to choose an option that can be used for WordPerfect"
					echo "printing.  When you have done so, rerun this program and enter the appropriate"
					echo "filter option.  After running this program, rerun xwpdest (wpdest) and edit the"
					echo "destination for this printer and insert into the spool string the correct"
					echo "filter option (i.e., if vf is selected, insert a -v in the spool string)."
					echo ""
					echo "Program terminated, no changes made to the printcap."
					echo ""
					echo ""
					exit 0
				;;
				y|Y)
					REPLACE=1
					break
				;;
			esac
			read ans
		done
		echo ""
	fi
fi

eval $CLS

echo "Is this printer attached to a serial or parallel port?"
echo ""
echo "     1. Serial     2. Parallel"
echo ""
echo $NL1 "Selection: $NL2"
read line
echo ""
while [ line ]
do
	case $line in
		1|s|S)
			echo "What is the baud rate of this printer?"
			echo ""
			echo "     1. 9600     2. 19200    3. 38400    4. other"
			echo ""
			echo $NL1 "Selection: $NL2"
			read baud
			echo ""
			while [ baud ]
			do
				case $baud in
					1|a|A) BAUD=9600
							break
							;;
					2|b|B) BAUD=19200 
							break
							;;
					3|c|C) BAUD=38400
							break
							;;
					4|o|O) echo $NL1 "Enter Baud rate: $NL2"
						read BAUD
						break
					;;
				esac
				echo $NL1 "$baud is not an option, please choose again: $NL2"
				read baud
			done
			echo ""
			echo "What is the parity of this printer?"
			echo ""
			echo "     1. None      2. Even     3. Odd"
			echo ""
			echo $NL1 "Selection: $NL2"
			read par
			echo ""
			while [ par ]
			do
				case $par in
					3|o|O) PARITY="parenb parodd" 
							break
							;;
					2|e|E) PARITY="parenb -parodd" 
							break
							;;
					1|n|N) PARITY="-parenb" 
							break
							;;
				esac
				echo $NL1 "$par is not an option, please choose again: $NL2"
				read par
			done
			echo ""
			echo "How many data bits does the line send?"
			echo ""
			echo "     1. Eight    2. Seven"
			echo ""
			echo $NL1 "Selection: $NL2"
			read bits
			echo ""
			while [ bits ]
			do
				case $bits in
					2|s|S|7) DATABITS="cs7" 
							break
							;;
					1|e|E|8) DATABITS="cs8" 
							break
							;;
				esac
				echo $NL1 "$bits is not an option, please choose again: $NL2"
				read bits
			done
			echo ""
			echo "What type of handshaking does the line require?"
			echo ""
			echo "     1. Software    2. Hardware"
			echo ""
			echo $NL1 "Selection: $NL2"
			read hand
			echo ""
			while [ hand ]
			do
				case $hand in
					2|h|H)
						if [ -f /xenix ]
						then
							HANDSHAKE="-ixon -clocal ctsflow cread"
						else
							HANDSHAKE="-ixon -clocal cread"
						fi
						break
							;;
					1|s|S) HANDSHAKE="ixon clocal cread" 
							break
							;;
				esac
				echo $NL1 "$hand is not an option, please choose again: $NL2"
				read bits
			done
			echo ""
			echo "If you want a delay to occur between each print job, enter the"
			echo "delay value in seconds (default = 0)."
			echo ""
			if [ "$BSD" ]
			then
				SLEEP=10
			fi
			echo $NL1 "Seconds-> $NL2"
			read SLEEP
			echo ""
			while [ $SLEEP ]
			do
				bnum1=`echo $SLEEP | sed -n '/[^0-9]/ p'`
				if [ "$bnum1" ]
				then
					echo $NL1 "$SLEEP is not a numeric value, please reenter value: $NL2"
					read SLEEP
					continue
				fi
				if [ "$SLEEP" -gt 255 ]
				then
					SLEEP=255
				fi
				break
			done
			break
		;;
		2|p|P)
			break
		;;
	esac
	echo $NL1 "$line is not an option, please choose again: $NL2"
	read line
done

echo ""
echo ""
echo "* Please Wait *"
echo ""

# Remove temp files if they exists
rm -f /tmp/.WPPED_tmp_._1
rm -f /tmp/.WPPED_tmp_._2

# If System V Spooler, begin here
if [ "$SYSV" ]
then
	if [ ! -f $SYSV/$P_DEST ]
	then
		eval $CLS
		echo ""
		echo "Error: "$P_DEST" is not found in directory $SYSV."
		echo ""
		echo "Please verify the existence of "$P_DEST" or its spelling.  You will need"
		echo "to rerun this program and enter the correct printer name."
		echo ""
		exit 1
	fi

# Backup printer interface program
	if [ -w $SYSV ]
	then
		cp $SYSV/$P_DEST $SYSV/"$P_DEST".wbk
	else
		cp $SYSV/$P_DEST /tmp/"$P_DEST".wbk
	fi

# Check for WPOTHER script info (5.0 & 5.1/6.0).  Remove all found
	while [ 1 ]
	do
		OTHERLINES=`fgrep -n "$OTHERSTR" $SYSV/$P_DEST | awk -F':' '{ print $1 }'`
		for startline in $OTHERLINES
		do
			endline=`expr $startline + 3`
			other=`sed -n "$endline p" $SYSV/$P_DEST | awk '/WPOTHER/ { print $5 }'`
			if [ "$other" ]
			then
				endline=`expr $endline + 6`
				lastline=`expr $endline + 8`
				while [ $endline -lt $lastline ]
				do
					str=`sed -n "$endline p" $SYSV/$P_DEST`
					if [ "$str" = "$OTHERSTR" ]
					then
						break
					fi
					endline=`expr $endline + 1`
					str=
				done
				if [ "$str" ]
				then
					testline=`expr $startline - 1`
					testline=`sed -n "$testline p" $SYSV/$P_DEST`
					if [ "$testline" = '#! /bin/sh' ]
					then
						startline=`expr $startline - 1`
					fi
					sed "$startline,$endline d" $SYSV/$P_DEST > /tmp/.WPPED_tmp_._1
					cp /tmp/.WPPED_tmp_._1 $SYSV/$P_DEST
					rm -f /tmp/.WPPED_tmp_._1
					continue 2
				fi
			fi
		done
		break
	done

# Check for local script.  Write other script after 5.0, don't write if 5.1/6.0
	startline=`fgrep -n "$LOCALSTR" $SYSV/$P_DEST | awk -F':' '{ print $1 }' | sed -n '1 p'`
	if [ ! "$startline" ]
	then
		startline=`fgrep -n "$LOCALSTROLD" $SYSV/$P_DEST | awk -F':' '{ print $1 }' | sed -n '1 p'`
		if [ "$startline" ]
		then
			LOCALSTR=$LOCALSTROLD
		fi
	fi
	endline=`fgrep -n "$LOCALSTR" $SYSV/$P_DEST | awk -F':' '{ print $1 }' | sed -n '2 p'`
	if [ "$startline" ]
	then
		WPFLG=`expr $startline + 3`
		WPFLG=`sed -n "$WPFLG p" $SYSV/$P_DEST | awk '/wpp/ { print $1 }'`
	else
		WPFLG=
	fi
	if [ "$WPFLG" ]
	then

# Replace, insert, or remove stty string
		rm -f /tmp/.WPPED_tmp_._1
		sttyline=`sed -n "$startline,$endline p" $SYSV/$P_DEST | grep -n stty | awk -F':' '{ print $1 }'`
		if [ "$BAUD" ]
		then
			if [ "$sttyline" ]
			then
				sttyline=`expr $sttyline + $startline - 1`
				sed "$sttyline s/^.*/		stty $BAUD $DATABITS $PARITY $HANDSHAKE -opost 0\<\&1/" $SYSV/$P_DEST > /tmp/.WPPED_tmp_._1
			else
				sed "11G" $SYSV/$P_DEST | sed "12 s/^/		stty $BAUD $DATABITS $PARITY $HANDSHAKE -opost 0\<\&1/" > /tmp/.WPPED_tmp_._1
			fi
		elif [ "$sttyline" ]
		then
			sttyline=`expr $sttyline + $startline - 1`
			sed "$sttyline d" $SYSV/$P_DEST > /tmp/.WPPED_tmp_._1
		fi
		if [ -f /tmp/.WPPED_tmp_._1 ]
		then
			cp /tmp/.WPPED_tmp_._1 $SYSV/$P_DEST
			rm -f /tmp/.WPPED_tmp_._1
		fi

# Replace, insert, or remove sleep string
		sleepline=`sed -n "$startline,$endline p" $SYSV/$P_DEST | grep -n sleep | awk -F':' '{ print $1 }'`
		if [ "$SLEEP" -a "$SLEEP" != "0" ]
		then
			if [ "$sleepline" ]
			then
				sleepline=`expr $sleepline + $startline - 1`
				sed "$sttyline s/^.*/			sleep $SLEEP/" $SYSV/$P_DEST > /tmp/.WPPED_tmp_._1
			else
				catline=`sed -n "$startline,$endline p" $SYSV/$P_DEST | grep -n cat | awk -F':' '{ print $1 }'`
				sleepline=`expr $catline + $startline - 2`
				catline=`expr $sleepline + 1`
				sed ""$sleepline"G" $SYSV/$P_DEST | sed "$catline s/^/			sleep $SLEEP/" > /tmp/.WPPED_tmp_._1
			fi
		elif [ "$sleepline" ]
		then
			sleepline=`expr $sleepline + $startline - 1`
			sed "$sleepline d" $SYSV/$P_DEST > /tmp/.WPPED_tmp_._1
		fi
		if [ -f /tmp/.WPPED_tmp_._1 ]
		then
			cp /tmp/.WPPED_tmp_._1 $SYSV/$P_DEST
			rm -f /tmp/.WPPED_tmp_._1
		fi
	else
		if [ "$startline" = 2 -a "$endline" = 11 ]
		then
			sed -n '1,11 p' $SYSV/$P_DEST > /tmp/.WPPED_tmp_._1
			sed '1,11 d' $SYSV/$P_DEST > /tmp/.WPPED_tmp_._2
			cp /tmp/.WPPED_tmp_._2 $SYSV/$P_DEST
			rm -f /tmp/.WPPED_tmp_._2
		else
			echo "#! /bin/sh" >> /tmp/.WPPED_tmp_._1
		fi

# Create new printer filter which includes WPOTHER script information
		echo $LOCALSTR >> /tmp/.WPPED_tmp_._1
		echo "for option in \$5" >> /tmp/.WPPED_tmp_._1
		echo "do" >> /tmp/.WPPED_tmp_._1
		echo "	if [ -x \$option/wpp ]" >> /tmp/.WPPED_tmp_._1
		echo "	then" >> /tmp/.WPPED_tmp_._1
		echo "		\$option/wpp <\$6" >> /tmp/.WPPED_tmp_._1
		echo "		exit 0" >> /tmp/.WPPED_tmp_._1
		echo "	elif [ \$option = WPREMOTE -o \$option = WPOTHER ]" >> /tmp/.WPPED_tmp_._1
		echo "	then" >> /tmp/.WPPED_tmp_._1
		echo "		copies=\$4" >> /tmp/.WPPED_tmp_._1
		if [ "$BAUD" ]
		then
			echo "		stty $BAUD $DATABITS $PARITY $HANDSHAKE -opost 0<&1" >> /tmp/.WPPED_tmp_._1
		fi
		echo "		while [ \"\$copies\" -gt 0 ]" >> /tmp/.WPPED_tmp_._1
		echo "		do" >> /tmp/.WPPED_tmp_._1
		echo "			cat \$6 2>&1" >> /tmp/.WPPED_tmp_._1
		if [ "$SLEEP" -a "$SLEEP" != "0" ]
		then
			echo "			sleep $SLEEP"  >> /tmp/.WPPED_tmp_._1
		fi
		echo "			copies=\`expr \$copies - 1\`" >> /tmp/.WPPED_tmp_._1
		echo "		done" >> /tmp/.WPPED_tmp_._1
		echo "		exit 0"  >> /tmp/.WPPED_tmp_._1
  		echo "	fi"  >> /tmp/.WPPED_tmp_._1
		echo "done"  >> /tmp/.WPPED_tmp_._1
		echo $LOCALSTR >> /tmp/.WPPED_tmp_._1
		cat $SYSV/$P_DEST >> /tmp/.WPPED_tmp_._1
		cp /tmp/.WPPED_tmp_._1 $SYSV/$P_DEST
		rm -f /tmp/.WPPED_tmp_._1
	fi

	chmod 777 $SYSV/$P_DEST 2>/dev/null

	eval $CLS
	echo ""
	echo "     The $P_DEST printer filter has been modified successfully for"
	echo "WordPerfect printing.  If you are using \"Custom Spool Command\" as"
	echo "the WPApp destination for this spooler destination, you must do"
	echo "the following to ensure WordPerfect will take advantage of the changes:"
	echo ""
	echo "     1. Rerun xwpdest (wpdest)."
	echo "     2. Edit the Custom Spool Command WPApp destination for $P_DEST."
	echo "     3. Select \"Spooler Command\"".
	echo "     4. Insert into the string after the \"lp\" command and before"
	echo "        the \"<f>\" the following option:  -oWPOTHER"
	echo ""
	echo "        Example: lp -c -d$P_DEST -oWPOTHER <f>"
	echo ""
	echo "     5. Exit and Save the WPApp destinations."
	echo ""
	echo ""
fi

if [ "$BSD" ]
then

# Backup printcap
	if [ -w /etc ]
	then
		BAK=/etc
	else
		BAK=/tmp
	fi
	if [ -f $BAK/printcap.wbk ]
	then
		mv $BAK/printcap.wbk $BAK/printcap.wbk2
	fi
	cp /etc/printcap $BAK/printcap.wbk

# Get the spool directory for this entry for script location
	OPTVAL=sd
	getoption
	spooldir=$OPTSTR
	if [ "$spooldir" ]
	then
		spooldir=`echo $spooldir | sed 's/sd=//g'`
	else
		firstname=`sed -n "$startline p" /etc/printcap | awk -F'|' '{ print $1 }' | awk -F':' '{ print $1 }'`
		firstname=`expr "$firstname" \| "$P_DEST" | sed 's/ /_/g'`
		spooldir=/var/spool/$firstname
	fi
	if [ ! -w $spooldir ] 
	then
		spooldir=
	fi

# Get users choice for the script directory for this printer
	eval $CLS
	scriptname=`echo "$P_DEST"wp | sed 's/ /_/g'`
	echo ""
	echo "     In order for proper communications to exist between the printer and the"
	echo "port, a script is created which will be invoked from the printcap each time"
	echo "a WordPerfect document is printed."
	if [ $spooldir ]
	then
		echo ""
		echo "     The default directory for this script is,"
		echo ""
		echo "     $spooldir"
		echo ""
		echo "If the default directory is correct, press Return."
		echo "If the default directory is not correct, enter the correct one."
	else
		echo "     You will need write permissions to this directory in order to create"
		echo "this script."
		echo ""
		echo "Please specify this directory."
	fi
	echo ""
	echo $NL1 "-> $NL2"
	read location

	if [ "$location" ]
	then
		spooldir=$location
	fi
	echo ""

# If script directory does not exist, make it or error out
	if [ "$spooldir" -a ! -d "$spooldir" ]
	then
		echo "Creating directory $spooldir . . ."
		mkdir -p $spooldir
		chmod 777 $spooldir 2>/dev/null
		echo ""
	fi
	if [ ! "$spooldir" -o ! -d "$spooldir" ]
	then
		echo "Error: Could not create or find the script directory $spooldir"
		echo ""
		exit 1
	fi
	if [ ! -w $spooldir -a ! -f $spooldir/$scriptname ]
	then
		echo "Error: Insufficient privileges to create $scriptname"
		echo "       in $spooldir."
		echo ""
		exit 1
	fi

	seddir=`echo "$spooldir" | sed 's/\// /g' | sed 's/ /\\\ /g' | sed 's/ /\//g'`

# Now write the new filter option string for this entry
	if [ $REPLACE ]
	then
		opt_str=`echo $opt_str | tr '/' '.'`
		sed "$optionline s/$opt_str/$P_OPTION=$seddir\/$scriptname/" /etc/printcap > /tmp/.WPPED_tmp_._1
	else
		newline=`expr $endline + 1`
		sed ""$endline"G" /etc/printcap | sed "$endline s/:$/:\\\/" | sed "$newline s/^/	:$P_OPTION=$seddir\/$scriptname:/" > /tmp/.WPPED_tmp_._1
	fi
	cp /tmp/.WPPED_tmp_._1 /etc/printcap
	rm -f /tmp/.WPPED_tmp_._1
	echo ""

# Backup old script
	if [ -w $spooldir -a -f $spooldir/$scriptname ]
	then
		mv $spooldir/$scriptname $spooldir/"$scriptname".old
	fi

# Create the script
	echo "#!/bin/sh" >> /tmp/.WPPED_tmp_._1
	if [ "$BAUD" ]
	then
		echo "stty $BAUD $DATABITS $PARITY $HANDSHAKE -opost 0<&1" >> /tmp/.WPPED_tmp_._1
	fi
	echo "cat 2>&1" >> /tmp/.WPPED_tmp_._1
	if [ "$SLEEP" -a "$SLEEP" != "0" ]
	then
		echo "sleep $SLEEP" >> /tmp/.WPPED_tmp_._1
	fi
	echo "exit 0" >> /tmp/.WPPED_tmp_._1
	cp /tmp/.WPPED_tmp_._1 $spooldir/$scriptname
	rm -f /tmp/.WPPED_tmp_._1
	chmod 777 $spooldir/$scriptname 2>/dev/null

	eval $CLS
	echo ""
	echo "     The printcap entry has been modified successfully for WordPerfect"
	echo "printing.  The printcap will now execute a script located in"
	echo "$spooldir called \"$scriptname\".  Now that this printer is modified, you"
	echo "must do the following if the WPApp destination you are using is the type"
	echo "\"Custom Spool Command\":"
	echo ""
	echo "     1. Rerun xwpdest (wpdest)."
	echo "     2. Edit the Custom Spool Command WPApp destination for $P_DEST."
	echo "     3. Select \"Spooler Command\"".
	echo "     4. Insert into the string after the \"lpr\" command and before"
	echo "        the \"<f>\" the following option:  -$opt"
	echo ""
	echo "        Example: lpr -$opt -P$P_DEST <f>"
	echo ""
	echo "     5. Exit and Save the WPApp destinations."
	echo ""
	echo ""
fi
exit 0

