#
# Copyright (c) 1999-2005 Easysoft Ltd. All rights reserved.
#
# $Id: install_service 60 2005-07-01 14:12:12Z martin $
#
# This script installs services/inetd (xinetd) entries for a server.
#
# Calling format:
#
# install_service product product_install_dir
#                 install_subdir shell_startup_name template_prefix
#
# product - the product name used in output (spaces allowed)
# product_install_dir - base easysoft install path
# install_subdir - subdir where product is installed
# shell_startup_name - name of the shell script to be run by {x}inetd
# template_prefix - prefix to add to template files services, inetd.conf and
#                   xinetd.conf (pass "" for none)
#
# returns:
#
# 0 = success
# 1 = invalid command line
# 2 = environment variable not defined
# 3 = something we require is missing
# 99 = install aborted at user request
#
# Solaris 10 and SMF
# In Solaris 10 inetd services are no longer defined in /etc/inetd.conf.
# SMF - Service Management Facility is used now.
# Although you can write a inetd.conf like file and run inetconv to convert
# the Sun suggested way appears to be to define your service using the
# service_bundle.dtd.1 and import it with svccvg.
#
# xinetd
# ======
#
# xinetd uses /etc/xinetd.conf by default.
# A service entry in xinetd.conf looks like:
#
# service esoobserver
# {
#     stuff
# }
#
# If xinetd.conf contains the IncludeDir setting this points to a directory
# where each file (not beginning with a .) is a service definition.
#
# darwin
# ======
#
# inetd on MAC OS X appears to get its service defns from netinfo and
# not /etc/services. We need to use niutil commands to do this.
# Max OS X 10.2.n appears to run inetd AND xinetd, Apple say for Compatability
# period but advise moving everything to xinetd.
#
if [ "$ECHO" = "" ];then
  echo "error - ECHO not defined, expect it to be exported"
  exit 2
fi
if [ "$TESTEXISTS" = "" ];then
  $ECHO "error - TESTEXISTS not defined, expect it to be exported"
  exit 2
fi
if [ "$SUPPORT_EMAIL" = "" ];then
  $ECHO "error - SUPPORT_EMAIL not defined, expect it to be exported"
  exit 2
fi
if [ "$PIDARG" = "" ];then
  $ECHO "error - PIDARG not defined, expect it to be exported"
  exit 2
fi
if [ "$PSARGS" = "" ];then
  $ECHO "error - PSARGS not defined, expect it to be exported"
  exit 2
