#! /bin/sh
#    Digital TCP/IP printer daemon installation script.
#
# Installation script for the Digital TCP/IP host resident software.
#
# 1) Log in as root or su root
# 2) Copy the files from the installation floppy into your installation 
#    directory.
#	cd /usr/nic
#	tar -xvf  /dev/rfd0
# 3) Execute the install script from the installation directory.
#	./tcpinst
# 4) Answer the questions and follow the instructions as directed by the
#    install script.
# 5) Test by printing a file. If you have trouble look at the troubleshooting
#    guide.
#
# The install script is straight forward and follows the basic
# algorithm which is described as follows:
#
#   1) Verify and initialize the environment as follows:
#
#       Setup system aliases and globals
#
#	Make sure running as root
#
#       Check that all needed Digital utilities are in this directory or in a
#       subdirectory named SYSTEM (for supported systems only), where SYSTEM
#       is found by the uname (-s) command. For systems with a user changeable
#       system name the user is prompted to get the system type.
#
#   2) Copy the installation files into the target directories
#      Copy the files in CWD to NICPRINT_DIR
#
#   3) Prompt the user for information about this printer
#
#   4) Create the named pipes for this printer if needed for this system
#
#   4) Create the spool dir for this printer if needed
#
#   5) Add the printer to /etc/printcap if needed
#
#   6) Startup the daemon process if needed
#
#   7) Prompt the user if he wants to set up another printer. Go to 3 if yes.
#
# This script will work on any unix that understands #! and has the bourne
# shell (/bin/sh) installed. Ultrix systems have a skeletal /bin/sh. The
# set of commands below checks for /bin/sh5 and uses it to keep Ultrix and
# similar systems happy.

if [ $# -ne 1 ]
then
    if test -f /bin/sh5
    then
        exec /bin/sh5 $0 1
    fi
fi

#trap 'exit' 15

##############
# Declare some functions used by the main code seen at the end of
# this script.

yes_no () {

while :
  do
    ANSWER=""
    read ANSWER

    case "$ANSWER" in
    yes)	return 1 ;;
    y)		return 1 ;;
    no)		return 0 ;;
    n)		return 0 ;;
    *)
		$ECHONOCR "Please enter yes or no : ${nnl}"
		continue
		;;
    esac
  done

}

#set up command aliases and globals
set_commands_and_globals () {
	ECHO=echo

	SED=/bin/sed
	if [ ! -f "$SED" ] #this utility is needed by checkpath
	then
	   $ECHO "Required utility program is missing. /bin/sed not found."
	   exit 3
	fi

case "`echo 'x\c'`" in
'x\c')  ECHONOCR="echo -n"  nnl= ;;      # BSD
x)      ECHONOCR="echo"     nnl="\c" ;;  # Sys V
*)      ECHO "Can't set up echo, exiting $0." 1>&2; exit 1;;
esac

	RM=rm
        SU=su
	MKDIR=mkdir
	MKNOD=mknod
	CHOWN=chown
	CHMOD=chmod
	CP=cp
	CAT=cat
	MV=mv
	GREP=grep
	LS=ls
	DEVICE_TYPE=1
        PS=ps
        UNAME=uname

	for x in $SU $RM $MKDIR $MKNOD $CHOWN $CHMOD $CP $CAT $MV $SED $GREP $LS $PS
	do
	   if [ ! -f "$x" ]
	   then
		checkpath $x
		if [ "$?" = 0 ]
		then
		   exit 3
		fi
	   fi
	done

#global variables
	PRINTERS_DONE=""
        SYSTEM=""
        HARDWARE=""
        RELEASE=""
	INSTALL="0"
	CWD=`pwd`
	NOW=`date`
	NICPRINT_DIR=/usr/nic_2
	DEVDIR=/dev/nic_2
	BASE_PORT_NUM=10000

	SPOOLDIR=/usr/spool
	SPOOLQ_OWNER=lp

}


checkpath () {
#checks if commands are in the path. Returns a 1 if they are and 0 if not
#Arg1 the command to find
#
# This function requires that SED and ECHO be properly set
#
	commandtofind=$1
	pathdirs=`$ECHO $PATH |$SED -e 's/:/ /g' `
	for x in $pathdirs
	do
	   #$ECHO "Debug: Checking path $x"
	   if [ ! -d "$x" ]
	   then
		#$ECHO "Warning: Path item not found $x"
		continue
	   fi
	
	   if [ -f "${x}/${commandtofind}" ]
	   then
		#$ECHO "Debug: Command found in path. ${x}  ${commandtofind}"
		return 1
	   fi
	done
	$ECHO "Required utility not found. ${commandtofind}"
	return 0
}

	
# insure that we're running as root
verify_uid () {
	#$ECHO "Debug: verify_uid"

# would like to SU and continue the script, but too many problems
#        if test -z "`id | $GREP 'uid=0'`"
#        then
#           $ECHO "You must login as superuser to run this program."
#           $SU
#        fi

	if test -z "`id | $GREP 'uid=0'`"
	then
            $ECHO
            $ECHO "You must have superuser privileges to run this program."
            $ECHO
            exit 1
        fi

}

