:
# set -x

#  Define the release
RELEASE="2.1.0"
RELEASE_DIR="Xncd.$RELEASE"

#  Define the target directories.
TFTP_RESTRICTED=false
TFTP_DIR="/tftpboot"
MOP_DIR="/usr/lib/mop"		# or /usr/lib/dnet/*.sys
FONT_DIR="/usr/lib/X11"

#
#  Define exit codes.
#
EXIT_NOT_SUPER_USER=1
EXIT_QUIT=2
EXIT_FAILED=3
EXIT_INTRUP=4

#
#  We need to be able to report what is not yet installed.
#
NOT_YET="Neither the server image(s) nor the fonts have been installed."

#
#  Enable HUP, DEL, INT, and TERM
#
trap "exit $EXIT_INTRUP" 1 2 3 15
trap "echo; echo \$NOT_YET; rm -f tf.$$ /tmp/ps.$$" 0

#
#  Define variables used to display text and to ask questions.
#
if test -r /usr/ucb/fmt
then
    SAY="/usr/ucb/fmt"
else
    SAY="cat"
fi
ASK="echo -n"
X=`$ASK XxX`
if test "$X" != "XxX"
then
    ASK="echo; echo"
else
    ASK="echo; $ASK"
fi

#
#  Figure out how to see what's running.
#
PS_OPTS="-ax"
(ps $PS_OPTS | grep '^  *1 .*init') > /dev/null 2>&1
if test $? -eq 1
then
    PS_OPTS="-e"
    (ps $PS_OPTS | grep '^  *1 .*init') > /dev/null 2>&1
    if test $? -eq 1
    then
	PS_OPTS=""
    fi
fi

#
#  Describe conventions.
#
( echo
  echo "When questions are asked they will contain a string in square brackets"
  echo "right before the question mark.  This is the default response -- that"
  echo "is the response that will be assumed if you simply depress return."
  echo
  echo "Any time a question is asked you may respond with q, quit, Q, or QUIT"
  echo "to terminate this installation script.  If you are being asked for a"
  echo "file or directory name and want to use one of these as a response,"
  echo "simply preceed it with ./ (e.g. ./q)."
) | $SAY


#
#  Do our best to make sure we're running as root.
#
if test -r /bin/id
then
    ID=`/bin/id`
else
    if test -r /usr/bin/id
    then
	ID=`/usr/bin/id`
    fi
fi
if test "$ID"
then
    ID=`expr "$ID" : "uid=\([0-9]*\)("`
    if test "$ID" = ""
    then
	ID=-1
    fi
else
    ID=-1
fi

if test $ID -ne 0
then
    ( echo
      echo "You need to be logged in as root (uid=0) to perform this"
      echo "installation.  It cannot be determined that you are logged in"
      echo "as root."
    ) | $SAY
    eval $ASK "Are you logged in as root [n]?"
    read ANSWER
    case "$ANSWER" in
	y| yes| Y| YES)
	    ;;
	q| quit| Q| QUIT)
	    exit $EXIT_QUIT
	    ;;
	*)
	    exit $EXIT_NOT_SUPER_USER
	    ;;
    esac
fi

#
#  See what networking utilitites are on this system.
#

( echo
  echo "The NCD server must be placed on a system that supports a minimum"
  echo "set of network utilities.  We'll now see if we can determine which"
  echo "network utilitites are available.  Please be patient..."
) | $SAY

#
#  Snapshot the state of the system.  This is necessary in order to
#  save hugh amounts of time on systems where ps is expensive.
#
ps $PS_OPTS > /tmp/ps.$$

#
#  First see if we have inetd
#
if grep inetd /tmp/ps.$$ > /dev/null
then
    INETD_PRESENT=true
else
    INETD_PRESENT=false
fi

#
#  See if we have BOOTP
#
if grep '^bootp' /etc/inetd.conf > /dev/null 2>&1
then
    BOOTP_PRESENT=$INETD_PRESENT
else
    if grep bootp /tmp/ps.$$ > /dev/null
    then
	BOOTP_PRESENT=true
    else
	BOOTP_PRESENT=false
    fi