fi
cwd=`pwd`
#
# Check command line
#
cmdline_error=""
if [ $# -ne 5 ];then
  cmdlinestr="incorrect number of parameters"
  cmdline_error="1"
else
  PRODUCTLONG="$1"
  PRODUCTINSTALLPATH="$2"
  PRODUCTSUBDIR="$3"
  SHELLSTARTUPNAME="$4"
  TEMPLATEPREFIX="$5"
fi
#
# If command line error
#
if [ "$cmdline_error" = "1" ];then
  $ECHO "\"$*\""
  $ECHO "Invalid command line, $cmdlinestr"
  $ECHO "Usage:"
  $ECHO "install_service product product_install_path install_subdir shell_startup_name template_prefix"
  exit 1
fi
#
# template filenames
#
TEMPLATESERVICE="${TEMPLATEPREFIX}service.conf"
#
# See what service/inetd files we can find.
#
SERVICE_FILE="/etc/services"	# service file
INETD_CONF="/etc/inetd.conf"	# default inet config file
XINETD_CONF="/etc/xinetd.conf"	# default xinetd config file
INETD_REREAD="HUP"		# get inetd to reread config
XINETD_REREAD="USR2"		# get xinetd to reread config
XINET_INCLUDEDIR=""		# xinetd.conf IncludeDir value
INETD_PIDFILES="/var/run/inetd.pid /etc/inetd.pid"
TMPSERVICES="services.tmp"	# temp file for service names/port on Darwin

PLATFORM=`uname -s`
#
# Solaris 10
#
osver=`uname -r`
os2=`$ECHO $osver | awk -F. '{print $2}'`
if [ \( "$PLATFORM" = "SunOS" \) -a \( "$os2" = "10" \) ];then
  # hunt down svccfg
  svccfg=""
  if [ -x /usr/sbin/svccfg ];then
    svccfg="/usr/sbin/svccfg"
  elif [ -x /sbin/svccfg ]; then
    svccfg="/sbin/svccfg" ]
  else
    svccfg quit
    if [ $? eq 0 ];then
      svccfg="svccfg"
    fi
  fi
  if [ "$svccfg" = "" ];then
    $ECHO "**WARNING** Don't know where the svccfg command is"
    $ECHO "Cannot install the server under inetd"
    exit 3
  fi
  cwd=`pwd`
  cd $PRODUCTINSTALLPATH/$PRODUCTSUBDIR
  if [ ! $TESTEXISTS server/install ]; then
    $ECHO "**WARNING** Cannot find server/install dir"
    $ECHO "Cannot install the server under inetd"
    exit 3
  fi
  cd server/install
  xmlcfg=`ls *$SHELLSTARTUPNAME*.xml 2>/dev/null | tail -1`
  if [ "$xmlcfg" = "" ];then
    $ECHO "**WARNING** Cannot find any server XML config files"
    $ECHO "Cannot install the server under inetd"
    exit 3
  fi
  $ECHO "validate $xmlcfg" > validate.cmd
  $ECHO "import $xmlcfg" > import.cmd
  $svccfg -v -f validate.cmd
  if [ $? -ne 0 ];then
    $ECHO "**WARNING** Failed to validate $xmlcfg"
    $ECHO "Cannot install the server under inetd"
    exit 4
  fi
  $svccfg -v -f import.cmd
  if [ $? -ne 0 ];then
    $ECHO "**WARNING** Failed to import $xmlcfg"
    $ECHO "Cannot install the server under inetd"
    exit 4
  fi
  ####inetadm -l svc:/network/esoobserver/tcp
  exit 0
fi
$ECHO "Looking for service/{x}inetd files..."
#
#  Check the services file already exists.
#
$ECHO
$ECHO "Checking for your services file..."

if [ "$PLATFORM" = "Darwin" ];then
  if [ ! -x /usr/bin/niutil ];then
    $ECHO "**WARNING** Cannot find niutil command"
    $ECHO "Service entry cannot be installed into netinfo."
    exit 3
  fi
  $ECHO "Generating temporary list of services/ports."
  $ECHO "This may take a few minutes."
  SERVICE_NAMES=`/usr/bin/niutil -list . /services | awk '{print $2}'`
  >$TMPSERVICES
  > s.awk
  echo '/name:/ {name=$2}' >> s.awk
  echo '/port:/ {port=$2}' >> s.awk
  echo "END {print name, port}" >> s.awk
  for SNAME in $SERVICE_NAMES
  do
    echo -n .
    SRV=`/usr/bin/niutil -read . /services/$SNAME 2>/dev/null | awk -f ./s.awk`
    if [ $? -ne 0 ];then
      continue
    fi
    DNAME=`echo $SRV | awk '{print $1}'`
    DPORT=`echo $SRV | awk '{print $2}'`
    echo "$DNAME $DPORT" >> $TMPSERVICES
  done
  rm ./s.awk
  echo
  SERVICE_FILE=`pwd`/services.tmp
else
  while [ ! -w "$SERVICE_FILE" ]
  do
    $ECHO "**WARNING** Failed to find writeable $SERVICE_FILE file"
    SERVICE_FILE=""
    while [ "$SERVICE_FILE" = "" ]
    do
      $ECHO
      $ECHO "Enter the path to your services file (leave empty to abort) : \c"
      read SERVICE_FILE
      if [ "$SERVICE_FILE" = "" ];then
        $ECHO "Installation aborted"
        exit 99
      fi
    done
  done
fi
$ECHO "  using service file ${SERVICE_FILE}"
#
#  Check the inetd configuration file already exists.
#
$ECHO
$ECHO "Checking for inetd/xinetd configuration files..."
if [ ! -w "${INETD_CONF}" ]; then
  $ECHO "  install did not find a writable ${INETD_CONF} file."
  INETD_CONF=""