get_system_name () {

	while :

	do 

	$ECHO ""
	$ECHO "	Select the type of system that you are installing on : "
	$ECHO ""
	$ECHO "	1)	AT&T / SVR4 i386"
	$ECHO "	2)	SCO UNIX systemV"
	$ECHO "	3)	None of the above."
	$ECHO ""
	$ECHONOCR "	Enter choice ( 1 , 2 or 3 ) -->   ${nnl}"
		read SYS_ID

		if [ \( "$SYS_ID" = 1 \) ]
		 	then
		 	SYSTEM="i386"
			return 1
		fi

		if [ \( "$SYS_ID" = 2 \) ]
		 	then
		 	SYSTEM="scosysv"
			return 1
		fi
		
		if [ \( "$SYS_ID" = 3 \) ]
		 	then
		 	return 0
		fi
		continue
	done	
}


verify_target_sys () {

    	SYSTEM="`$UNAME -s`"
	HARDWARE="`$UNAME -m`"

  	if [ \( "$SYSTEM" = HP-UX \) -a \( "$SYSTEM" = AIX \) -a \( "$SYSTEM" = OSF1 \) ]
		then
		return 0
	fi 
		if [ \( "$SYSTEM" = ULTRIX \) ]
		 	then

		SYSTEM="`$UNAME -s`_`$UNAME -m`"
		fi

		if [ \( "$SYSTEM" = SunOS  \) ]
		 	then

		SYSTEM="`$UNAME -s`_`$UNAME -m`"

        	RELEASE="`$UNAME -r | cut -c1`"
        $ECHO "The release is $RELEASE"

        if [ \( "$RELEASE" = 5 \) ]
        then
            SYSTEM="SunOS_5.x"
        else
            if [ \( "$RELEASE" = 4 \) ]
            then
                SYSTEM="SunOS_4.x"
            fi
        fi
    fi

    $ECHO
    case "$SYSTEM" in
    ULTRIX_RISC)  $ECHO "The system name is $SYSTEM" ;;
    SunOS_4.x) $ECHO "The system name is $SYSTEM" ;;
    SunOS_5.x)   $ECHO "The system name is $SYSTEM" ;;
    OSF1) 	   $ECHO "The system name is $SYSTEM" ;;
    HP-UX)       $ECHO "The system name is $SYSTEM" ;;
    AIX)       $ECHO "The system name is $SYSTEM" ;;
    *)

    get_system_name 

    if [ $? = "1" ]
	then 
	return 0
    fi

cat << EOM |more

Operating system $SYSTEM not recognized. If your system is not supported you
may need to port the Digital TCP/IP host resident software. Source files for the
necessary utilities can be found with the installation software. Please refer 
to the TCP/IP porting guide in the N.I.C. Owners Manual for further 
information.

This installation script uses the format SYSTEM (eg ULTRIX_RISC) for its
system name, where the SYSTEM can be displayed by the uname command. 
Executable files for supported machines can be found in the 
installation directory tree in the SYSTEM subdirectory.

If you have completed the port and loaded the executables into the top level
of the installation directory, then you may continue the installation by 
answering the questions.  Otherwise terminate the script by hitting <CTRL>C

EOM

        $ECHONOCR "Does your system use the printcap file for printer setup? ${nnl}"

	yes_no
	if [ $? = "1" ]
	then
            $ECHO "Trying script as a generic BSD system ..."
            SYSTEM="BSD"
        else
            $ECHO
            $ECHO "Trying script as a generic SYSTEM V system ..."
            SYSTEM="SYSTEMV"
        fi
        $ECHO
        ;;
    esac
    $ECHO
}