fi
#
#  See if we have RARP
#
if grep '^rarp' /etc/inetd.conf > /dev/null 2>&1
then
    RARP_PRESENT=$INETD_PRESENT
else
    if grep rarpd /tmp/ps.$$ > /dev/null
    then
	RARP_PRESENT=true
    else
	RARP_PRESENT=false
    fi
fi
#
#  See if we have TFTP
#
if grep '^tftp' /etc/inetd.conf > /dev/null 2>&1
then
    TFTP_PRESENT=$INETD_PRESENT
else
    if grep tftpd /tmp/ps.$$ > /dev/null
    then
	TFTP_PRESENT=true
    else
	TFTP_PRESENT=false
    fi
fi
#
#  See if we have MOP
#
if grep mop_mom /tmp/ps.$$ > /dev/null
then
    MOP_PRESENT=true
else
    MOP_PRESENT=false
fi
#
#  See if we could boot
#
if test \( \( $BOOTP_PRESENT = true -o $RARP_PRESENT = true \) -a \
$TFTP_PRESENT = true \) -o $MOP_PRESENT = true
then
    CAN_BOOT=true
else
    CAN_BOOT=false
fi

#
#  Make sure there's a server image here before we get too excited about
#  installing it.
#
if test -d "$RELEASE_DIR"
then
    if test $CAN_BOOT = false
    then
	#
	#  If we cannot operate as a boot server, better ask.
	#
	( echo
	  echo "This system does not appear to be capable of operating as"
	  echo "a boot server.  This is based on a quick look at the files"
	  echo "in /etc and the processes that are running.  It could be that"
	  echo "the appropriate network utilities are not enabled."
	) | $SAY
	eval $ASK "Do you want to install the server image [y]?"
	read ANSWER
	case "$ANSWER" in
	    n| no| N| NO)
		INSTALL_SERVER=false
		;;
	    q| quit| Q| QUIT)
		exit $EXIT_QUIT
		;;
	    *)
		INSTALL_SERVER=true
		;;
	esac
    fi
    if $INSTALL_SERVER
    then
	#
	#  If mop exists, ask if it's to be used.
	#
	if $MOP_PRESENT
	then
	    ( echo
	      echo "NCD units can either be booted using a combination of TFTP"
	      echo "and either BOOTP or RARP or they can be booted via MOP"
	      echo "(which is a DECnet utility)."
	    ) | $SAY
	    eval $ASK "Do you want to use MOP [n]?"
	    read ANSWER
	    case "$ANSWER" in
		y| yes| Y| YES)
		    USE_MOP=true
		    ;;
		q| quit| Q| QUIT)
		    exit $EXIT_QUIT
		    ;;
		*)
		    USE_MOP=false
		    ;;
	    esac
	else
	    USE_MOP=false
	fi
	#
	#  See if we can detect where tftp wants its files.
	#
	if $USE_MOP
	then
	    SERVER_DIR="$MOP_DIR"
	else
	    if test -r /etc/inetd.conf
	    then
		SERVER_DIR=`grep '^tftp' /etc/inetd.conf`
		SERVER_DIR=`expr "$SERVER_DIR" : '.*-[rs][ 	]*\([^ 	][^ 	]*\)'`
		if test "$SERVER_DIR"
		then
		    if test "$SERVER_DIR" = "/"
		    then
			SERVER_DIR="$TFTP_DIR"
		    else
			TFTP_RESTRICTED=true
			TFTP_DIR="$SERVER_DIR"
		    fi
		else
		    SERVER_DIR="$TFTP_DIR"
		fi
	    else
		SERVER_DIR="$TFTP_DIR"
	    fi
	fi
	#
	#  Now see if the user wants to change our idea of where the download
	#  image should go.
	#
	if $USE_MOP
	then
	    ( echo
	      echo "The server image must be installed where it can be found"
	      echo "by MOP.  This is usually /usr/lib/mop."
	    ) | $SAY
	else
	    ( echo
	      echo "The server image must be installed where it can be found"
	      echo "by the bootp or tftp daemon.  On BSD derived systems, the"
	      echo "correct place for tftpd is the directory \"$SERVER_DIR\"."
	      echo "On BSD derived system running secure or restricted TFTP"
	      echo "the correct place is in the directory specified in the"
	      echo "TFTP daemon invocation.  With bootpd the file location"
	      echo "and name are specified by the administrator."
	    ) | $SAY
	fi
	eval $ASK "Enter the name of the server directory [$SERVER_DIR]?"
	read ANSWER
	if test "$ANSWER"
	then
	    case "$ANSWER" in
		q| quit| Q| QUIT)
		    exit $EXIT_QUIT
		    ;;
		*)
		    SERVER_DIR="$ANSWER"
		    ;;
	    esac
	fi
	while test ! -d "$SERVER_DIR"
	do
	    ( echo
	      echo "The directory you entered, \"$SERVER_DIR\", doesn't exist."
	      echo "Please enter the pathname of the directory in which to"
	      echo "install the server image, or q to quit."
	    ) | $SAY
	    eval $ASK "Enter the name of the server directory [$SERVER_DIR]?"
	    read ANSWER
	    if test "$ANSWER"
	    then
		case "$ANSWER" in
		    q| quit| Q| QUIT)
			exit $EXIT_QUIT
			;;
		    *)
			SERVER_DIR="$ANSWER"
			;;
		esac
	    fi
	done
	#
	#  Make sure we're not already in $SERVER_DIR
	#
	touch tf.$$
	if test -f "$SERVER_DIR"/tf.$$
	then
	    rm -f tf.$$
	    ( echo
	      echo "You appear to be in \"$SERVER_DIR\"."
	    ) | $SAY
	    STATUS=0
	else
	    rm -f tf.$$
	    ( echo
	      echo "The server image will be installed in \"$SERVER_DIR\"."
	    ) | $SAY
	    mkdir "$SERVER_DIR"/"$RELEASE_DIR"
	    cp $RELEASE_DIR/Xncd* "$SERVER_DIR"/"$RELEASE_DIR"
	    STATUS=$?
	fi
	if test $STATUS -eq 0
	then
	    for FILE in $RELEASE_DIR/Xncd*
	    do
		FILE=`basename "$FILE"`
		MODEL=`expr "$FILE" : "Xncd\([0-9][0-9][ce]*\)"`
		case "$MODEL" in
		    16)
			if test ! "$WANT"
			then
			    ( echo
			      echo "With the NCD16 server you can make either"
			      echo "the full function server, the small server,"
			      echo "or the local telnet client server the"
			      echo "default.  If you have a majority of"
			      echo "1MB units you should use the small memory"
			      echo "server as the default.  If you must have"
			      echo "NFS or if most of your NCD16 units are"
			      echo "1.5MB or larger, use the full function"
			      echo "server.  If you need local telnet"
			      echo "capability, use the local telnet server."
			    ) | $SAY

			    eval $ASK "Do you want to use the full function server as the default [y]?"
			    read ANSWER
			    case "$ANSWER" in
				n| no| N| NO)
			            eval $ASK "Do you want to use the small server as the default [y]?"
			            read ANSWER
			            case "$ANSWER" in
				        n| no| N| NO)
				            WANT="L"
				            ;;
				        q| quit| Q| QUIT)
				            exit $EXIT_QUIT
				            ;;
				        *)
				            WANT="S"
				            ;;
			            esac
				    ;;
				q| quit| Q| QUIT)
				    exit $EXIT_QUIT
				    ;;
				*)
				    WANT="F"
				    ;;
			    esac
			fi
			case "$FILE" in
			    Xncd16)
			        GOT="F"
			        ;;
			    Xncd16_s)
			        GOT="S"
			        ;;
			    Xncd16_lt)
			        GOT="L"
			        ;;
			esac
			if test $WANT = $GOT
			then
			    if test -f "$SERVER_DIR"/Xncd16
			    then
				rm -f "$SERVER_DIR"/Xncd16.old
				( echo
				  echo "Moving \"$SERVER_DIR/Xncd16\" to"
				  echo "\"$SERVER_DIR/Xncd16.old\"."
				) | $SAY
				mv "$SERVER_DIR"/Xncd16 \
				    "$SERVER_DIR"/Xncd16.old
			    fi
			    ( echo
			      echo "Linking "$SERVER_DIR/Xncd16" to"
			      echo "\"$SERVER_DIR/$FILE\"."
			    ) | $SAY
			    ln "$SERVER_DIR"/"$RELEASE_DIR"/$FILE "$SERVER_DIR"/Xncd16
			fi
			;;
		    16e)
			if test ! "$WANT"
			then
			    ( echo
			      echo "With the NCD16e server you can make either"
			      echo "the full function server or the local"
			      echo "telnet client server the default."
			    ) | $SAY

			    eval $ASK "Do you want to use the full function server as the default [y]?"
			    read ANSWER
			    case "$ANSWER" in
				n| no| N| NO)
				    WANT="L"
				    ;;
				q| quit| Q| QUIT)
				    exit $EXIT_QUIT
				    ;;
				*)
				    WANT="F"
				    ;;
			    esac
			fi
			GOT=`expr "$FILE" : "Xncd16e_lt"`
			if test $GOT -eq 0
			then
			    GOT="F"
			else
			    GOT="L"
			fi
			if test $WANT = $GOT
			then
			    if test -f "$SERVER_DIR"/Xncd16e
			    then
				rm -f "$SERVER_DIR"/Xncd16e.old
				( echo
				  echo "Moving \"$SERVER_DIR/Xncd16e\" to"
				  echo "\"$SERVER_DIR/Xncd16e.old\"."
				) | $SAY
				mv "$SERVER_DIR"/Xncd16e \
				    "$SERVER_DIR"/Xncd16e.old
			    fi
			    ( echo
			      echo "Linking "$SERVER_DIR/$RELEASE_DIR/Xncd16e" to"
			      echo "\"$SERVER_DIR/$FILE\"."
			    ) | $SAY
			    ln "$SERVER_DIR/$RELEASE_DIR"/$FILE "$SERVER_DIR"/Xncd16e
			fi
			;;
		    17c)
			if test ! "$WANT"
			then
			    ( echo
			      echo "With the NCD17c server you can make either"
			      echo "the full function server or the local"
			      echo "telnet client server the default."
			    ) | $SAY

			    eval $ASK "Do you want to use the full function server as the default [y]?"
			    read ANSWER
			    case "$ANSWER" in
				n| no| N| NO)
				    WANT="L"
				    ;;
				q| quit| Q| QUIT)
				    exit $EXIT_QUIT
				    ;;
				*)
				    WANT="F"
				    ;;
			    esac
			fi
			GOT=`expr "$FILE" : "Xncd17c_lt"`
			if test $GOT -eq 0
			then
			    GOT="F"
			else
			    GOT="L"
			fi
			if test $WANT = $GOT
			then
			    if test -f "$SERVER_DIR"/Xncd17c
			    then
				rm -f "$SERVER_DIR"/Xncd17c.old
				( echo
				  echo "Moving \"$SERVER_DIR/Xncd17c\" to"
				  echo "\"$SERVER_DIR/Xncd17c.old\"."
				) | $SAY
				mv "$SERVER_DIR"/Xncd17c \
				    "$SERVER_DIR"/Xncd17c.old
			    fi
			    ( echo
			      echo "Linking "$SERVER_DIR/$RELEASE_DIR/Xncd17c" to"
			      echo "\"$SERVER_DIR/$FILE\"."
			    ) | $SAY
			    ln "$SERVER_DIR/$RELEASE_DIR"/$FILE "$SERVER_DIR"/Xncd17c
			fi
			;;
		    19)
			if test ! "$WANT"
			then
			    ( echo
			      echo "With the NCD19 server you can make either"
			      echo "the full function server or the local"
			      echo "telnet client server the default."
			    ) | $SAY

			    eval $ASK "Do you want to use the full function server as the default [y]?"
			    read ANSWER
			    case "$ANSWER" in
				n| no| N| NO)
				    WANT="L"
				    ;;
				q| quit| Q| QUIT)
				    exit $EXIT_QUIT
				    ;;
				*)
				    WANT="F"
				    ;;
			    esac
			fi
			GOT=`expr "$FILE" : "Xncd19_lt"`
			if test $GOT -eq 0
			then
			    GOT="F"
			else
			    GOT="L"
			fi
			if test $WANT = $GOT
			then
			    if test -f "$SERVER_DIR"/Xncd19
			    then
				rm -f "$SERVER_DIR"/Xncd19.old
				( echo
				  echo "Moving \"$SERVER_DIR/Xncd19\" to"
				  echo "\"$SERVER_DIR/Xncd19.old\"."
				) | $SAY
				mv "$SERVER_DIR"/Xncd19 \
				    "$SERVER_DIR"/Xncd19.old
			    fi
			    ( echo
			      echo "Linking "$SERVER_DIR/$RELEASE_DIR/Xncd19" to"
			      echo "\"$SERVER_DIR/$FILE\"."
			    ) | $SAY
			    ln "$SERVER_DIR/$RELEASE_DIR"/$FILE "$SERVER_DIR"/Xncd19
			fi
			;;
		    *)
			( echo
			  echo "An internal error in the install script has"
			  echo "been encountered.  Please contact NCD for"
			  echo "assistance."
			) | $SAY
			exit $EXIT_FAILED
			;;
		esac
	    done
	    ( echo
	      echo "The server boot image has been installed."
	    ) | $SAY
	else
	    ( echo
	      echo "The installation has failed.  Contact NCD for assistance."
	    ) | $SAY
	    exit $EXIT_FAILED
	fi
    fi