else
  $ECHO "  install found a writable ${INETD_CONF} file."
fi
if [ ! -w "${XINETD_CONF}" ]; then
  $ECHO "  install did not find a writable ${XINETD_CONF} file."
  XINETD_CONF=""
else
  $ECHO "  install found a writable ${XINETD_CONF} file."
  grep -i includedir < "${XINETD_CONF}" 2>/dev/null 1>/dev/null
  if [ $? -eq 0 ];then
    XINETD_INCLUDEDIR=`grep -i includedir < "${XINETD_CONF}" | awk '{print $2}'`
    $ECHO "  xinetd is configured with IncludeDir ${XINETD_INCLUDEDIR}"
    if [ ! "$TESTEXISTS" "${XINETD_INCLUDEDIR}" ];then
      $ECHO "  **WARNING** but ${XINETD_INCLUDEDIR} does not seem to exist"
      XINETD_INCLUDEDIR=""
    else
      if [ ! -d "${XINETD_INCLUDEDIR}" ];then
        $ECHO "  **WARNING** but ${XINETD_INCLUDEDIR} is not a directory"
        XINETD_INCLUDEDIR=""
      fi
    fi
  fi
fi
#
# Determine if this machine is using inetd or xinetd
#
$ECHO "Attempting to determine if you are running inetd or xinetd..."
if [ "$PIDARG" = "2" ];then
  pidxinetd=`ps $PSARGS | grep xinetd | grep -v grep | awk '{print $2}'`
  pidinetd=`ps $PSARGS | grep inetd | grep -v xinetd | grep -v grep | grep -v esoob | awk '{print $2}'`
else
  pidxinetd=`ps $PSARGS | grep xinetd | grep -v grep | awk '{print $1}'`
  pidinetd=`ps $PSARGS | grep inetd | grep -v xinetd | grep -v grep | grep -v esoob | awk '{print $1}'`
fi
#
# what if we have multiple pids - which one?
#
if [ "$pidinetd" != "" ];then
  pids=`$ECHO "$pidinetd" | wc -w | awk '{print $1}'`
  if [ "$pids" -gt 1 ];then
    #
    # More than 1 pid found
    #
    $ECHO "  Found more than one process which looks like inetd"
    $ECHO "  Looking for inetd.pid file"
    runpid=""
    for pf in $INETD_PIDFILES
    do
      if [ \( "$runpid" = "" \) -a \( "$TESTEXISTS" "$pf" \) ];then
        runpid=`cat "$pf"`
	if [ "$runpid" != "" ];then
          $ECHO "$pidinetd" | grep "$runpid" 2>/dev/null 1>/dev/null
	  if [ "$?" -ne 0 ];then          # not found
            runpid=""
          else
            $ECHO "  Found $pf containing matching pid $runpid"
          fi
        fi
      fi
    done
    if [ "$runpid" = "" ];then
      $ECHO "  WARNING: Could not identify a running inetd server"
      pidinetd=""
    else
      pidinetd="$runpid"
    fi
  fi
fi
#
sssitu=""
sspid=""
ssname=""
sssignal=""
if [ "$pidxinetd" != "" ];then	                # xinetd
  if [ "$pidinetd" != "" ];then	                # xinetd & inetd
    #
    # We have found inetd and xinetd - which one?
    # Look for configuration files and if we find only one
    #
    $ECHO "  **WARNING** You appear to have both inetd and xinetd running!"
    if [ "${INETD_CONF}" != "" ]; then          # inetd.conf
      if [ "${XINETD_CONF}" != "" ]; then       # inet.conf & xinetd.conf
        $ECHO "  **WARNING** and you have configuration files for both SuperServers"
        $ECHO "  You will have to indicate which SuperServer you are using."
        sssitu="both"
      else                                      # inetd.conf & !xinetd.conf
        $ECHO "  Guessing inetd based on existence of ${INETD_CONF}"
        sspid="$pidinetd"
        ssconf="${INETD_CONF}"
        sssitu="guessed"
        guess="inetd"
      fi
    else			                # !inetd.conf
      if [ "${XINETD_CONF}" != "" ]; then       # !inetd.conf & xinetd.conf
        $ECHO "  Guessing xinetd based on existence of ${XINETD_CONF}"
        sspid="$pidxinetd"
	ssconf="${XINETD_CONF}"
        sssitu="guessed"
        guess="xinetd"
      else			                # !inetd.conf & !xinetd.conf
        $ECHO "  **WARNING"" and no configuration files for either"
        sssitu="both"
      fi
    fi
  else
    $ECHO "  Found a running xinetd but no inetd"
    sspid="$pidxinetd"
    ssconf="${XINETD_CONF}"
    sssitu="xinetd"
  fi