# Verify that the executables needed to install and print are here!
# If a needed tool is not in this (the install) directory OR in the
# SYSTEM subdirectory abort the installation.
# 
check_utilities () {
     BUNDLE=""

     if [ \( "$SYSTEM" = ULTRIX_RISC \) ]
     then
         BUNDLE="nicfilter infilter nsconfig outfilter psbanner psfilter"
     fi

     if [ \( "$SYSTEM" = SunOS_5.x \) -o \( "$SYSTEM" = i386 \) ]
     then
         BUNDLE="nicfilter infilter nsconfig"
     fi

     if [ \( "$SYSTEM" = HP-UX \) -o \( "$SYSTEM" = AIX \) -o \( "$SYSTEM" = scosysv \) ]
     then
         BUNDLE="nic_print nsconfig"
     fi

     if [ \( "$SYSTEM" = SunOS_4.x \) -o \( "$SYSTEM" = OSF1 \) ]
     then
         BUNDLE="nic_print infilter nsconfig outfilter psbanner psfilter"
     fi



   exec 4<&1                    # Throw away output from these commands
   exec 5<&2
   exec > /dev/null 2>&1

     for x in $BUNDLE
     do
         $CP $CWD/$SYSTEM/$x $CWD/

         if test -z "`$LS $CWD/$x`"
         then
             exec 1<&4 
             exec 2<&5 

cat << EOM |more

ERROR : Installation aborted. Required N.I.C. utilities are missing.

The following executables are required before installation is possible :

	${BUNDLE}

If this is a supported system these executables should be supplied with 
the installation software in the directory $SYSTEM. If this is not a 
supported system the source code for the host resident software must 
be ported before installation can be completed. 

EOM

	     exit 3
	 fi

     done

     exec 1<&4 
     exec 2<&5 

}


