#!/bin/ksh
#
# @(#)VEETEST configuration utility.
#
# This script is provided to make the needed configuration changes to
# a system that has access to VEE, but did not install it.   That is,
# you would use it if there is an installed VEE on an NFS mounted disk 
# (not on your local machine).
#
# Run this script as root with the command:  ./config/configVee in the
#  actual dirctory that VEE is installed in.  It does the following:
#
# 1) check_arpa - checks to see if networking files exist.  reports if not.
#
# 2) check_etc_services - Check for veesm entry in /etc/services or NIS.
#
# 3) config_inetd_conf - Put veesm in the /etc/inetd.conf file, reconfig inetd.
#
# The following items are not done on HP-UX 10.0 nfs client systems.
#
# 4) config_veesm - link veesm (/usr/etc or /usr/sbin) to actual executable.
#
# 5) links /usr/bin/veetest to actual executable
#
# 6) copies man pages over if it can (can't if man directory is nfs mount)
#
# 7) config_appdefaults - copies over the appdefaults file.
#
# 8) warn_nis - tells user that networking is not locally configurable if
#               Network Information System (aka Yellow pages) is configured.
#
# Copyright 1995 Hewlett-Packard Company, All Rights Reserved.
#
# Explicitly set the PATH, so that we know where commands are coming
# from.  This is just a safety measure, in case commands are accidently
# invoked without a specific path name.
#
PATH='/bin:/usr/bin:/etc:/lib:/usr/local/bin:/usr/contrib/bin:/usr/ucb'
export PATH

scriptName=$(basename $0)
# some defines.
SUCCESS=0
FAILURE=1
WARNING=2
EXCLUDE=3
PRODUCT=veetest
YPBIND_RUNNING=NO

NineOh=$(echo $(uname -r) | grep '09\.')
PS="/bin/ps -ae" # works ok on Solaris, but not SunOS

veesmFile="/usr/sbin/veesm"
installDir="/opt/$PRODUCT"
ISSERVER=YES
if [ -f /bin/hp-pa ]; then
    AppDefaultsDir="/usr/lib/X11/app-defaults"
    if [ ! -z "$NineOh" ]; then
      # HP-UX 9.x of some kind.
      veesmFile="/usr/etc/veesm"
      installDir="/usr/lib/$PRODUCT"
	else
      # HP-UX 10.0, must determine if we are a client or server.
	  SERVER=$(dcnodes -rb)
	  MYNAME=$(hostname)
      if [ "$SERVER" != "$MYNAME" ]; then
		ISSERVER=NO
	  fi
    fi
else
    # assume Sun Solaris
    AppDefaultsDir=/usr/openwin/lib/app-defaults
fi

error() { echo "ERROR:   $*"; }
note()  { echo "NOTE:    $*"; }
warn()  { echo "WARNING: $*"; }
msg()   { echo " $*"; }
pmsg()  { print -n " $*"; }

#########################################################################
# Function:    must_be_root    
# Purpose:     check is user is root
#########################################################################
must_be_root()
{
  case "$(whoami)" in
     root)
        : # ok
     ;;
     *)
        error "You must be root to use this command."
        exit 1
     ;;
  esac
}

#########################################################################
# Purpose: Check for existance of veetest executable and set the DIR
#          variable.
#########################################################################
check_for_vee()
{
    if [ -x veetest -a -s veetest -a -f veetest ]; then
        installDir=$(pwd)
    else
        error "Can't find the veetest executable.  Please run this script"
        msg   "from the directory that VEE is installed in."
        msg   ""
        msg "configVee aborting."
        msg   ""
        exit 1
    fi
}

#########################################################################
# Check some basic configuration information
#########################################################################
check_arpa() {

    if [ ! -f /etc/services ]; then
        msg
        warn "Unable to find the networking file, /etc/services"
        msg  " This may mean that NIS (Network Information System,"
        msg  " previously known as Yellow Pages) is installed, or that"
        msg  " networking has not been configured correctly on this"
        msg  " machine. Please see your system administrator."
        msg  " Will create /etc/services."
        msg
        touch /etc/services
    fi

    if [ ! -f /etc/inetd.conf ]; then
        msg
        warn "Unable to find the networking file, /etc/inetd.conf"
        msg  " This may mean that NIS (Network Information System,"
        msg  " previously known as Yellow Pages) is installed, or that"
        msg  " networking has not been configured correctly on this"
        msg  " machine. Please see your system administrator."
        msg  " Will create /etc/inetd.conf"
        msg
        touch /etc/inetd.conf
    fi
}

#########################################################################
# Check to see if ypbind is running
#########################################################################
check_ypbind()
{
    if [ $($PS | grep ypbind | grep -v grep | wc -l) -gt 0 ]; then
        YPBIND_RUNNING=YES
    fi
}