elif [ "$pidinetd" != "" ];then
  $ECHO "  Found a running inetd but no xinetd"
  sspid="$pidinetd"
  ssconf="${INETD_CONF}"
  sssitu="inetd"
else				                # !inetd & !xinetd
  $ECHO "  **WARNING** I could not find a running inetd or xinetd"
  sspid=""
  sssitu="none"
  if [ "${INETD_CONF}" != "" ];then             # inetd.conf
    if [ "${XINETD_CONF}" != "" ];then          # inetd.conf & xinetd.conf
      $ECHO "  and multiple configuration files could be found"
      ssconf=""
      guess=""
      sssitu="both"
    else                                        # inetd.conf & !xinetd.conf
      $ECHO "  Guessing inetd based existence of ${XINETD_CONF}"
      ssconf="${INETD_CONF}"
      guess="inetd"
      sssitu="none"
    fi
  else                                          # !inetd.conf
    if [ "${XINETD_CONF}" != "" ];then          # !inetd.conf & xinetd.conf
      $ECHO "  Guessing xinetd based on existence of ${XINETD_CONF}"
      ssconf="${XINETD_CONF}"
      guess="xinetd"
      sssitu="none"
    else
      $ECHO "  and no configuration files could be found"
      ssconf=""
      guess=""
    fi
  fi
fi
$ECHO
#
#
#
if [ "$sssitu" = "guessed" ];then
  cat <<EOF
The install script appeared to find a running inetd (PID=$pidinetd) and
xinetd (PID=$pidxinetd) SuperServer but thinks you are probably using $guess
based on the existence of $ssconf. The install can add this service to
$ssconf and it will be picked up by $guess when it is started but you
should check why there is no SuperServer running at this time.

EOF
  ssname="$guess"
elif [ "$sssitu" = "inetd" -o "$sssitu" = "xinetd" ]; then
  cat <<EOF
The install script found a running $sssitu (PID=$sspid) SuperServer - good.

EOF
  ssname="$sssitu"
elif [ "$sssitu" = "both" ];then
  #
  # Darwin 10.2.n has both inet/xinetd whilst Mac moving from inetd to xinetd
  #
  if [ "$PLATFORM" = "Darwin" ];then
    cat <<EOF
The install script found inetd and xinetd SuperServers which is not
totally unexpected on Darwin. Apple advise using xinetd.

EOF
  else
    cat <<EOF
The install script found what appears to be running inetd and xinetd
SuperServers and either configuration files for both or no recognised
configuration files at all. There is no further basis by which this script
can determine for itself which SuperServer you are using.

EOF
  fi
  answer=""
  while [ "$answer" = "" ]
  do
    $ECHO "Which SuperServer are you using? (q=quit install,inetd,xinetd): \c"
    read answer
    if [ "$answer" = "q" ];then
      $ECHO "Installation aborted at user request"
      exit 99
    elif [ "$answer" = "inetd" ];then
      ssname="inetd"
      ssconf="${INETD_CONF}"
      sspid="$pidinetd"
    elif [ "$answer" = "xinetd" ];then
      ssname="xinetd"
      ssconf="${XINETD_CONF}"
      sspid="$pidxinetd"
    else
      answer=""
    fi
  done
elif [ "$sssitu" = "none" ];then
  cat <<EOF
The install script could not find a running SuperServer, either inetd
or xinetd.
EOF
  if [ "$guess" != "" ];then
    cat <<EOF