set_file_mode () {

	$CHMOD 755 $CWD/*
	$CHMOD 666 $CWD/*.c
	$CHMOD 666 $CWD/*.h
	$CHMOD 666 $CWD/nicprint.conf

}


# copy the files from CWD into NICPRINT_DIR
# Now the NICPRINT_DIR directory is empty and we can move the files out 
# of the current directory into NICPRINT_DIR.
# 
copy_parts_to_nicprint () {
   $ECHO
   $ECHO "Copying $CWD to ${NICPRINT_DIR}"
   exec 4<&1                    # Throw away output from these commands
   exec 5<&2
   exec > /dev/null 2>&1

	for x in $CWD/*
	do
	   #$ECHO "	Copying $x to ${NICPRINT_DIR}"
	   $CP $x ${NICPRINT_DIR}/
	done

   exec 1<&4 
   exec 2<&5 
}


#create a character device with the same major/minor #'s as /dev/null
#for AIX output to go to (it is simultaneously sent to the named pipe
#via the backend program

create_null_dev () {
	#$ECHO Check existence
	if [ ! -p "/dev/${1}" ]
	then
	   #$ECHO making a character device with same major/minor as /dev/null
	   $MKNOD /dev/${1} c 2 2
	fi

	#$ECHO create null device
}


#create a named pipe
create_npipe () {
	if [ ! -d "${DEVDIR}" ]
	then
	   #$ECHO making the dev directory
	   $MKDIR ${DEVDIR}
	fi
	#$ECHO Check existence
	if [ ! -p "${DEVDIR}/${1}" ]
        then
	   #$ECHO making the named pipe
	   $MKNOD ${DEVDIR}/${1} p

           if [ \( "$SYSTEM" = BSD \) -o \( "$SYSTEM" = ULTRIX_RISC \) -o \( "$SYSTEM" = SunOS_4.x \)  -o \( "$SYSTEM" = OSF1 \) ]
           then
# CHMOD is not essential and may fail on some systems so hide output from user
   exec 4<&1                              # Throw away output from commands
   exec 5<&2
   exec > /dev/null 2>&1
	       $CHOWN daemon ${DEVDIR}/${1}
   exec 1<&4 
   exec 2<&5 
	       $CHMOD 666 ${DEVDIR}/${1}
           fi

           if [ \( "$SYSTEM" = AIX \) ]
           then
	       $CHMOD 666 ${DEVDIR}/${1}
           fi

           if [ \( "$SYSTEM" = HP-UX \) -o \( "$SYSTEM" = scosysv \) -o \( "$SYSTEM" = SYSTEMV \) ]
           then
# CHMOD is not essential and may fail on some systems so hide output from user
   exec 4<&1                              # Throw away output from commands
   exec 5<&2
   exec > /dev/null 2>&1
	       $CHOWN lp ${DEVDIR}/${1}
   exec 1<&4 
   exec 2<&5 
	       $CHMOD 600 ${DEVDIR}/${1}
           fi

	fi
	#$ECHO create npipe returning
}


#Create a spool dir
create_spool_dir () {
	if [ ! -d ${SPOOLDIR}/${1} ]
        then
	   $MKDIR ${SPOOLDIR}/${1}
	   $CHOWN daemon ${SPOOLDIR}/${1}
	fi
}

#
# Place the nicprint_conf file in the spool directory with
# <printer name> <printer port> as the contents of the file.
#

init_nconfig () {
cat << EOM > ${SPOOLDIR}/${PNAME}/nicprint_conf
EOM
}

display_answers () {

$ECHO ""
$ECHO ""
$ECHO "You have entered the following information:"
$ECHO ""

if [ -n "$NP_NAME" ]
then
	$ECHONOCR "  Nodename of the N.I.C. :			${nnl}"
	$ECHO $NP_NAME
fi

if [ -n "$PNAME" ]
then
	$ECHONOCR "  Printer name to be used by this printer :	${nnl}"
	$ECHO "$PNAME"
fi

if [ -n "$PHYS_PORT" ]
then
	$ECHONOCR "  The printer is attached on :			${nnl}"
fi

if [ \( -n "$NP_NAME" \) -a \( -n "$PHYS_PORT" \) ]
then
  case $DEVICE_TYPE
  in
	1)	# N.I.C. 100
		if [ -n "$port1" ]
		then
			$ECHO "parallel port"
		fi

		if [ -n "$port2" ]
		then
			$ECHO "serial port"
		fi
	;;
	#*)	# N.I.C. 200
	#	if [ -n "$port1" ]
	#	then
	#		$ECHO "parallel port 1"
	#	fi
#
	#	if [ -n "$port2" ]
	#	then
	#		$ECHO "parallel port 2"
	#	fi
#
	# 	if [ -n "$port3" ]
	#	then
	#		$ECHO "parallel port 3"
	#	fi
#
	#	if [ -n "$port4" ]
	#	#then
	#		$ECHO "uniport"
	#	fi
	#;;
  esac
fi
}

# Prompt for devicetype
get_devicetype () {

$ECHO ""
$ECHO "Select the type of N.I.C. you are installing:"
$ECHO ""
$ECHO "	1)   N.I.C. 100"
#$ECHO "	2)   N.I.C. 200"
$ECHO ""
#$ECHONOCR "	(1, or 2) ?  ${nnl}"
$ECHONOCR "	(1) ?  ${nnl}"

while :
  do
    read DEVICE_TYPE

	case "$DEVICE_TYPE"
	in
		1)
			break
			;;
		2)
			break
			;;
		*)
			$ECHONOCR "Enter value 1 .  ? ${nnl}"
			#$ECHONOCR "Enter a value 1 or 2.  ? ${nnl}"
			continue
			;;
	esac
  done
}

# promptfor_printerport
get_physicalport () {

$ECHO ""
$ECHO "The following ports are equipped on N.I.C. $NP_NAME:"
$ECHO ""

port1=""
port2=""
port3=""
port4=""

    case "$DEVICE_TYPE"
    in
	1)
		# Prompt for port number of N.I.C.100

    		$ECHO "	Port 1 is parallel port"
    		$ECHO "	Port 2 is serial port"
		$ECHO ""

		$ECHONOCR "Enter the port number to which the printer is attached.  ? ${nnl}"

		while :
  		do

    			read PHYS_PORT

			if [ \( "$PHYS_PORT" = 1 \) -o \( "$PHYS_PORT" = 2 \) ]
			then
				break
			else
				$ECHONOCR "Select 1 or 2.  ? ${nnl}"
				continue
			fi
		done
	  ;;
	#2)
		# Prompt for port number of N.I.C. 200

    	#	$ECHO "	Port 1 is parallel port 1"
    	#	$ECHO "	Port 2 is parallel port 2"
    	#	$ECHO "	Port 3 is parallel port 3"
    	#	$ECHO "	Port 4 is uniport"
	#	$ECHO ""
#
	#	$ECHONOCR "Enter the port number to which the printer is attached.  ? ${nnl}"
	#	while :
  	#	do
    	#		read PHYS_PORT

	#		if [ \( "$PHYS_PORT" = 1 \) -o \( "$PHYS_PORT" = 2 \) -o \( "$PHYS_PORT" = 3 \) -o \( "$PHYS_PORT" = 4 \) ]
	#		then
	#			break
	#		else
	#			$ECHONOCR "Select a value between 1 and 4.  ? ${nnl}"
	#			continue
	#		fi
	#	done
	#  ;;
    esac

# Now map the PHYSICAL port into a port name
#
    case "$DEVICE_TYPE"
    in
	1)
		case "$PHYS_PORT"
		in
			1)
				port1=$PNAME
			;;
			2)
				port2=$PNAME
			;;
			*)
				$ECHO "Illegal selection"
			;;
		esac
		;;
	#*)
	#	case "$PHYS_PORT"
	#	in
	#		1)
	#			port1=$PNAME
	#		;;
	#		2)
	#			port2=$PNAME
	#		;;
	#		3)
	#			port3=$PNAME
	#		;;
	#		4)
	#			port4=$PNAME
	#		;;
	#		*)
		#		$ECHO "Illegal selection"
		#	;;
		#e#sac
	#;;
    esac

#if [ -n "$port1" ]
#then
#	$ECHO "Printer $port1 is to be attached to port 1"
#fi

#if [ -n "$port2" ]
#then
#	$ECHO "Printer $port2 is to be attached to port 2"
#fi

#if [ -n "$port3" ]
#then
#	$ECHO "Printer $port3 is to be attached to port 3"
#fi

#if [ -n "$port4" ]
#then
#	$ECHO "Printer $port4 is to be attached to port 4"
#fi
	
}


get_nodename () {
	$ECHO ""
	$ECHONOCR "Enter the nodename of this N.I.C..  ? ${nnl}"
	read NP_NAME
}

get_printername () {
	$ECHO ""
	$ECHONOCR "Enter the printer name to be used by this printer? ${nnl}"
	read PNAME
}

get_queuename () {
	$ECHO ""
	QNAME=$PNAME
#	$ECHO "The queue for this printer will be named as $QNAME after this installation"
}

# promptfor_printerport
get_printerport () {

    $ECHO ""
    $ECHO "The following ports are equipped on N.I.C. $NP_NAME:"
    $ECHO ""

    case "$DEVICE_TYPE"
    in
	1)
	# Prompt for port number of N.I.C.100

    	$ECHO "	Port 1 is parallel port"
    	$ECHO "	Port 2 is serial port"
	$ECHO ""

	$ECHONOCR "Enter the port number to which the printer is attached.  ? ${nnl}"

	while :
  	do
    		read PHYS_PORT

		if [ \( "$PHYS_PORT" = 1 \) -o \( "$PHYS_PORT" = 2 \) ]
		then
			break
		else
			$ECHONOCR "Select 1 or 2.  ? ${nnl}"
			continue
		fi
	done
	;;
	#2)
	# Prompt for port number of N.I.C. 200

    	#$ECHO "	Port 1 is parallel port 1"
    	#$ECHO "	Port 2 is parallel port 2"
    	#$ECHO "	Port 3 is parallel port 3"
    	#$ECHO "	Port 4 is uniport"
	#$ECHO ""

	#$ECHONOCR "Enter the port number to which the printer is attached.  ? ${nnl}"

	#while :
  	#do
    	#	read PHYS_PORT
#
	#	if [ \( "$PHYS_PORT" = 1 \) -o \( "$PHYS_PORT" = 2 \) -o \( "$PHYS_PORT" = 3 \) -o \( "$PHYS_PORT" = 4 \) ]
	#	then
	#		break
	#	else
	#		$ECHONOCR "Select a value between 1 and 4.  ? ${nnl}"
	#		continue
	#	fi
	#done
	# ;;
    esac
}

setfilters() {

        $ECHO
	$ECHONOCR "Is this a PostScript printer (yes,y,no,n) ? ${nnl}"
	yes_no
	if [ $? = "1" ]
	then
           FORMAT=POSTSCRIPT
	else
           FORMAT=NORMAL
	fi


}

generate_ultrix_printcap () {
	if [ -f "/tmp/Digitalpcap$$" ]
	then
	   rm -f /tmp/Digitalpcap$$
	fi

	$ECHO
	$ECHO "The sample printcap entry shown below has been created for"
	$ECHO "your printer in the file /tmp/Digitalpcap$$."
	$ECHO
	$ECHO "#"
	$ECHO "# This printcap entry is automatically generated for the N.I.C. product"
	$ECHO "# The input filter supplied, called infilter, can be replaced"
	$ECHO "# by your own special purpose filter."
	$ECHO "#" > /tmp/Digitalpcap$$
	$ECHO "# This printcap entry is automatically generated for the N.I.C. product" >> /tmp/Digitalpcap$$
	$ECHO "# The input filter supplied, called infilter, can be replaced" >> /tmp/Digitalpcap$$
	$ECHO "# by your own special purpose filter." >> /tmp/Digitalpcap$$
	
	$ECHO "${PNAME}|N.I.C. printer :\\"
	$ECHO "	:lp=/dev/null:\\"
        if [ \( "$FORMAT" =  POSTSCRIPT \) ]
	then
	    $ECHO "	:if=${NICPRINT_DIR}/psfilter|${NICPRINT_DIR}/nicfilter ${NP_NAME} ${PORT_NUM}:\\"
	    $ECHO "	:of=${NICPRINT_DIR}/psbanner:\\"
        else
	    $ECHO "	:if=${NICPRINT_DIR}/infilter|${NICPRINT_DIR}/nicfilter ${NP_NAME} ${PORT_NUM}:\\"
	    $ECHO "	:of=${NICPRINT_DIR}/outfilter:\\"
        fi
	$ECHO "	:sd=${SPOOLDIR}/${QNAME}:"
	$ECHO "${PNAME}|N.I.C. printer :\\" >> /tmp/Digitalpcap$$
	$ECHO "	:lp=/dev/null:\\" >> /tmp/Digitalpcap$$
        if [ \( "$FORMAT" =  POSTSCRIPT \) ]
	then
	    $ECHO "	:if=${NICPRINT_DIR}/psfilter|${NICPRINT_DIR}/nicfilter ${NP_NAME} ${PORT_NUM}:\\" >> /tmp/Digitalpcap$$
	    $ECHO "	:of=${NICPRINT_DIR}/psbanner:\\" >> /tmp/Digitalpcap$$
        else
	    $ECHO "	:if=${NICPRINT_DIR}/infilter|${NICPRINT_DIR}/nicfilter ${NP_NAME} ${PORT_NUM}:\\" >> /tmp/Digitalpcap$$
	    $ECHO "	:of=${NICPRINT_DIR}/outfilter:\\" >> /tmp/Digitalpcap$$
        fi

	$ECHO "	:sd=${SPOOLDIR}/${QNAME}:" >> /tmp/Digitalpcap$$
	$ECHO "#" >> /tmp/Digitalpcap$$
	$ECHO "#"
}

generate_simp_printcap () {
	if [ -f "/tmp/Digitalpcap$$" ]
	then
	   rm -f /tmp/Digitalpcap$$
	fi

	$ECHO
	$ECHO "The sample printcap entry shown below has been created for"
	$ECHO "your printer in the file /tmp/Digitalpcap$$."
	$ECHO
	$ECHO "#"
	$ECHO "# This printcap entry is automatically generated for the N.I.C. product"
	$ECHO "# Simple input and output filters are provided for your convenience."
	$ECHO "#" > /tmp/Digitalpcap$$
	$ECHO "# This printcap entry is automatically generated for the N.I.C. product" >> /tmp/Digitalpcap$$
	$ECHO "#" > /tmp/Digitalpcap$$
	$ECHO "${PNAME}|N.I.C. printer :\\"
	$ECHO "	:lp=${DEVDIR}/${PNAME}:\\"
        if [ \( "$FORMAT" =  POSTSCRIPT \) ]
	then
	    $ECHO " 	:if=${NICPRINT_DIR}/psfilter:\\"
            $ECHO "	:of=${NICPRINT_DIR}/psbanner:\\"
        else
	    $ECHO " 	:if=${NICPRINT_DIR}/infilter:\\"
            $ECHO "#	:of=${NICPRINT_DIR}/outfilter:\\"
        fi

	$ECHO "	:sd=${SPOOLDIR}/${QNAME}:"
	$ECHO "${PNAME}|N.I.C. printer :\\" >> /tmp/Digitalpcap$$
	$ECHO "	:lp=${DEVDIR}/${PNAME}:\\" >> /tmp/Digitalpcap$$
        if [ \( "$FORMAT" =  POSTSCRIPT \) ]
	then
	    $ECHO " 	:if=${NICPRINT_DIR}/psfilter:\\" >> /tmp/Digitalpcap$$
	    $ECHO "	:of=${NICPRINT_DIR}/psbanner:\\" >> /tmp/Digitalpcap$$
        else
	    $ECHO " 	:if=${NICPRINT_DIR}/infilter:\\" >> /tmp/Digitalpcap$$
	    $ECHO "#	:of=${NICPRINT_DIR}/outfilter:\\" >> /tmp/Digitalpcap$$
        fi
	$ECHO "	:sd=${SPOOLDIR}/${QNAME}:" >> /tmp/Digitalpcap$$
	$ECHO "#" >> /tmp/Digitalpcap$$
	$ECHO "#"
}

try_append_simp_printcap () {
        $ECHO
        $ECHO "This entry can be appended automatically to your /etc/printcap file."
        $ECHO
	$ECHONOCR "Would you like to perform this action? (yes,y,no,n) ? ${nnl}"
	yes_no
	if [ $? = "1" ]
	then
	   $CP /etc/printcap /etc/oldprintcap
	   $CAT /etc/oldprintcap /tmp/Digitalpcap$$ > /etc/printcap
	   $ECHO
	   $ECHO /etc/printcap has been updated.
	   $ECHO
	else
	   $ECHO
	   $ECHO "/etc/printcap is not modified."
	   $ECHO
	fi
}


start_daemon() {

   exec 4<&1                              # Throw away output from commands
   exec 5<&2
   exec > /dev/null 2>&1
    if test -z "`$PS -ef |$GREP \"nic_print ${DEVDIR}/$PNAME $NP_NAME $PORT_NUM\" |$GREP -v grep`"
    then
        if test -z "`$PS -ax |$GREP \"nic_print ${DEVDIR}/$PNAME $NP_NAME $PORT_NUM\" |$GREP -v grep`"
        then
   exec 1<&4 
   exec 2<&5 
           $ECHO "Starting printer daemon :"
           $ECHO
           $ECHO "        ${NICPRINT_DIR}/nic_print ${DEVDIR}/$PNAME $NP_NAME $PORT_NUM&"
           ${NICPRINT_DIR}/nic_print ${DEVDIR}/$PNAME $NP_NAME $PORT_NUM&
        else
   exec 1<&4 
   exec 2<&5 
           $ECHO "WARNING : Printer $PNAME already has a daemon serving it"
           $ECHO
           $ECHO "You may have already configured printer $PNAME. If you have"
           $ECHO "installed a new TCP/IP release or changed the printer"
	   $ECHO "configuration you may need to kill the existing daemon and"
	   $ECHO "restart it with the command :"
           $ECHO
           $ECHO "	${NICPRINT_DIR}/nic_print ${DEVDIR}/$PNAME $NP_NAME $PORT_NUM&"
           $ECHO
           $ECHO "The current version of the installation script DOES NOT check"
           $ECHO "for duplicate entries in the /etc/printcap file."
        fi
    else
   exec 1<&4 
   exec 2<&5 
        $ECHO "WARNING : Printer $PNAME already has a daemon serving it"
        $ECHO
    fi
}

tell_user_ultrix_instructs () {

cat << EOM |more

Printer $PNAME now has a logical connection to port $PHYS_PORT of the 
NetPrint host $NP_NAME. You should test to make sure that the N.I.C. 
box will properly communicate with this host before attempting to print.
Refer to the TCP/IP Installation chapter of the N.I.C. Owners Manual
for further information. If there are any problems refer to the trouble
shooting guide.

EOM
}

tell_user_sysV_instructs () {

cat << EOM |more

TCP/IP installation completed successfully.


Before you can use your N.I.C., the printer interface program that is to be 
used by this printer must be edited to redirect output to the nicfilter 
program. Printers may then be configured using the lpadmin maintenance
command. Refer to the System V Solaris section in the TCP/IP Installation 
chapter of the N.I.C. Owners Manual for further information.

EOM
}

tell_user_final_instructs () {

cat << EOM |more

Printer $PNAME now has a logical connection through ${DEVDIR}/$PNAME to 
port $PHYS_PORT of the N.I.C. host $NP_NAME. You should test to make sure
that the N.I.C. will properly communicate with this host before attempting
to print. Refer to the TCP/IP Installation chapter of the N.I.C. Owners
Manual for further information. If there are any problems refer to the
trouble shooting guide.

EOM
}


final_message () {

    $ECHO
    if test -n "$PRINTERS_DONE"
    then
    	$ECHO "Printer(s) $PRINTERS_DONE configured successfully."
        $ECHO
    fi

    $ECHO "Exiting installation script."
    $ECHO
}


howto_autostart () {
cat << EOM |more

You may want to have the system automatically start the N.I.C. daemon for
your new printer. Edit the file rc.local and add the following lines directly
after the lpd entry:
 
 if [-f /usr/nic/nic_print]; then
 	/usr/nic/nic_print  /dev/nic/printername   hostname  port)#
 	echo -n ' nic_print'

this is for 1 printer.  for additional printers you would need multiple entries.

If you cannot connect to the N.I.C. check the files:
 	/etc/hosts
 	/etc/ethers
 	/etc/rc.local for the rarpd startup.

 If you make changes to hostnames or addresses and you have yellowpages 
 runnings you must reconfigure the yellow pages to reflect these changes. 

EOM
}

###########################################################
#
# MAIN 
#
	#initialize the world
	set_commands_and_globals 	#Setup command aliases and globals
	verify_target_sys		#Find out what system we are running on
	verify_uid 			#Make sure running as root


	#Just check that we are OK to run before asking a bunch of questions
	#dont do any real work until user is happy with setup.

	if [ -d "$NICPRINT_DIR" ]
	then
	   if [ "$CWD" != "$NICPRINT_DIR" ]
	   then
	      $ECHO ""
	      $ECHO "You are not in the NICPRINT directory ${NICPRINT_DIR}. To configure"
	      $ECHO "additional printers cd ${NICPRINT_DIR} and restart this script."
	      $ECHO ""
	      $ECHO "If you would like to install new TCP/IP software you must"
	      $ECHO "remove ${NICPRINT_DIR} and restart this script."
	      $ECHO ""
	      exit 3
	   fi
	fi

	check_utilities                 #Verify that all the nic executables 
	                                #are in place
	set_file_mode                   #Verify all files in install directory 
	                                #have right permissions

	# If System V Solaris setup and configuration must be done by hand.
	# So just get executables to the right place
	if [ \( "$SYSTEM" = SunOS_5.x \) -o \( "$SYSTEM" = i386 \) ]
	then
		if [ ! -d "$NICPRINT_DIR" ]
		then
			#Create the N.I.C. dir and copy the files
	   		$ECHO "Installing the basic N.I.C. product from $CWD"
			$MKDIR $NICPRINT_DIR
			copy_parts_to_nicprint
		fi	

		tell_user_sysV_instructs
		exit 0
	fi

    while :              # loop until user has no more devices to configure
      do

	# in the real version we will read in the current nicprint_config
	# file and then go into a command loop. 

	# get names from the user (i.e. printer name, ip etc)
	
        get_nodename
        get_printername
        get_queuename
#        get_devicetype
        get_physicalport
	
        display_answers
        $ECHO
        $ECHONOCR "Do you want to accept this configuration (yes,y,no,n) ? ${nnl}"

	yes_no
	if [ $? = "0" ]
        then
            $ECHO ""
            $ECHONOCR "Do you want to configure additional printers (yes,y,no,n) ? ${nnl}"

	    yes_no
	    if [ $? = "1" ]
            then
                continue
            else
		final_message
                exit 1
            fi
        fi

        $ECHO
	#if you are not in /usr/nic and the software has not already been 
	#copied then the script will try to
	#create /usr/nic and copy the files in the local dir to /usr/nic

    if [ $INSTALL = "0" ] 
    then
	if [ -d "$NICPRINT_DIR" ]
	then
	   if [ "$CWD" != "$NICPRINT_DIR" ]
	   then
	      $ECHO ""
	      $ECHO "You are not in the NICPRINT directory ${NICPRINT_DIR}."
	      $ECHO "To re-install you must remove ${NICPRINT_DIR}"
	      exit 3
	   fi
	else
	   $ECHO "Installing the basic N.I.C. product from $CWD"
	   $MKDIR $NICPRINT_DIR         #Create the N.I.C. dir
	   copy_parts_to_nicprint	#Copy the files in CWD to NICPRINT_DIR
	fi	
    fi

    INSTALL="1"             # All software is installed in /usr/nic

	# If this is ULTRIX or sys5 solaris, no named pipe needed ...
        if [ \( "$SYSTEM" != ULTRIX_RISC \) -a \( "$SYSTEM" != SunOS_5.x \) -a \( "$SYSTEM" != i386 \) ]
        then
	    create_npipe $PNAME

            if [ \( "$SYSTEM" = AIX \) ]
            then
	        create_null_dev $PNAME
            fi
	fi

        PORT_NUM=`expr $BASE_PORT_NUM + $PHYS_PORT`

        # Unless we are a BSD style unix we do not need a spool dir or printcap
        if [ \( "$SYSTEM" = BSD \) -o \( "$SYSTEM" = ULTRIX_RISC \) -o \( "$SYSTEM" = SunOS_4.x \) -o \( "$SYSTEM" = OSF1 \) ]
        then
            create_spool_dir $QNAME

            setfilters
            if [ \( "$SYSTEM" = ULTRIX_RISC \) ]
            then
	        generate_ultrix_printcap $PNAME $QNAME 
            else
	        generate_simp_printcap $PNAME $QNAME 
            fi
	    try_append_simp_printcap
        fi

	# Unless this is ULTRIX or SunOS_5.x, install the nic daemon for 
	# this printer
        if [ \( "$SYSTEM" != ULTRIX_RISC \) -a \( "$SYSTEM" != SunOS_5.x \) -a \( "$SYSTEM" != i386 \) ]
        then
	    start_daemon
	fi

	# If this is ULTRIX, we output slightly different instructions ...
        if [ \( "$SYSTEM" = ULTRIX_RISC \) ]
        then
	    tell_user_ultrix_instructs
        else
            tell_user_final_instructs
        fi

        if test -z "$PRINTERS_DONE"
        then
	    PRINTERS_DONE="$PNAME"
	else
	    PRINTERS_DONE="$PRINTERS_DONE, $PNAME"
	fi

        $ECHO ""
        $ECHONOCR "Do you want to configure additional printers (yes,y,no,n) ? ${nnl}"

	yes_no
	if [ $? = "0" ]
        then
            break
        fi

    done

    final_message

    exit 0