#########################################################################
# If ypbind is not running, then customize the /etc/services file.
# In either case:
# Make sure the veesm service is there with the correct port number.
# Make sure no one else is using the assigned port number.
#########################################################################
config_etc_services() {

    # is /etc/services managed by NIS (nee YellowPages)
    YPBIND_RUNNING=NO
	check_ypbind
    if [ "$YPBIND_RUNNING" = "YES" ]; then
		check_ypcat_for_veesm
    else
		check_etc_services		
    fi
}

#########################################################################
# Check (and modify if needed) the veesm entry in /etc/services.
#########################################################################
check_etc_services()
{
    pmsg "Checking 'veesm' entry in /etc/services..."
    AOK=YES

    line=$(grep '^veesm' /etc/services)
    portline2=$(echo ${line} | grep '[ \t]4789/tcp')
    portline=$(grep '^[^#].*[ \t]4789/tcp' /etc/services | grep -v "veesm")

    if [ "${line}" != "" ]; then
        if [ "${portline2}" = "" ]; then
            # have a line with veesm in it, but incorrect port number
			echo
			msg "  Modifying port number entry in /etc/services"
            grep -v "veesm" /etc/services >> /tmp/services$$
            mv /tmp/services$$ /etc/services
            echo "veesm 4789/tcp   #HP VEE service manager" >> /etc/services
			AOK=NO
        fi
    else
        msg "  Adding entry for veesm in /etc/services"
        echo "veesm 4789/tcp   #HP VEE service manager" >> /etc/services
		AOK=NO
    fi

    # Check file to make sure no one else is using our port.
    if [ "${portline}" != "" ]; then
        msg
        warn "The default services number for veesm (4789/tcp) is already assigned."
        msg  "Please examine the /etc/services file to find the conflicting entry"
        msg  "and change it.  Port 4789 is an assigned port for veesm."
        msg  "Invalid entry:"
        msg  "${portline}"
        msg
		AOK=NO
    fi

    if [ "$AOK" = "YES" ] ;then
        msg "OK"
    fi
}

#########################################################################
# Determine if the NIS (yellow pages) veesm entry is correct.
# Warn if missing, incorrect port number or someone else is using 4789.
#########################################################################
check_ypcat_for_veesm()
{
    pmsg "NIS running, checking 'veesm' entry in \"ypcat services.byname\"..."
   	AOK=YES

    line=$(ypcat services.byname |grep "^veesm")
    portline=$(echo $line | grep '^[^#].*[ \t]4789/tcp' | grep -v "veesm")
    portline2=$(echo $line | grep '[ \t]4789/tcp')
    if [ "${line}" != "" ]; then
      # NIS has an entry.
        if [ "${portline2}" = "" ]; then
            # have a line with veesm in it, but incorrect port number.
            msg ""
            warn "The veesm NIS services entry has an invalid port number."
            msg  "Change the entry in the NIS services.byname database file to:"
            msg  "'veesm  4789/tcp  #HP VEE service manager'"
            msg  "Invalid entry:"
            msg  "'$line'"
			AOK=NO
         fi
    else
        # NIS is missing the veesm entry.
        msg  ""
        note "The entry for veesm is missing from your NIS services.byname database,"
        msg  "add the following entry to the NIS database and reconfigure inetd:"
        msg  "'veesm  4789/tcp  #HP VEE service manager'"
		AOK=NO
    fi

    # Check file to make sure no one else is using our port.
    if [ "${portline}" != "" ]; then
        msg  ""
        warn "The default services number for veesm (4789/tcp) is already assigned."
        msg  "Please examine the NIS database file for services to find the"
        msg  "conflicting entry.  Port 4789 is an assigned port for veesm."
        msg  "Invalid entry:"
        msg  "'${portline}'"
		AOK=NO
        return $WARNING
    fi

    if [ "$AOK" = "YES" ] ;then
        msg "OK"
    fi
}


########################################################################
# Kick start the inet daemon
########################################################################
sighup_inetd() {

    NUM=$(/bin/ps -ae | grep inetd  | grep -v grep | awk '{print $1}')
    /bin/kill -1 $NUM  # jog inetd so it re-reads inetd.conf file.
}


#########################################################################
# Customize the /etc/inetd.conf file if needed.
#########################################################################
config_inetd_conf() {

	pmsg "Checking 'veesm' entry in /etc/inetd.conf..."
    AOK=YES
    line=$(grep "^veesm" /etc/inetd.conf)

    if [ "${line}" = "" ]; then
        # no veesm entry.  Add it.
		echo
        msg "  Adding entry for veesm in /etc/inetd.conf"
        echo "veesm stream tcp nowait root $veesmFile veesm" >> /etc/inetd.conf
        sighup_inetd
    else
        line=$(grep "^veesm" /etc/inetd.conf | grep $veesmFile)
        if [ "${line}" = "" ]; then
			echo
            msg "  Modifying veesm entry in /etc/inetd.conf"
            # Have bad entry.  Take it out and put in a new one.
            grep -v veesm /etc/inetd.conf > /tmp/inetd.$$       
            mv /tmp/inetd.$$ /etc/inetd.conf    
            echo "veesm stream tcp nowait root $veesmFile veesm" >> /etc/inetd.conf
            sighup_inetd
        fi
    fi

    if [ "$AOK" = "YES" ] ;then
        msg "OK"
    fi
}