else
    ( echo
      echo "Since there is no server image in the current directory, it"
      echo "is assumed that a font tape is being installed."
    ) | $SAY
fi
NOT_YET="The fonts have not been installed."

if $TFTP_RESTRICTED
then
    if test "$TFTP_DIR" = "/"
    then
	TFTP_DIR=""
    fi
else
    TFTP_DIR=""
fi

( echo
  echo "The font directories must be installed where they can be"
  echo "accessed by the TFTP daemon, NFS, or DAP.  If you are planning"
  echo "to use TFTP, the correct place appears to be the directory"
  echo "\"$TFTP_DIR$FONT_DIR\".  If you are not planning to use TFTP,"
  echo "the correct place (asuming the default font path) is"
  echo "\"$FONT_DIR\"."
) | $SAY
FONT_DIR="$TFTP_DIR$FONT_DIR"

eval $ASK "Enter the name of the directory which will contain the font directories [$FONT_DIR]?"
read ANSWER
if test "$ANSWER"
then
    case "$ANSWER" in
	q| quit| Q| QUIT)
	    exit $EXIT_QUIT
	    ;;
	*)
	    FONT_DIR="$ANSWER"
	    ;;
    esac
fi

while test ! -d "$FONT_DIR"
do
    ( echo
      echo "The directory you entered, $FONT_DIR, doesn't exist."
      echo "Please enter the pathname of the directory in which to"
      echo "install the font directories, or q to quit."
    ) | $SAY

    eval $ASK "Enter the name of the directory which will contain the font directories [$FONT_DIR]?"
    read ANSWER
    if test "$ANSWER"
    then
	case "$ANSWER" in
	    q| quit| Q| QUIT)
		exit $EXIT_QUIT
		;;
	    *)
		FONT_DIR="$ANSWER"
		;;
	esac
    fi
done

touch tf.$$
if test -f "$FONT_DIR"/tf.$$
then
    rm -f tf.$$
    ( echo
      echo "You appear to be in \"$FONT_DIR\"."
    ) | $SAY
    STATUS=0
else
    rm -f tf.$$
    ( echo
      echo "The font directories will be installed in \"$FONT_DIR\"."
    ) | $SAY
    (tar cf - ncd | (cd "$FONT_DIR"; tar xf -))
    STATUS=$?
fi
if test $STATUS -eq 0
then
    ( echo
      echo "The fonts have been installed."
    ) | $SAY
    NOT_YET=""
else
    ( echo
      echo "The installation has failed.  Contact NCD for assistance."
    ) | $SAY
    exit $EXIT_FAILED
fi

exit 0