However it did find the configuration file $ssconf so it is assuming
you are using $guess. You should check that this assumption is correct
before continuing. As a SuperServer configuration exists this script can
add the service entry to it so it will be picked up by $guess when it is
started.

EOF
  ssname="$guess"
  else
    #
    # No inetd or xinetd running and no config files.
    #
    cat <<EOF

You do not appear to be running inetd or xinetd and no configuration
files for either server could be found. Installing the $PRODUCTLONG service
will be skipped.

EOF
  exit 0
  fi
fi
if [ "$ssconf" = "" ];then
  cat <<EOF
The install script did not find a configuration file for $ssname.
Please enter the name of the configuration your $ssname uses.
EOF
  answer=""
  while [ "$answer" = "" ]
  do
    $ECHO "Name of $ssname configuration file? (q=quit): \c"
    read answer
    if [ "$answer" = "q" ];then
      $ECHO "Installation aborted at user request"
      exit 99
    elif [ "$answer" = "" ];then
      $ECHO "Please enter the path/filename of your inetd configuartion file"
    else
      if [ -w "$answer" ];then
        ssconf="$answer"
      else
        "$answer not found or not writable"
        answer=""
      fi
    fi
  done
fi
#
#  Check we have the correct signal to tell {x}inetd to reread its
#  configuration file.
#
if [ \( "$ssname" = "xinetd" \) -a \( "$sspid" != "" \) ];then
  cat <<EOF

You are running (or have chosen) to update xinetd.
By default xinetd rereads its config file when sent a $XINETD_REREAD
signal but it can be configured to use any signal. Please confirm you
want this script to ask xinetd to reread its configuration using
a $XINETD_REREAD signal. If you are unsure, the default is usually OK but
you can skip asking xinetd to reread the configuration and perform this
manually yourself by entering s.
EOF
  answer=""
  while [ "$answer" = "" ]
  do
    $ECHO "Use $XINETD_REREAD signal (y, s=skip, or enter other signal (e.g. HUP) [y]): \c"
    read answer
    if [ "$answer" = "" ];then
      answer="y"
    elif [ "$answer" = "s" ];then
      sspid=""
    elif [ "$answer" != "y" ];then
      SIGS=`kill -l | sed 's/SIG//g' | sed 's/[0-9]*)//g'`
      $ECHO "$SIGS" | grep "$answer" 1>/dev/null 2>/dev/null
      if [ $? -ne 0 ];then
        $ECHO "Cannot find signal $answer - please renter a valid signal"
        answer=""
      else
        XINETD_REREAD="$answer"
      fi
    fi
  done
  sssignal="$XINETD_REREAD"
else
  sssignal="$INETD_REREAD"
fi
#$ECHO "ssconf=$ssconf"
#$ECHO "sspid=$sspid"
#$ECHO "ssname=$ssname"
#$ECHO "sssignal=$sssignal"
#$ECHO "Press the return key to continue"
#read press

#
# Install services entry
# ======================
#
#
# Get the default port and service names from the configuration file.
#
cd $PRODUCTINSTALLPATH/$PRODUCTSUBDIR
if [ ! "$TESTEXISTS" "server/install/$TEMPLATESERVICE" ]; then
  cat <<EOF
Installation aborted
Failed to find the template service file "server/install/$TEMPLATESERVICE"
Please contact support at $SUPPORT_EMAIL mailing them the contents of this
session and they will rectify the problem.

EOF
  exit 99
fi
portnumber=`cat server/install/$TEMPLATESERVICE | awk '{print $2}' | awk -F/ '{print $1}'`
servicename=`cat server/install/$TEMPLATESERVICE | awk '{print $1}'`
subsservice="$servicename"
cat <<EOF

Attempting to install the service entries.

The service name provides a friendly mapping between textual names for
internet services, and their underlying assigned port numbers and protocol
types. {x}inetd will listen on behalf of a service and run it when a client
connects to the specified port. The defaults for the $PRODUCTLONG are:

Service Name: $servicename
Port Number: $portnumber

EOF