#########################################################################
# Link the veesmFile entry in inetd.conf to the actual executable.
#########################################################################
config_veesm() {
    rm -f $veesmFile
    msg "link: ln -s ${installDir}/veesm $veesmFile"
    ln -s ${installDir}/veesm $veesmFile > /dev/null 2>&1
}

#########################################################################
# Create app-defaults/VEE if needed.
#########################################################################
config_appdefaults() {

    # Move the app-defaults/Vee file over.
    if [ -f $AppDefaultsDir/Vee ]; then
        if [ -f $AppDefaultsDir/Vee.old ]; then
            msg "rm:   rm $AppDefaultsDir/Vee"
            rm $AppDefaultsDir/Vee
        else
            msg "move: mv $AppDefaultsDir/Vee to $AppDefaultsDir/Vee.old"
            mv $AppDefaultsDir/Vee $AppDefaultsDir/Vee.old
        fi
    fi
    msg "copy: cp ${installDir}/config/Vee $AppDefaultsDir/Vee"
    cp ${installDir}/config/Vee $AppDefaultsDir/Vee

    if [ -f $AppDefaultsDir/HyperHelp ]; then
        mv $AppDefaultsDir/HyperHelp $AppDefaultsDir/HyperHelp.old
    fi

}

#================================= MAIN ================================
#=======================================================================
# Start
# Make sure this script is run as root.
#=======================================================================
must_be_root

#=======================================================================
# Look at command line options.
#=======================================================================
if [ "$1" ]; then
    if [ "$1" = "-inetd" ]; then
        #kick inetd
        msg "Sending the inet daemon a SIGHUP"
        sighup_inetd
        exit 1
    else
        msg "Usage: configVee [-inetd]."
        exit 1
    fi
fi

msg 
msg "$scriptName starting on host $(hostname)."
msg 

#=======================================================================
# Check to see if we are running this script in the directory where
# VEE exists.  Lots of links are going to get created that rely on this.
#=======================================================================
check_for_vee

#=======================================================================
# Check to see if networking files exist, see if ypbind is running.
#=======================================================================
check_arpa

#========================================================================
# Enable veesm in /etc/services or check for NIS services.
#========================================================================
config_etc_services

#========================================================================
# Put veesm in the /etc/inetd.conf file.
# Also force inetd to re-look at the file.
#========================================================================
config_inetd_conf

if [ "$ISSERVER" = "NO" ]; then
	# UX 10.0 client.  Bail out.
	msg ""
	msg "$scriptName finished."
	msg ""
	exit 0
fi

#========================================================================
# link veesm where the pointer entry is in /etc/inted.conf to the actual
# executable.
#========================================================================
config_veesm

#========================================================================
# Make a symbolic link in /usr/bin to veetest
#========================================================================
rm -f /usr/bin/${PRODUCT}
msg "link: ln -s ${installDir}/${PRODUCT} /usr/bin/${PRODUCT}"
ln -s ${installDir}/${PRODUCT} /usr/bin/${PRODUCT}

#========================================================================
# Copy the man pages over if they exist.
# If the man pages directory is not writable give a notice msg if there
# is not a version of the vee man page.
#========================================================================
if [ -f ${installDir}/man/veetest.1 ]; then
    mkdir /usr/man > /dev/null 2>&1    # just to be sure
    mkdir /usr/man/man1 > /dev/null 2>&1 # just to be sure
    rm -f /usr/man/man1/veedoc.1 /usr/man/man1/veetest.1 # remove old ones
    msg "copy: cp ${installDir}/man/veetest.1 /usr/man/man1"
    cp ${installDir}/man/veetest.1 /usr/man/man1/ > /dev/null 2>&1
    if [ $? -ne 0 ]; then
        # The copy failed.  Print a notice if some man pages don't exist.
        if [ ! -f /usr/man/man1/veetest.1 ]; then
            note "The man pages directory is not writeable by this system so"
            msg  "the VEE man pages were not copied over to the man page directory."
            msg  "Contact your system administrator if you want the man pages."
        fi
    fi
fi

#========================================================================
# configure app-defaults if needed.
#========================================================================
config_appdefaults

#========================================================================
# Finish
#========================================================================
msg ""
msg "$scriptName finished."
msg ""

exit 0
