#! /bin/sh
#    LPD Configuration Utility installation script.
#
# 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.
#	./lpdinst
# 4) The installations script copies the nsconfig utility that corresponds
#    to the system type it is being installed on to the /usr/nic directory.
# 5) The nicprint.conf file is also copied to the /usr/nic directory.
#
# 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 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
#
# 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
	CHMOD=chmod
	CP=cp
	CAT=cat
	MV=mv
	GREP=grep
	LS=ls
        PS=ps
	SED=sed
        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
	BASE_PORT_NUM=10000

}

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 \) -a \( "$SYSTEM" != SunOS \) ]
    then
        SYSTEM="`$UNAME -s`_`$UNAME -m`"
    fi

    if [ \( "$SYSTEM" = SunOS \) ]
    then
        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 TCP/IP host resident software. Source files for the
necessary utilities can be found with the installation software. 

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="nsconfig nicprint.conf"
     fi

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

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

     if [ \( "$SYSTEM" = SunOS_4.x \) -o \( "$SYSTEM" = OSF1 \) ]
     then
         BUNDLE="nsconfig nicprint.conf"
     fi



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

     for x in $BUNDLE
     do
	$ECHO " $CWD/$SYSTEM/$x"

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

cat << EOM |more

ERROR : N.I.C. Configuration Utility Installation aborted. Required Utilities are missing.

	${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/$SYSTEM/*
	$CHMOD 666 $CWD/$SYSTEM/nicprint.conf
}

make_dir () {

	if [ -d $NICPRINT_DIR ]
		then
		$MV ${NICPRINT_DIR}/nsconfig ${NICPRINT_DIR}/nsconfig.old
		$MV ${NICPRINT_DIR}/nicprint.conf ${NICPRINT_DIR}/nicprint.conf.old
	else
	$MKDIR ${NICPRINT_DIR}
	fi
}
# 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/$SYSTEM  to ${NICPRINT_DIR}"
   #exec 4<&1                    # Throw away output from these commands
  # exec 5<&2
 #  exec > /dev/null 2>&1

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

 #  exec 1<&4 
 #  exec 2<&5 
}

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


final_message () {

    $ECHO
    	$ECHO "N.I.C. LPD Configuration Utility installed successfully."
        $ECHO
    	$ECHO "Exiting installation script."
    $ECHO
}

###########################################################
#
# 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.

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

 	$ECHO "Installing the LPD Configuration Utility from $CWD"
	make_dir
	copy_parts_to_nicprint
	final_message
        exit 1