#
# Check that this service does not already exist.
#
$ECHO "Checking service name \"$servicename\" and port \"$portnumber\" are not already in use"
skip=""
SERVICE_LAST_ACTION=""
unique=`awk '{print $1}' < $SERVICE_FILE | grep "^$servicename$"`
while [ "$unique" != "" ]
do
  $ECHO "Service name \"$servicename\" already exists in the $SERVICE_FILE."
  $ECHO
  $ECHO "Possible conflicting service entries are:"
  grep "^$servicename[ 	].*$" $SERVICE_FILE
  $ECHO
  $ECHO "You can choose to replace this entry or specify a new unique service name".
  answer=""
  while [ \( "$answer" != "r" \) -a \( "$answer" != "d" \) -a \( "$answer" != "q" \) -a \( "$answer" != "s" \) ]
  do
    $ECHO "Replace (r), Define new service name (d), (q=abort): \c"
    read answer
  done
  if [ "$answer" = "q" ];then
    $ECHO "Installation aborted"
    exit 99
  elif [ "$answer" = "d" ];then
    $ECHO "service name: \c"
    SERVICE_LAST_ACTION="d"
    read servicename
  elif [ "$answer" = "r" ];then
    SERVICE_LAST_ACTION="r"
    cp $SERVICE_FILE $SERVICE_FILE.pre_ES
    grep -v "^$servicename[ 	].*$" $SERVICE_FILE.pre_ES > $SERVICE_FILE
    if [ "$PLATFORM" = "Darwin" ];then
      /usr/bin/niutil -destroy . /services/$servicename
    fi
    #cp $INETD_CONF $INETD_CONF.pre_ES
    #grep -v "^$servicename[ 	].*$" $INETD_CONF.pre_ES > $INETD_CONF
  fi
  unique=`awk '{print $1}' < $SERVICE_FILE | grep "^$servicename$"`
done
$ECHO "  Cannot find service name $servicename in $SERVICE_FILE - OK"
#
# Check the port is not already in use
#
defport="$portnumber"
spareport=""
while [ "$spareport" = "" ]
do
  if (grep -v "^#" "$SERVICE_FILE" | grep "$portnumber" 1>/dev/null)
  then
    $ECHO "Attempting to use $portnumber for the $PRODUCTLONG Server."
    $ECHO "This appears to be in use. Please choose another port."
    $ECHO "Port: \c"
    while [ "$newport" = "" ]
    do
	read newport
	$ECHO $newport | grep -E '^[0-9]+$' 1>/dev/null
	if [ $? = 1 ]; then
	  $ECHO "$newport invalid - please specify a port number"
	  newport=""
	fi
    done
    portnumber="$newport"
  else
    $ECHO "  Cannot find port number $portnumber in $SERVICE_FILE - OK"
  fi
  spareport="$portnumber"
done
$ECHO "Installing a $SERVICE_FILE entry for the $PRODUCTLONG"
$ECHO "as service name $servicename on port $spareport"
sed -e "s/$defport/$spareport/g" -e "s/$subsservice/$servicename/g" < "server/install/$TEMPLATESERVICE" > server/install/service.install
cat server/install/service.install >> $SERVICE_FILE
#
# darwin service entry
#
if [ "$PLATFORM" = "Darwin" ];then
  rm $TMPSERVICES
  $ECHO "Installing a service entry into netinfo"
  /usr/bin/niutil -list . /services/$servicename 2>/dev/null 1> /dev/null
  if [ $? -eq 0 ];then	# service exists already
    /usr/bin/niutil -destroy . /services/$servicename
  fi
  /usr/bin/niutil -create . /services/$servicename
  /usr/bin/niutil -createprop . /services/$servicename port $spareport
  /usr/bin/niutil -createprop . /services/$servicename protocol tcp tcp
fi
#
# Find a shell to place in the inetd/xinetd config file
# =====================================================
#
$ECHO
$ECHO "Looking for a shell to use in {x}inetd configuration file..."
if [ ! -x /bin/sh ];then
  $ECHO "/bin/sh does not seem to exist"
  $ECHO "Please enter the full path to a shell that may be used"
  shellpath=""
  while [ "$shellpath" = "" ]
  do
    $ECHO "full shell path: \c"
    read shell
    if [ ! "$TESTEXISTS" "$shell" ]; then
      $ECHO "$shell does not exist"
      shell=""
    elif [ ! -x "$shell" ]; then
      $ECHO "$shell is not executable"
      shell=""
    fi
    shellpath="$shell"
  done
else
  shellpath="/bin/sh"
fi
$ECHO "  Using $shellpath"
#
# Install inetd/xinetd configuration file entry
# =============================================
#
if [ "$ssconf" != "" ];then
  $ECHO "Writing new $ssconf entry for $ssname"
  if [ "$ssname" = "xinetd" ];then
    baseconf="xinetd.conf"
  else
    baseconf="inetd.conf"
  fi
  baseconf="${TEMPLATEPREFIX}$baseconf"
  if [ ! "$TESTEXISTS" "server/install/$baseconf" ]; then
      cat <<EOF
Installation aborted
Failed to find the template {x}inetd.conf file "server/install/$baseconf"
Please contact support at $SUPPORT_EMAIL mailing them the contents of this
session and they will rectify the problem.

EOF
      exit 99
  fi

  sed "s|SHELL|$shellpath|g" < "server/install/${baseconf}" > "server/install/${baseconf}.install1"
  sed "s|STARTUP|$PRODUCTINSTALLPATH/$PRODUCTSUBDIR/server/$SHELLSTARTUPNAME|g" < "server/install/${baseconf}.install1" > "server/install/${baseconf}.install2"
  sed "s/$subsservice/$servicename/g" < "server/install/${baseconf}.install2" > "server/install/${baseconf}.install3"
  if [ "$ssname" = "inetd" ];then
    #
    # inetd
    #
    cp "$ssconf" "$ssconf.pre_ES"
    grep -v "^$servicename[ 	].*$" "$ssconf.pre_ES" > "$ssconf"
    cat "server/install/${baseconf}.install3" >> $ssconf
  else
    #
    #  xinetd
    #
    grep -i includedir < "${ssconf}" 2>/dev/null 1>/dev/null
    if [ $? -eq 0 ];then
      XINETD_INCLUDEDIR=`grep -i includedir < "${ssconf}" | awk '{print $2}'`
      #
      #  xinetd.conf uses IncludeDir and the dir exists.
      #
      if [ \( "$TESTEXISTS" "${XINETD_INCLUDEDIR}" \) -a \( -d "${XINETD_INCLUDEDIR}" \) ];then
        #
        #  Move old file out of the way
        #
        if [ "$TESTEXISTS" "${XINETD_INCLUDEDIR}/$servicename" ];then
          mv "${XINETD_INCLUDEDIR}/$servicename" "${XINETD_INCLUDEDIR}/$servicename.tmp"
	  sed 's/^/# /' < "${XINETD_INCLUDEDIR}/$servicename.tmp" > "${XINETD_INCLUDEDIR}/$servicename"
	  rm "${XINETD_INCLUDEDIR}/$servicename.tmp"
          cat "server/install/${baseconf}.install3" >> "${XINETD_INCLUDEDIR}/$servicename"
        else
          cp "server/install/${baseconf}.install3"  "${XINETD_INCLUDEDIR}/$servicename"
        fi
      else			# no IncludeDir
        #
        #  xinetd does not use IncludeDir so just add entry to end of
        #  config file.
        #
        grep -i '^service[ 	]*'$subsservice "$ssconf"
        if [ $? = 0 ];then
          cp "$ssconf" "$ssconf.tmp"
          awk '/^service[ 	]*'$subsservice'/ {del=1}; {if (del != 1) print;} /}/ {del=0}' < "$ssconf.tmp" > "$ssconf"
        fi
        cat "server/install/${baseconf}.install3" >> $ssconf
      fi
    fi
  fi
  #
  #  Tell inetd to re-read conf files
  #
  if [ "$sspid" != "" ];then
    $ECHO "Informing $ssname (pid=$sspid) of the configuration file change"
    kill -"$sssignal" $sspid
  else
    $ECHO "No inetd/xinetd running so nothing to inform of configuration changes"
  fi
fi
cd $cwd

exit 0
