#!/usr/bin/ksh -p
#
# ident "@(#)utadm.sh	1.97 02/01/09 SMI"
#
# Copyright 1998-2002 Sun Microsystems, Inc.  All rights reserved.
#

#
#  Usage:  utadm [-c] [-r] [-p] [-f] [-n] [-a interface] [-d interface]
# 
#  assumes host that this script is run on is being configured as a single
# corona central server, meant to serve a collection of corona desktop units
# connected via the interfaces listed in the command line.
#  the default subnet for the first interface is: 192.168.128.1
#  the user will be prompted if they are willing to accept the default address
# for each interface, and if not, they have to give an new IP address and
# netmask.  It is assumed that whatever net the user chooses will have 250 or
# fewer contiguous addresses in the least significant byte (a less restrictive,
# but equivalent, assumption than that it will be a class C network).
#
#  this script sets up the newtnet according to the following scheme:
#    255.255.255.0	-- netmask (Class C)
#    192.168.X.1	-- server's ip address for an interface
#			   (X={128,129,130...} mapped in order of args)
#    192.168.X.255	-- broadcast address on interface X
#    192.168.X.0	-- address of network for interface X
#    192.168.X.1	-- address of gateway/router on network X
#    192.168.X.16	-- address of the first corona desktop unit on net X
#    192.168.X.240	-- address of the last corona desktop unit on net X
#
#  the symbolic hostname for network X is "<hostname>-<intf>".  the symbolic name
# for network X is "${CORONANAME}-<intf>".
#  we don't add symbolic names to the /etc/hosts file for each desktop, we
# get by with just the numeric names.
#  the dhcptab layout scheme is as follows:
#  * there is a corona-specific macro that is included by all interfaces
#    which contains lease time and negotiate enable.
#  * the remainder of the macros are per-interface, and include:
#      * the "<netname>" macro has the the loghost and facility level switches,
#        it has the auth server and port number, and it includes the intf's
#        log macro.
#      * there is also a network macro for each interface, it's name is the
#        IP address with "_"'s substituted for "."'s and contains the
#        broadcast IPA, the subnet mask, MTU, router IPA, and host IPA for
#        this interface.
#  * the defined symbols are:
#      * 21: AuthSrvr
#      * 22: AuthPort
#      * 23: NewtVer
#      * 24: LogHost
#      * 25: LogKern
#      * 26: LogNet
#      * 27: LogUSB
#      * 28: LogVid
#      * 29: LogAppl
#      * 31: FWSrvr
#      * 32: unused (was Display format index)
#      * 33: Intf
#
#### need to add script to add more desktops
#
export LC_ALL=C
export PATH=/usr/bin:/sbin:/usr/sbin

UTLIB="$(/bin/pkginfo -r SUNWuto)/SUNWut/lib"
UTSBIN="$(/bin/pkginfo -r SUNWuto)/SUNWut/sbin"

DEF_NET_MASK="255.255.255.0";
DEF_NET_PFX="192.168";
DEF_START_NET="128";
DEF_START_NUM=16;
DEF_END_NUM=240;
#DEF_END_NUM=20;
#echo "Reset DEF_END_NUM"

LEASE_DAYS=1;
LOG_KERN=4;
LOG_NET=4;
LOG_USB=4;
LOG_VID=4;
LOG_APPL=4;
AUTH_PORT=7009;

#
# This variable is used as a flag during dhcp configuration.
# If it is "true" then it means that prior to S8U5 there
# was no mechanism of using dhcpconfig non-interactively.
# So we manipulate the /etc/default/dhcp file directly.
# If it "false" then we know that this is a post S8U5
# machine and so we have a mechanism for non-interactive
# dhcpconfig. In this case we do not depend on the
# location of the dhcp files.
# 
USE_FILES=true

#
# names of files and directories that will be touched by this script
#
DHCP_DIR="/var/dhcp";
NSSWITCH="/etc/nsswitch.conf";
HOSTNAME_R="/etc/hostname";	# with suffix .{hme,qfe,vge}
HOSTS="/etc/inet/hosts";
NETMASKS="/etc/inet/netmasks";
NETWORKS="/etc/inet/networks";
INIT_DHCP="/etc/init.d/dhcp";
INIT_DTLOGIN="/etc/init.d/dtlogin";
INIT_SYSSUSPEND="/etc/default/sys-suspend";
INIT_SESSIONETC="/usr/dt/config/sessionetc"
INIT_OPENWINSYS="/usr/openwin/lib/openwin-sys"
CORONA_NAME="SunRay";
CORONA_TITLE="Sun Ray";
OFFLINE_FILE="/var/opt/SUNWut/offline"

#
# Variables that will be used ONLY if USE_FILES=true
#
DHCPTAB="${DHCP_DIR}/dhcptab";
DHCP_CONFIG="/etc/default/dhcp";

ME=$0
ARGS=$*

function DetermineRelease
{
    if [ -d /usr/lib/inet/dhcp ]; then
	USE_FILES=false
    fi
}

#
# Usage message
#
function Usage {

rm /tmp/newtifs.$$ 2>/dev/null
print "
Usage: $ME [-c] [-r] [-p] [-f] [-n] [-a <intf_name>] [-d <intf_name>] ...
  Switches:
    -c			# create framework for ${CORONA_TITLE} interconnect
    -r			# remove all ${CORONA_TITLE} interconnects
    -a <intf_name>	# add <intf_name> as ${CORONA_TITLE} interconnect
    -d <intf_name>	# delete <intf_name> as ${CORONA_TITLE} interconnect
    -p			# print current configuration
    -f			# take server offline
    -n			# bring server online

  Parameters:
    <intf_name>:	# a network interface name, such as
			# hme[0-9], qfe[0-9], or vge[0-9]
"

exit 2
}


#
# prompt user for yes or no answer
#  usage:  YesOrNo <prompt> <default value>
# takes two arguments, a PROMPT and the default value (Y | N).
# Returns 0 if the user specified "Y", nonzero otherwise.
#
function YesOrNo {
  if [ ${#} -ne 2 ]; then
    return 1;
  fi

  if [ "${2}" = "Y" ]; then
    DEFPMPT="([Y]/N): \c "
    DEFVAL="Y"
  else
    DEFPMPT="(Y/[N]):\c "
    DEFVAL="N"
  fi

  while true
  do
    print "${1} ${DEFPMPT}";
    read ANS
    : ${ANS:="${DEFVAL}"}
    if [ "${ANS}" = "Y" -o "${ANS}" = "y" ]; then
      return 0;
    elif [ "${ANS}" = "N" -o "${ANS}" = "n" ]; then
      return 1;
    fi
  done
}


#
# Verify a dotted IP address
#  usage:  CheckIPA <ipaddr>
# returns a 0 if a good ipa is given, and a 1 otherwise
#
function CheckIPA {
  IP_ADDR=`expr ${1} : '^\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\)$'`
  if [ ! "${IP_ADDR}" ]; then
    return 1
  fi
  NNO1=${1%%.*}
  tmp=${1#*.}
  NNO2=${tmp%%.*}
  tmp=${tmp#*.}
  NNO3=${tmp%%.*}
  tmp=${tmp#*.}
  NNO4=${tmp%%.*}
  if [ ${NNO1} -gt 255 -o ${NNO2} -gt 255 -o ${NNO3} -gt 255 -o ${NNO4} -gt 255 ]; then
    return 1
  fi
  return 0
}

# Translate a host IP address to its network address
function IP2Net {
  NET=`print "$1 $2" | awk '{
			split($1, a, ".")
			split($2, m, ".")
			dot = ""
			out = ""
			for (i = 1; i <= 4; i++) {
				for (j = 1; j <= 256; j *= 2) {
					if ((m[i] + j) == 256) {
						out = out dot a[i] - a[i] % j
						break
					} else if ((m[i] + j) > 256) {
						out = "BadMask"
						i = 5
						break
					}
				}
				dot = "."
			}
			printf "%s\n", out
		}'`
  if [ $NET = "BadMask" ]; then
    return 1;
  fi
  return 0;
}

# Get undotted host number from IP address
function IP2Host {
  H=`print "$1 $2" | awk '{
			split($1, a, ".")
			split($2, m, ".")
			dot = ""
			out = 0
			for (i = 1; i <= 4; i++) {
				out *= 256
				for (j = 1; j <= 256; j *= 2) {
					if ((m[i] + j) == 256) {
						out += a[i] % j
						break
					} else if ((m[i] + j) > 256) {
						print "BadMask"
						exit
					}
				}
			}
			printf "%d\n", out
		}'`
  if [ $H = "BadMask" ]; then
    return 1;
  fi
  return 0;
}

# Add dotted network and undotted host number together
function NetPlusHost {
  IP=`print "$1 $2" | awk '{
			split($1, a, ".")
			dot = ""
			out = ""
			b = $2
			for (i = 4; i >= 1; i--) {
				if (a[i] > 0 && b > a[i]) {
					print "BadAdd\n"
					exit
				}
				c =  (a[i] + b) % 256
				b -= (b % 256)
				b /= 256
				out = c dot out
				dot = "."
			}
			printf "%s\n", out
		}'`
  if [ $IP = "BadAdd" ]; then
    print -u2 "Host $2 overflows $2 subnet";
    return 1;
  fi
  return 0;
}

# Return the maximum host number for a given netmask
function MaxHostNum {
  H=`print "$1" | awk '{
			split($1, m, ".")
			out = 0
			for (i = 1; i <= 4; i++) {
				out *= 256
				for (j = 1; j <= 256; j *= 2) {
					if ((m[i] + j) == 256) {
						out += j - 1
						break
					} else if ((m[i] + j) > 256) {
						print "BadMask"
						exit
					}
				}
			}
			printf "%d\n", out
		}'`
  if [ $H = "BadMask" ]; then
    return 1;
  fi
  return 0;
}

#
# modify nsswitch file 
#  usage:  UpdateNsswitch <path>
# force hosts, netmasks, and networks to come from local files
#
function UpdateNsswitch {
  rm -f /tmp/tmpfile.$$;
  awk 'BEGIN { }
		/^hosts:/ { print "# SUNRAY DEL " $0;
		  	    printf "hosts:\tfiles ";
			    for (i = 2; i <= NF; i++) {
			      if ($i != "files") {
				printf "%s ", $i;
			      }
			    }
			    print "# SUNRAY ADD";
			    break;
		}
		/^netmasks:/ {  print "# SUNRAY DEL " $0;
		  		printf "netmasks:\tfiles ";
				for (i = 2; i <= NF; i++) {
				  if ($i != "files") {
				    printf "%s ", $i;
				  }
				}
				print "# SUNRAY ADD";
				break;
		}
		/^networks:/ {  print "# SUNRAY DEL " $0;
		  		printf "networks:\tfiles ";
				for (i = 2; i <= NF; i++) {
				  if ($i != "files") {
				    printf "%s ", $i;
				  }
				}
				print "# SUNRAY ADD";
				break;
		}
		/^services:/ {  print "# SUNRAY DEL " $0;
		  		printf "services:\tfiles ";
				for (i = 2; i <= NF; i++) {
				  if ($i != "files") {
				    printf "%s ", $i;
				  }
				}
				print "# SUNRAY ADD";
				break;
		}
		{ print $0 }
		END { }' $1 > /tmp/tmpfile.$$;

  mv -f /tmp/tmpfile.$$ $1;
}


#
# restore a file to it's non-newt state (handle both NEWT and SUNRAY)
#
function RestoreFile {
  rm -f /tmp/tmpfile.$$;
  nawk 'BEGIN { omit = 0 }
		/^# SUNRAY DEL / {
		  sub("# SUNRAY DEL  *", "", $0)
		  print $0
		  break;
		};
		/^# NEWT DEL / {
		  sub("# NEWT DEL  *", "", $0)
		  print $0
		  break;
		};
		/# SUNRAY ADD/ {
		  break;
		};
		/# NEWT ADD/ {
		  break;
		};
		/^# SUNRAY BEGIN/ { omit = 1 };
		/^# NEWT BEGIN/ { omit = 1 };
		(omit == 0) { print $0 };
		/^# SUNRAY END/ { omit = 0 };
		/^# NEWT END/ { omit = 0 };
		END { }' $1 > /tmp/tmpfile.$$;
  cp -f /tmp/tmpfile.$$ $1;
  rm -f /tmp/tmpfile.$$;
}


#
# lock a file in preparation for updating it
#  usage:  StartFileUpdate <path>
#  first tries to lock the file and if it succeeds, it makes a copy
# of the given file, which is to be used to modify the file.
#  the EndFileUpdate function is called after changes have been made
# to the file and it is ready to be put back in operation.
#
function StartFileUpdate {
  # try to lock the file to be changed

  # generate a tmp copy of the new file
  cp -f $1 $1.$$
  if [ ${?} -ne 0 ]; then
    print "Error:  unable to make tmp file" >&2;
    exit 1;
  fi
  chmod 600 $1.$$;
}


#
# end the update of a file
#  usage:  EndFileUpdate <path>
#  this is called after StartFileUpdate is called and atomically
# updates the given file and unlocks it
#
function EndFileUpdate {
  # save a copy of the original file
  rm -f $1.bak
  cp -p -f $1 $1.bak
  if [ ${?} -ne 0 ]; then
    print "Error:  unable to make backup file \"$1.bak\"" >&2;
    exit 1;
  fi

  # Copy over the original, preserving ownership, links, and permissions
  cp -f $1.$$ $1
  rm -f $1.$$
}

#
# Verify that dhcp is installed
#
function CheckDHCP {
  if [ ! -x "${INIT_DHCP}" ]; then
    print "Error:  DHCP cannot be run on this machine." >&2;
    print "        Ensure DHCP is installed, /etc/init.d/dhcp has the" >&2;
    print "        correct permissions and run \"utadm\" again." >&2;
    exit 1;
  fi
}

function GetDHCPpid {
  dhcppid=`ps -e |
	grep in.dhcpd |
	sed -e 's/^  *//' -e 's/ .*//'`
}

#
# Stop current dhcp daemon
#  usage:  StopDHCP
#
function StopDHCP {
  "${INIT_DHCP}" stop
  print "### stopped DHCP daemon";
}

#
# Start current dhcp daemon
#  usage:  StartDHCP
#
function StartDHCP {
  # Do this because DHCP doesn't seem to start reliably
  GetDHCPpid
  while [ -z "${dhcppid}" ]; do
    "${INIT_DHCP}" start
    GetDHCPpid
  done
  print "### started DHCP daemon";
}


#
# Setup the DHCP configuration file
#  usage:  SetupCfgFile
# write a new dhcp config file for Corona use.
#
function SetupCfgFile {
  #
  # If this is a post S8U5 then use the non-interactive
  # dhcpconfig command to setup dhcp configuration file
  # in the correct location
  #
  if ! $USE_FILES ; then
    dhtadm -P >/dev/null 2>&1
    if [ ${?} -ne 0 ]; then
	dhcpconfig -D -r SUNWfiles -p ${DHCP_DIR} >/dev/null 2>&1
    fi
    return 0
  fi

  #
  # This is a pre-S8U5 system. So we use the file location
  #
  # check if the config file exists
  if [ -f ${DHCP_CONFIG} ]; then
    TMPDIR=`sed -n 's/^PATH=//p' ${DHCP_CONFIG}`
    if [ ${TMPDIR} != ${DHCP_DIR} ]; then
      printf "Warning: DHCP_DIR changed to ${TMPDIR}" >&2;
      DHCP_DIR=${TMPDIR};
      DHCPTAB="${DHCP_DIR}/dhcptab";
    fi
    return 0;
  fi

  # create a new ${DHCP_CONFIG} file that forces files and path for Corona
  print "# This file controls the defaults for datastore type and location.\n# for the DHCP service. Two directives are currently supported,\n# 'RESOURCE' and 'PATH'. 'RESOURCE' can be either 'files' or 'nisplus'.\n# 'PATH' can be a unix pathname for 'files' resources, or a legal\n# nisplus directory for 'nisplus' resources." > ${DHCP_CONFIG};
  print "RESOURCE=files" >> ${DHCP_CONFIG};
  print "PATH=${DHCP_DIR}" >> ${DHCP_CONFIG};
}


#
# Set options in the dhcp startup file for the dhcp service.
#  usage:  SetupOptions <interfaces>
# Handle DHCP offer TTL, rescan interval, and BOOTP compatibility.
#
function SetupOptions {
  # cache extended offers for 10 seconds
  OPTIONS="-o 10";

  # modify the file
  StartFileUpdate ${INIT_DHCP};
  RestoreFile "${INIT_DHCP}.$$";
  sed -e "/^case \"\$1\" in\$/{
i\\
ulimit -n 1024 # SUNRAY ADD
}" "${INIT_DHCP}.$$" > /tmp/tmpfile.$$;

  # don't use EndFileUpdate so links aren't broken
  cp -f /tmp/tmpfile.$$ "${INIT_DHCP}";
  rm -f /tmp/tmpfile.$$ "${INIT_DHCP}.$$";
}

function NohupDtlogin {
  StartFileUpdate ${INIT_DTLOGIN};
  RestoreFile "${INIT_DTLOGIN}.$$";
  sed -e "/^[ 	]*.usr.dt.bin.dtlogin/{
s/^/# SUNRAY DEL /
a\\
		nohup /usr/dt/bin/dtlogin -daemon & # SUNRAY ADD
}" "${INIT_DTLOGIN}.$$" > /tmp/tmpfile.$$;

  # don't use EndFileUpdate so links aren't broken
  cp -f /tmp/tmpfile.$$ "${INIT_DTLOGIN}";
  rm -f /tmp/tmpfile.$$ "${INIT_DTLOGIN}.$$";
}

function FixSysSuspend {
  StartFileUpdate ${INIT_SYSSUSPEND};
  RestoreFile "${INIT_SYSSUSPEND}.$$";
  sed -e "/^[ 	]*PERMS/{
s/^/# SUNRAY DEL /
a\\
# SUNRAY BEGIN
a\\
PERMS=-
a\\
# SUNRAY END
}" "${INIT_SYSSUSPEND}.$$" > /tmp/tmpfile.$$;

  mv -f /tmp/tmpfile.$$ "${INIT_SYSSUSPEND}.$$";
  EndFileUpdate ${INIT_SYSSUSPEND};
}

#
# Change sessionetc to skip starting sdtvolcheck for corona sessions
#
function FixSessionEtc {
  StartFileUpdate ${INIT_SESSIONETC};
  RestoreFile "${INIT_SESSIONETC}.$$";
  sed -e '/^if \[ "$DTXSERVERLOCATION" != "remote" \]$/{
s/^/# SUNRAY DEL /
a\
UTDPY=${DISPLAY#*:} # SUNRAY ADD
a\
UTDPY=${UTDPY%.*} # SUNRAY ADD
a\
if [ "$DTXSERVERLOCATION" != "remote" -a "$UTDPY" = "0" ] # SUNRAY ADD
}' "${INIT_SESSIONETC}.$$" > /tmp/tmpfile.$$;

  mv -f /tmp/tmpfile.$$ "${INIT_SESSIONETC}.$$";
  EndFileUpdate ${INIT_SESSIONETC};
  #
  # Make up for past transgressions.
  #
  chmod a+x ${INIT_SESSIONETC}
}

#
# Change openwin-sys to skip starting console speckeysd for corona sessions
#
function FixOpenwinSys {
  StartFileUpdate ${INIT_OPENWINSYS};
  RestoreFile "${INIT_OPENWINSYS}.$$";
  sed -e '/^if \[ -x $OPENWINHOME\/bin\/speckeysd \] ; then$/{
s/^/# SUNRAY DEL /
a\
# SUNRAY BEGIN
a\
utconsole=f
a\
case "$DISPLAY" in
a\
 *:0|*:0.0) utconsole=t ;;
a\
esac
a\
if [ -x $OPENWINHOME/bin/speckeysd -a "$utconsole" = "t" ] ; then
a\
# SUNRAY END
}' "${INIT_OPENWINSYS}.$$" > /tmp/tmpfile.$$;

  mv -f /tmp/tmpfile.$$ "${INIT_OPENWINSYS}.$$";
  EndFileUpdate ${INIT_OPENWINSYS};
  #
  # Make up for past transgressions.
  #
  chmod a+x ${INIT_OPENWINSYS}
}

#
# Get the UTC offset in minutes
#  usage:  GetTimeOffset
# calculates the offset in minutes from UTC and puts it into UTC_OFFSET
#
function GetTimeOffset {
  LOCAL=`date '+%H'`;
  GMT=`date -u '+%H'`;
  if [ ${LOCAL} -gt ${GMT} ]; then
    let TIME=24+${GMT}-${LOCAL};
  else
    let TIME=${GMT}-${LOCAL};
  fi
  let TIME=${TIME}\*3600;
  UTC_OFFSET="${TIME}";
}

function getNetMask {
  print "$1" | awk '{
      split($0, a);
      for (i = 0; i < NF; i++) {
	if (a[i] == "netmask") {
	  n = length(a[i + 1]);
	  m = "";
	  for (j = 1; j < n; j += 2) {
	    d = substr(a[i + 1], j, 1);
	    y = (index("0123456789abcdef", d) - 1) * 16;
	    d = substr(a[i + 1], j + 1, 1);
	    y += (index("0123456789abcdef", d) - 1);
	    if (m == "")
	      m = y;
	    else
	      m = m"."y;
          }
	  print m;
	  break;
        }
      }
    }'
}

# Replaced by IP2Net
function getNetAddr {
  # make new network address
  J=0;
  NET="";
  while [ ${J} -lt 4 ]; do
    IPA=`print $1 | cut -d "." -f$((J + 1))`;
    MSK=`print $2 | cut -d "." -f$((J + 1))`;
    NET="${NET}$((${IPA} & ${MSK}))";
    let "J = J + 1";
    if [ ${J} -lt 4 ]; then
	NET="${NET}.";
    fi
    if [ ${MSK} -ne 0 ]; then
	NN=${IPA};
    fi
  done
}

#
# get information about a given network interface
#  usage:  GetInterfaceInfo <intf name>
# get the following info into the following variables:
#  INTF_IPA:     the IPA of the interface
#  INTF_NAME:    the symbolic name for this interface
#  INTF_MASK:    the subnet mask for this interface
#  INTF_NET:     the address of this network
#  INTF_NETPFX:  the subnet prefix of this network
#  INTF_NETNAME: the symbolic name of this network
#  INTF_MTU:     the MTU for this interface
#  INTF_BC:      the broadcast address for this network
#  INTF_ROUTER:  the address of the router for this network
#
#  this is only to be called after the interface has been setup (i.e., after
# the "ifconfig plumb" has been done), and the /etc/hosts table has been setup.
#  returns a 0 if all values are set properly and a 1 otherwise
#
function GetInterfaceInfo {
  # get the intf's mtu, network address, and IP address
  INTF_INFO=`netstat -n -I ${1}`;
  if [ -z "${INTF_INFO}" ]; then
    return 1;
  fi
  INTF_MTU=`print "${INTF_INFO}" | awk 'NR == 2 { print $2 }'`;
  if [ -z "${INTF_MTU}" ]; then
    return 1;
  fi
  INTF_NET=`print "${INTF_INFO}" | awk 'NR == 2 { print $3 }'`;
  if [ -z "${INTF_NET}" ]; then
    return 1;
  fi
  INTF_IPA=`print "${INTF_INFO}" | awk 'NR == 2 { print $4 }'`;
  if [ -z "${INTF_IPA}" ]; then
    return 1;
  fi

  # get the intf's symbolic name
  INTF_NAME=`getent hosts ${INTF_IPA} | awk '{print $2}'`;
  if [ -z "${INTF_NAME}" ]; then
    print "Error: host name for ${INTF_IPA} not found" >&2;
    exit 1;
  fi

  # get the intf's netmask and broadcast address
  INTF_INFO=`ifconfig ${1}`;
  if [ ${?} -ne 0 ] || [ -z "${INTF_INFO}" ]; then
    return 1;
  fi
  INTF_MASK=`getNetMask "${INTF_INFO}"`
  INTF_BC=`print "${INTF_INFO}" | awk 'BEGIN { }
    {
      split($0, a);
      for (i = 0; i < NF; i++) {
	if (a[i] == "broadcast") {
	  print a[i + 1];
	}
      }
    }
    END { }'`;

  # get the intf's subnet prefix
  INTF_NETPFX=`print "${INTF_NET}" | sed -e 's/\.0//g'`;

  # get the net's symbolic name
  INTF_NETNAME=${CORONA_NAME}-${1}
  if [ -z "${INTF_NETNAME}" ]; then
    print "Error: net name for ${INTF_NET} not found" >&2;
    exit 1;
  fi

  # get the intf's router address
  INTF_INFO=`netstat -n -r | grep "^${INTF_NET}" | awk '{ print $2; exit }'`
  if [ -z "${INTF_INFO}" ]; then
    return 1;
  fi
  INTF_ROUTER="${INTF_INFO}";
  
#####print "${INTF_IPA}, ${INTF_NAME}, ${INTF_MASK}, ${INTF_NET}, ${INTF_MTU}, ${INTF_BC}, ${INTF_ROUTER}";
  return 0;
}


#
# make the dhcptab file
#  usage:  SetupDhcptab <interfaces>
# create the table and add all the macros
#
function SetupDhcptab {
  if $USE_FILES ; then
    # check if the dhcp dir exists in the proper place
    if [ ! -d ${DHCP_DIR} ]; then
	#
	# In the case of a freshly installed pre S8U5 machine
	# DHCP_DIR will not be present.
	# dhcp dir doesnt exist, so make it
	#
	mkdir -m 655 -p ${DHCP_DIR};
	if [ ${?} -ne 0 ]; then
	print "Error:  unable to create dhcp dir \"${DHCP_DIR}\".  check and retry" >&2;
	return 1;
	fi
    fi
  fi

  #
  # create a new dhcptab if there isn't one
  #  
  dhtadm -C >/dev/null 2>&1
  dhtadm -P >/dev/null 2>&1
  if [ ${?} -ne 0 ]; then
    # there was a failure, so complain and bail
    print "Error:  unable to create dhcptab.  check and retry" >&2;
    return 1;
  fi

  # add the Corona-specific macro, included by all interfaces
  # (includes lease time and negotiate enable)
  let LEASE=${LEASE_DAYS}\*86400;
  CORONA_MACRO=":LeaseTim=${LEASE}:LeaseNeg:";
  dhtadm -D -m "${CORONA_NAME}" 2> /dev/null
  dhtadm -A -m "${CORONA_NAME}" -d "${CORONA_MACRO}" 2> /tmp/Err.$$;

  if [ ${?} -ne 0 ]; then
    print "Error:  cannot add server macro \"${CORONA_NAME}\" to dhcptab:\n    `cat /tmp/Err.$$`" >&2;
    rm -f /tmp/Err.$$
    return 1;
  else
    rm -f /tmp/Err.$$
  fi

  # add all the symbols
  # remove them first
  for i in `dhtadm -P | awk '/SUNW.NewT.SUNW/ { print $1 }'`; do
    dhtadm -D -s $i 2> /dev/null
  done

  dhtadm -A -s AuthSrvr -d 'Vendor=SUNW.NewT.SUNW,21,IP,1,1' 2> /tmp/Err.$$;
  if [ ${?} -ne 0 ]; then
    print "Error:  cannot add symbol \"AuthSrvr\" to dhcptab:\n    `cat /tmp/Err.$$`" >&2;
    rm -f /tmp/Err.$$;
    return 1;
  else
    rm -f /tmp/Err.$$;
  fi

  dhtadm -A -s AuthPort -d 'Vendor=SUNW.NewT.SUNW,22,NUMBER,2,1' 2> /tmp/Err.$$;
  if [ ${?} -ne 0 ]; then
    print "Error:  cannot add symbol \"AuthPort\" to dhcptab:\n    `cat /tmp/Err.$$`" >&2;
    rm -f /tmp/Err.$$;
    return 1;
  else
    rm -f /tmp/Err.$$;
  fi

  dhtadm -A -s NewTVer -d 'Vendor=SUNW.NewT.SUNW,23,ASCII,1,0' 2> /tmp/Err.$$;
  if [ ${?} -ne 0 ]; then
    print "Error:  cannot add symbol \"NewTVer\" to dhcptab:\n    `cat /tmp/Err.$$`" >&2;
    rm -f /tmp/Err.$$;
    return 1;
  else
    rm -f /tmp/Err.$$;
  fi

  dhtadm -A -s LogHost -d 'Vendor=SUNW.NewT.SUNW,24,IP,1,1' 2> /tmp/Err.$$;
  if [ ${?} -ne 0 ]; then
    print "Error:  cannot add symbol \"LogHost\" to dhcptab:\n    `cat /tmp/Err.$$`" >&2;
    rm -f /tmp/Err.$$;
    return 1;
  else
    rm -f /tmp/Err.$$;
  fi

  dhtadm -A -s LogKern -d 'Vendor=SUNW.NewT.SUNW,25,NUMBER,1,1' 2> /tmp/Err.$$;
  if [ ${?} -ne 0 ]; then
    print "Error:  cannot add symbol \"LogKern\" to dhcptab:\n    `cat /tmp/Err.$$`" >&2;
    rm -f /tmp/Err.$$;
    return 1;
  else
    rm -f /tmp/Err.$$;
  fi

  dhtadm -A -s LogNet -d 'Vendor=SUNW.NewT.SUNW,26,NUMBER,1,1' 2> /tmp/Err.$$;
  if [ ${?} -ne 0 ]; then
    print "Error:  cannot add symbol \"LogNet\" to dhcptab:\n    `cat /tmp/Err.$$`" >&2;
    rm -f /tmp/Err.$$;
    return 1;
  else
    rm -f /tmp/Err.$$;
  fi

  dhtadm -A -s LogUSB -d 'Vendor=SUNW.NewT.SUNW,27,NUMBER,1,1' 2> /tmp/Err.$$;
  if [ ${?} -ne 0 ]; then
    print "Error:  cannot add symbol \"LogUSB\" to dhcptab:\n    `cat /tmp/Err.$$`" >&2;
    rm -f /tmp/Err.$$;
    return 1;
  else
    rm -f /tmp/Err.$$;
  fi

  dhtadm -A -s LogVid -d 'Vendor=SUNW.NewT.SUNW,28,NUMBER,1,1' 2> /tmp/Err.$$;
  if [ ${?} -ne 0 ]; then
    print "Error:  cannot add symbol \"LogVid\" to dhcptab:\n    `cat /tmp/Err.$$`" >&2;
    rm -f /tmp/Err.$$;
    return 1;
  else
    rm -f /tmp/Err.$$;
  fi

  dhtadm -A -s LogAppl -d 'Vendor=SUNW.NewT.SUNW,29,NUMBER,1,1' 2> /tmp/Err.$$;
  if [ ${?} -ne 0 ]; then
    print "Error:  cannot add symbol \"LogAppl\" to dhcptab:\n    `cat /tmp/Err.$$`" >&2;
    rm -f /tmp/Err.$$;
    return 1;
  else
    rm -f /tmp/Err.$$;
  fi

  dhtadm -A -s FWSrvr -d 'Vendor=SUNW.NewT.SUNW,31,IP,1,1' 2> /tmp/Err.$$;
  if [ ${?} -ne 0 ]; then
    print "Error:  cannot add symbol \"FWSrvr\" to dhcptab:\n    `cat /tmp/Err.$$`" >&2;
    rm -f /tmp/Err.$$;
    return 1;
  else
    rm -f /tmp/Err.$$;
  fi

  dhtadm -A -s Intf -d 'Vendor=SUNW.NewT.SUNW,33,ASCII,1,0' 2> /tmp/Err.$$;
  if [ ${?} -ne 0 ]; then
    print "Error:  cannot add symbol \"Intf\" to dhcptab:\n    `cat /tmp/Err.$$`" >&2;
    rm -f /tmp/Err.$$;
    return 1;
  else
    rm -f /tmp/Err.$$;
  fi

  return 0;
}

function AddIntfs {
  # add per interface macros
  I=0;
  for INTF in ${1}; do
    if [ ${START_NUM[$I]} -eq 0 ]; then
      let "I = I + 1";
      continue
    fi
    # get all the info for this interface
    GetInterfaceInfo "${INTF}";
    if [ ${?} -ne 0 ]; then
      print "Error:  unable to get information on interface \"${INTF}\"" >&2;
      return 1;
    fi

    # add server macro for each interface
    # (includes the corona macro and contains auth server ipa and port,
    #  loghost, logkern/net/usb/vid/appl)
    AUTH_SRVR="${INTF_IPA}";
    SRV_MACRO=":Include=${CORONA_NAME}:AuthSrvr=${AUTH_SRVR}:AuthPort=${AUTH_PORT}:LogHost=${INTF_IPA}:LogKern=${LOG_KERN}:LogNet=${LOG_NET}:LogUSB=${LOG_USB}:LogVid=${LOG_VID}:LogAppl=${LOG_APPL}:";
    dhtadm -A -m ${INTF_NETNAME} -d "${SRV_MACRO}" 2> /tmp/Err.$$;
    if [ ${?} -ne 0 ]; then
      print "Error:  cannot add interface macro \"${INTF_NETNAME}\" to dhcptab:\n    `cat /tmp/Err.$$`" >&2;
      rm -f /tmp/Err.$$;
      return 1;
    else
      rm -f /tmp/Err.$$;
    fi

    # add a network macro for each interface
    # (contains broadcast ipa, subnet mask, mtu, router ipa)
    if [ "${MULTINET[${I}]}" = "N" ]; then
      NET_NAME=`print ${INTF_NET} | sed -e 's/\./_/g'`;
      if [ -n "${ROUTER[${I}]}" ]; then
	INTF_ROUTER=${ROUTER[${I}]}
      fi
      NET_MACRO=":Broadcst=${INTF_BC}:Subnet=${INTF_MASK}:MTU=${INTF_MTU}:Router=${INTF_ROUTER}:FWSrvr=${FWSRVR[$I]}:Intf=${INTF}:";
      dhtadm -A -m ${INTF_NET} -d "${NET_MACRO}" 2> /tmp/Err.$$;
      if [ ${?} -ne 0 ]; then
        print "Error:  cannot add network macro \"${INTF_NET}\" to dhcptab:\n    `cat /tmp/Err.$$`" >&2;
        rm -f /tmp/Err.$$;
        return 1;
      else
        rm -f /tmp/Err.$$;
      fi
    fi
    let "I = I + 1";
  done
}

#
# initialize various dhcp network tables.
#  usage:  BuildNetworkTables
#  builds the network tables for all of the newtnet interfaces in ${NEWT_INTFS}
#  returns 0 on success, and 1 on failure
#
function BuildNetworkTables {
  I=0;
  for INTF in ${1}; do
    if [ ${START_NUM[$I]} -eq 0 ]; then
      let "I = I + 1";
      continue
    fi
    # get info about the interface and make a DHCP network table for it
    GetInterfaceInfo "${INTF}";
    NET_FILE=`print ${INTF_NET} | sed -e 's/\./_/g'`;

    if [ "${MULTINET[${I}]}" = "N" ]; then      
      pntadm -C ${INTF_NET} 2> /tmp/Err.$$
      if [ ${?} -ne 0 ]; then
        print "Error:  cannot create DHCP network table for \"${INTF_NET}\":\n    `cat /tmp/Err.$$`" >&2;
        rm -f /tmp/Err.$$;
        return 1;
      else
        rm -f /tmp/Err.$$;
      fi
    fi

    # add entries to the dhcp network table
    NUM=${START_NUM[${I}]};
    INPOOL=0;
    while [ ${NUM} -le ${END_NUM[${I}]} ]; do
      NetPlusHost ${NETADDR[${I}]} ${NUM}
      ADDR=$IP
      if [ ${IPADDR[${I}]} = ${ADDR} ]; then
	print "### Skipping host address ${IPADDR[${I}]} in DHCP address pool";
        let NUM=${NUM}+1;
	continue;
      fi
      if [ ${FWSRVR[${I}]} = ${ADDR} ]; then
	print "### Skipping firmware server address ${FWSRVR[${I}]} in DHCP address pool";
        let NUM=${NUM}+1;
	continue;
      fi
      let INPOOL=${INPOOL}+1;
      pntadm -A ${ADDR} -m ${INTF_NETNAME} -s ${HOSTNAME} ${INTF_NET} 2> /tmp/Err.$$;
      if [ ${?} -ne 0 ]; then
	print "Error:  problem in adding entry \"${ADDR}\" to DHCP network table for \"${INTF_NET}\":\n    `cat /tmp/Err.$$`" >&2;
	rm -f /tmp/Err.$$;
	return 1;
      else
	rm -f /tmp/Err.$$;
      fi

      let NUM=${NUM}+1;
    done
    if [ ${INPOOL} -eq 0 ]; then
      print -u2 "Warning: DHCP pool for network ${INTF_NET} is empty";
    fi
    let "I = I + 1";
  done

  return 0;
}

function addNetwork {
  while read name ip rest
  do
    if [ "${name}" = $1 -a "${ip}" = $2 ]; then
      return
    fi
  done <${NETWORKS}

  StartFileUpdate "${NETWORKS}";
  # remove anything that matches net prefix from ${NETWORKS} and add new line
  rm -f /tmp/tmpfile.$$;
  sed -e "/^${1}/s/^/# SUNRAY DEL /" "${NETWORKS}.$$" > /tmp/tmpfile.$$;
  print "${1}\t${2}\t${CORONA_NAME}\t# SUNRAY ADD - DO NOT MODIFY" \
	>>/tmp/tmpfile.$$;
  mv -f /tmp/tmpfile.$$ "${NETWORKS}.$$";
  EndFileUpdate "${NETWORKS}";
}

# add netmask entry if needed
function addNetMask {
  while read nw nm rest
  do
    if [ "$nw" = $1 -a "$nm" = $2 ]; then
	return
    fi
  done <${NETMASKS}

  # update the /etc/netmasks file
  StartFileUpdate "${NETMASKS}";

  # remove anything that matches the net address and add new line
  rm -f /tmp/tmpfile.$$;
  sed -e "/^[ 	#]*${1}[ 	]/s/^/# SUNRAY DEL /" "${NETMASKS}.$$" \
	>/tmp/tmpfile.$$;
  print "${1}\t${2} # SUNRAY ADD - DO NOT MODIFY" >> /tmp/tmpfile.$$;
  mv -f /tmp/tmpfile.$$ "${NETMASKS}.$$";

  EndFileUpdate "${NETMASKS}";
}

# remove netmask entry if needed
function rmvNetMask {
  while read name ip rest
  do
    if [ "$ip" = $1 ]; then
      return
    fi
  done <${NETWORKS}
      
  sed "/^${1}[ 	].*# SUNRAY ADD/d" ${NETMASKS} >${NETMASKS}.$$
  mv ${NETMASKS}.$$ ${NETMASKS}
}

# get a list of existing Corona interfaces
function GetCurrentCfg {
  sed -n "s/${CORONA_NAME}-\([^ 	]*\)[ 	].*/\1/p" ${NETWORKS} \
	>/tmp/newtifs.$$
  dhtadm -P 2>/dev/null | sed -n "s/^${CORONA_NAME}-\([^ 	]*\).*/\1/p" \
	>/tmp/dhcpifs.$$
  sort -u /tmp/newtifs.$$ /tmp/dhcpifs.$$ >/tmp/allifs.$$
}

# fix up network names to put interface name into the network name
function FixNetworks {
  dhcpStopped=false
  grep 'SUNRAY ADD' ${NETWORKS} | \
  while read name ip rest
  do
    INTF=`print $name | sed -n "s/^${CORONA_NAME}-//p"`
    if [ -n "${INTF}" -a -f "${HOSTNAME_R}.${INTF}" ]; then
      continue
    fi

    if ! $dhcpStopped ; then
	StopDHCP
	dhcpStopped=true
    fi
    intf=`dhtadm -P 2>/dev/null | sed -n "/^${ip}/s/.*:Intf=\"*\([^:\"]*\)[:\"].*/\1/p"`

    if [ $name != ${CORONA_NAME}-${intf} ]; then
	dhtadm -M -m $name -n ${CORONA_NAME}-${intf} 2>/dev/null
	sed -e "s/\<$name\>/${CORONA_NAME}-${intf}/g" ${NETWORKS} >${NETWORKS}.new
	cp ${NETWORKS}.new ${NETWORKS}
	rm ${NETWORKS}.new

	M=`getent netmasks $ip | awk '{print $2}'` 
	if [ -z $M ]; then
	    print -u2 "Error: netmask for host $1 does not exist";
	    continue;
	fi
	IP2Net $ip $M
	if [ ${?} -ne 0 ]; then
	    continue;
	fi
	pntadm -P $NET 2>/dev/null|cut -f3 >/tmp/pntadm.op.$$
	while read clientIp
	do
	    pntadm -M $clientIp -m ${CORONA_NAME}-${intf} $NET 2>/dev/null
	done </tmp/pntadm.op.$$
    fi
  done

  if $dhcpStopped ; then
    StartDHCP
  fi
}

# check parameters in /etc/system
function CheckEtcSystem {
  PTCNT=`sed -n 's/^[ 	]*set[ 	][ 	]*pt_cnt[ 	]*=[ 	]*\([0-9]*\).*/\1/p' /etc/system`
  if [ -z "$PTCNT" -o "$PTCNT" -le 100 ]; then
    print ""
    print "There may not be enough ptys configured to support more"
    print "than a few ${CORONA_TITLE} users. To add more ptys, edit /etc/system and"
    print "add the line \"set pt_cnt=NPTYS\", where NPTYS is the expected"
    print "number of users times the average number of shell windows per"
    print "user. The system must be rebooted with the reconfiguration"
    print "option (-r) for the change to take effect."
    print ""
  fi
}


################################################################
#
# MAIN
#
################################################################

set -u
umask 022
trap "rm -f /tmp/*.$$; exit" 0 1 2 3 14 15

# check if we're root
if [ `id | cut -c5` -ne 0 ]
then
  print "Error:  must have super user (root) privileges" >&2;
  Usage;
fi

DetermineRelease;
CheckDHCP;

FixNetworks;
GetCurrentCfg;

# parse the cmd line switches
VERBOSE="N";
CREATE="N";
REMOVE="N";
PRINT="N";
OFFLINE="N";
INTF_ADD=""
INTF_DEL=""
DHCP_DEL=""
while getopts cra:d:pfn ARGNAME 2>/dev/null; do
  case $ARGNAME in
    c)	CREATE="Y";REMOVE="N";;
    a)  fgrep -x -- ${OPTARG} /tmp/allifs.$$ >/dev/null;
	if [ $? -eq 0 ]; then
	  print "Error: Interface \"${OPTARG}\" already configured; cannot add" >&2;
	  Usage;
	fi
	if [ ${OPTARG#-} != ${OPTARG} ]; then
          print "Error:  invalid interface name: \"${OPTARG}\"";
	  Usage;
        fi
	print "${INTF_ADD}" | grep -w -- ${OPTARG} >/dev/null;
	if [ $? -eq 0 ]; then
	  print "Error: Interface \"${OPTARG}\" already in add list" >&2;
	  Usage;
	fi

        # make sure interface name is valid
        STATUS=`ifconfig ${OPTARG} plumb 2>&1`
        if [ -n "$STATUS" ]; then
          print "Error:  invalid interface name: \"${OPTARG}\"";
	  Usage;
        fi

    	INTF_ADD="${INTF_ADD} ${OPTARG}";;
    d)	fgrep -x -- ${OPTARG} /tmp/allifs.$$ >/dev/null;
	if [ $? -ne 0 ]; then
	  printf "Error: Interface \"${OPTARG}\" is not configured; cannot delete" >&2;
	  Usage;
	fi
	if [ ${OPTARG#-} != ${OPTARG} ]; then
          print "Error:  invalid interface name: \"${OPTARG}\"";
	  Usage;
        fi
	print "${INTF_DEL}" | grep -w -- ${OPTARG} >/dev/null;
	if [ $? -eq 0 ]; then
	  print "Error: Interface \"${OPTARG}\" already in delete list" >&2;
	  Usage;
	fi
	grep -w "${OPTARG}" /tmp/newtifs.$$ >/dev/null
	if [ $? = 0 ]; then
	  INTF_DEL="${INTF_DEL} ${OPTARG}"
	fi
	grep -w "${OPTARG}" /tmp/dhcpifs.$$ >/dev/null
	if [ $? = 0 ]; then
	  DHCP_DEL="${DHCP_DEL} ${OPTARG}"
	fi;;
    r)	REMOVE="Y";CREATE="N";;
    p)  PRINT="Y";;
    f)	touch $OFFLINE_FILE;OFFLINE="Y";;
    n)	rm $OFFLINE_FILE 2>/dev/null;OFFLINE="Y";;
    ?)  Usage;;
  esac
done

if [ $OPTIND -le $# ]; then
  shift `expr $OPTIND - 1`
  print -u2 "Unrecognized arguments: $*";
  Usage;
fi

ISSETUP=`dhtadm -P 2>/dev/null | grep -w "${CORONA_NAME}"`
if [ ${PRINT} = "Y" ]; then
  if [ -z "${ISSETUP}" ]; then
    print "${CORONA_TITLE} interconnect framework is not configured";
    exit 1;
  fi
  if [ ! -s /tmp/allifs.$$ ]; then
    print "No ${CORONA_TITLE} interfaces configured";
    exit 1;
  fi
  print "Interface	Host		Network		Netmask		Assigned IPs";
  for INTF in `cat /tmp/allifs.$$`; do
    H=`cat ${HOSTNAME_R}.${INTF}`;
    IP=`getent hosts $H | awk '{print $1}'`
    M=`getent netmasks $IP | awk '{print $2}'` 
    if [ -z $M ]; then
      print -u2 "Error: netmask for host $1 does not exist";
      continue;
    fi
    IP2Net $IP $M
    if [ ${?} -ne 0 ]; then
      continue;
    fi
    today=`date +%\Y%m%d`
    C=`pntadm -P ${NET} 2>/dev/null | grep -w "${CORONA_NAME}-${INTF}" | \
	nawk -v today=$today 'BEGIN { count = 0; total = 0 }
		{
			split($5, d, "/")
			if (d[3] != "")
				day = d[3] * 10000 + d[1] * 100 + d[2]
			else
				day = 0
			if ($5 == "Forever" || today <= day)
				count++
			total++
		}
		END {
			print count, total
		}'`
    printf "%-10s\t%-10s\t%-10s\t%-10s\t%6s/%-6s\n" $INTF $H $NET $M $C;
  done
  exit 0;
fi

# get this server's name and ipaddr, use this as the Corona Central server
HOSTNAME=`hostname`;

if [ -z "${HOSTNAME}" ]; then
  print "Error: hostname must be set" >&2;
  print "Set host name and try again" >&2;
  exit 1;
fi

HOSTIP=`getent hosts ${HOSTNAME}`
if [ -z "${HOSTIP}" ]; then
  print "Error: host IP address must be set" >&2;
  print "Set host IP address in ${HOSTS} files and try again" >&2;
  exit 1;
fi

# force CREATE mode on if the Corona macro is not defined
if [ -z "${ISSETUP}" -a -n "${INTF_ADD}" ]; then
  CREATE="Y"
fi

if [ "${REMOVE}" = "Y" ]; then
  if [ -n "${INTF_ADD}" ]; then
    print "Error: cannot combine -r and -a options" >&2;
    Usage;
  fi
  INTF_DEL=`cat /tmp/newtifs.$$`
  DHCP_DEL=`cat /tmp/dhcpifs.$$`
fi
rm /tmp/*ifs.$$ 2>/dev/null

# sanity check the command line options
if [ -z "${INTF_ADD}" -a -z "${INTF_DEL}" -a -z "${DHCP_DEL}" -a \
    "${REMOVE}" = "N" -a "${CREATE}" = "N" -a "${OFFLINE}" = "N" ]; then
  print "Error:  must supply at least one of -a, -d, -c, -f, -n, or -r" >&2;
  Usage;
fi

# if create, remove any old newt stuff and update the /etc/nsswitch.conf file
if [ ${CREATE} = "Y" ]; then
  print "### Configuring ${NSSWITCH}";
  StartFileUpdate ${NSSWITCH};
  RestoreFile "${NSSWITCH}.$$";
  UpdateNsswitch "${NSSWITCH}.$$";
  EndFileUpdate "${NSSWITCH}";

  # check if the networks file exists
  if [ ! -f "${NETWORKS}" ]; then
    # doesnt exist, so make a default one
    print "#\n# The networks file associates Internet Protocol (IP) network numbers\n# with network names.  The format of this file is:\n# \n#       network-name    network-number  nicnames . . .\n#\n\n#\n# The loopback network is used only for intra-machine communication\n#\nloopback        127\n\n#\n# Internet networks\n#\narpanet         10              arpa    # Historical\n" > ${NETWORKS};
    chmod 744 "${NETWORKS}";
  fi

  # check if the netmasks file exists
  if [ ! -f "${NETMASKS}" ]; then
    # doesnt exist, so make a default one
    print "# The netmasks file associates Internet Protocol (IP) address\n# masks with IP network numbers.\n#\n#       network-number  netmask\n#\n# The term network-number refers to a number obtained from the Internet Network\n# Information Center.  Currently this number is restricted to being a class\n# A, B, or C network number.  In the future we should be able to support\n# arbitrary network numbers per the Classless Internet Domain Routing\n# guidelines.\n#\n# Both the network-number and the netmasks are specified in\n# \"decimal dot\" notation, e.g:\n#\n#               128.32.0.0 255.255.255.0\n#\n" > ${NETMASKS};
    chmod 744 "${NETMASKS}"
  fi

  print "### Configuring DHCP Service for ${CORONA_TITLE}";
  SetupCfgFile;
  SetupDhcptab;
  StopDHCP;
  SetupOptions;
# Remove this because the reason for it should be fixed in dtlogin
#  NohupDtlogin;
  if [ -f "${INIT_SYSSUSPEND}" ]; then
    FixSysSuspend;
  fi
  FixSessionEtc;
  FixOpenwinSys;
  StartDHCP;

  if [ ${?} -ne 0 ]; then
    exit 1;
  fi
  ln ${INIT_DHCP} /etc/rcS.d/K35utdhcp 2>/dev/null
  ln ${INIT_DHCP} /etc/rc0.d/K35utdhcp 2>/dev/null
  ln ${INIT_DHCP} /etc/rc1.d/K35utdhcp 2>/dev/null
  ln ${INIT_DHCP} /etc/rc2.d/K35utdhcp 2>/dev/null
  ln ${INIT_DHCP} /etc/rc3.d/S35utdhcp 2>/dev/null
  print "### Disabling Routing";
  touch /etc/notrouter
  ndd -set /dev/ip ip_forwarding 0
fi

# do removals
for INTF in ${DHCP_DEL}; do
  MULTINET=N
  INTF_NETNAME=${CORONA_NAME}-${INTF}
  INTF_NET=`grep -w ${INTF_NETNAME} ${NETWORKS} | awk '{ print $2}'`
  while read name net rest
  do
    if [ "$name" != "${INTF_NETNAME}" -a "$net" = "${INTF_NET}" ]; then
      MULTINET=Y
    fi
  done <${NETWORKS}
  if [ -z "${INTF_NET}" ]; then
    print -u2 "Error: Cannot translate interface \"${INTF}\" to a network number"
    continue
  fi
  print "### Removing DHCP entries for \"${INTF}\""
  if [ $MULTINET = "N" ]; then
    dhtadm -D -m "${INTF_NET}";
    pntadm -R "${INTF_NET}" 2>&1 >/dev/null;
  else
    pntadm -P ${INTF_NET} | awk "/${INTF_NETNAME}/ { print \$3 }" \
    | while read ip rest
    do
	pntadm -D $ip ${INTF_NET}
    done
  fi
  dhtadm -D -m ${INTF_NETNAME};
done

for INTF in ${INTF_DEL}; do
  if [ ! -f ${HOSTNAME_R}.${INTF} ]; then
    print "Error: Interface \"${INTF}\" not configured - cannot be removed" >&2;
    continue;
  fi
  print "### Removing ${CORONA_TITLE} interface configuration for \"${INTF}\"";
  INTF_NAME=`cat ${HOSTNAME_R}.${INTF}`;
  INTF_IPA=`getent hosts ${INTF_NAME} | awk '{print $1}'`
  M=`getent netmasks ${INTF_IPA} | awk '{print $2}'` 
  if [ -z $M ]; then
    print -u2 "Error: netmask for host $1 does not exist";
    continue
  fi
  IP2Net ${INTF_IPA} ${M}
  if [ ${?} -ne 0 ]; then
    print -u2 "Error: Cannot translate host \"${INTF_NAME}\" for interface \"${INTF}\" to a network number"
    continue
  fi
  INTF_NET=$NET
  INTF_NETNAME=${CORONA_NAME}-${INTF}
  sed "/^${INTF_NETNAME}[ 	].*# SUNRAY ADD/d" ${NETWORKS} >${NETWORKS}.$$
  mv ${NETWORKS}.$$ ${NETWORKS}
  rmvNetMask ${INTF_NET}

  grep "^${INTF_IPA}[ 	].*# SUNRAY ADD" ${HOSTS} >/dev/null 2>&1
  if [ $? -ne 0 ]; then
    print "### Leaving interface \"${INTF}\" up"
  else
    ifconfig ${INTF} down 2>&1 >/dev/null;
    ifconfig ${INTF} unplumb 2>&1 >/dev/null;
    sed "/^${INTF_IPA}[ 	].*# SUNRAY ADD/d" ${HOSTS} >${HOSTS}.$$
    mv ${HOSTS}.$$ ${HOSTS}
    rm -f ${HOSTNAME_R}.${INTF}
  fi
  /etc/init.d/nscd stop 2>&1 >/dev/null;
  /etc/init.d/nscd start 2>&1 >/dev/null;
done

if [ ${REMOVE} = "Y" ]; then
  RestoreFile "${NSSWITCH}";
  RestoreFile "${INIT_DHCP}";
  RestoreFile "${INIT_DTLOGIN}";
  if [ -f "${INIT_SYSSUSPEND}" ]; then
    RestoreFile "${INIT_SYSSUSPEND}";
  fi
  RestoreFile "${INIT_SESSIONETC}";
  RestoreFile "${INIT_OPENWINSYS}";
  RestoreFile "${HOSTS}";
  RestoreFile "${NETWORKS}";
  RestoreFile "${NETMASKS}";

  # Remove the generic macro
  dhtadm -D -m "${CORONA_NAME}" 2> /dev/null

  # now remove all of the DHCP symbols
  for i in `dhtadm -P | awk '/SUNW.NewT.SUNW/ { print $1 }'`; do
    dhtadm -D -s $i 2> /dev/null
  done

  # remove the Corona crontab stuff
  rm -f /tmp/tmpfile.$$;
  crontab -l | grep -v "utlog" > /tmp/tmpfile.$$;
  crontab /tmp/tmpfile.$$;
  rm -f /tmp/tmpfile.$$;

  # remove log file entries in /etc/syslog.conf
  if [ -f /etc/syslog.conf ]; then
    grep -v "/var/opt/SUNWut/log" /etc/syslog.conf > /tmp/tmpfile.$$
    cp -f /tmp/tmpfile.$$ /etc/syslog.conf
    rm -f /tmp/tmpfile.$$
  fi
  
  
  rm /etc/rcS.d/K35utdhcp 2> /dev/null
  rm /etc/rc0.d/K35utdhcp 2> /dev/null
  rm /etc/rc1.d/K35utdhcp 2> /dev/null
  rm /etc/rc2.d/K35utdhcp 2> /dev/null
  rm /etc/rc3.d/S35utdhcp 2> /dev/null
  $UTSBIN/utfwadm -R

  exit 0;
fi

INTF_ALL=`ifconfig -a | sed -n -e '/lo0/d' -e '/: flags=/s/:.*//p'`

# do all the per-interface operations
I=0;
N=${DEF_START_NET};
for INTF in ${INTF_ADD}; do
  IFNM[${I}]=$INTF
  DHCPONLY[${I}]="N"
  # see if interface is already in use
  for test in ${INTF_ALL}; do
    if [ "${INTF}" = "${test}" -a -f "${HOSTNAME_R}.${INTF}" ]; then
      NAME[${I}]=`cat ${HOSTNAME_R}.${INTF}`
      if [ "${NAME[${I}]}" = ${HOSTNAME} ]; then
        print "Interface \"${INTF}\" is the primary network connection" >&2
	print "configured as hostname \"${HOSTNAME}\"." >&2
      else
        print "Interface \"${INTF}\" is already configured as" >&2
	print "hostname \""${NAME[${I}]}"\"" >&2
      fi
      YesOrNo "Do you want to configure DHCP on this network? " "N"
      if [ ${?} -ne 0 ]; then
	exit 1;
      fi
      DHCPONLY[${I}]="Y"
    fi
  done

  MULTINET[${I}]="N"
  NODHCP[${I}]="N"
  if [ "${DHCPONLY[${I}]}" = "N" ]; then
    # look in ${NETWORKS} file until we find a subnet that's available
    while :
    do
      fgrep "${DEF_NET_PFX}.${N}.0" ${NETWORKS} >/dev/null;
      if [ ${?} -ne 0 ]; then
        break;
      fi
      let "N = N + 1";
    done
  
    print "### configuring ${INTF} interface at subnet ${N}";
  
    # get interface network info
    IPADDR[${I}]="${DEF_NET_PFX}.${N}.1";
    NAME[${I}]="${HOSTNAME}-${INTF}";
    NETMASK[${I}]="${DEF_NET_MASK}";
    NETADDR[${I}]="${DEF_NET_PFX}.${N}.0";
    NETNAME[${I}]="${CORONA_NAME}-${INTF}";
    START_NUM[${I}]=${DEF_START_NUM};
    END_NUM[${I}]=${DEF_END_NUM};
    FWSRVR[${I}]=${IPADDR[${I}]}
    ROUTER[${I}]=${IPADDR[${I}]}
  
    SVIP=${IPADDR[${I}]}
    SVMSK=${NETMASK[${I}]}
    SVNET=${NETADDR[${I}]}
  else
    IPADDR[${I}]=`getent hosts ${NAME[${I}]} | awk '{print $1}'`
    config=`ifconfig ${INTF}`
    if [ $? -ne 0 ]; then
      print "Existing interface ${INTF} must be up to do DHCP configuration" >&2
      exit 1
    fi
    NETMASK[${I}]=`getNetMask "${config}"`
    IP2Net ${IPADDR[${I}]} ${NETMASK[${I}]}
    NETADDR[${I}]=$NET
    NETNAME[${I}]="${CORONA_NAME}-${INTF}"
    MaxHostNum $NETMASK[${I}]
    MHN=$H
    let "START_NUM[${I}] = $H - 10"
    let "END_NUM[${I}] = $H - 1"
    FWSRVR[${I}]=${IPADDR[${I}]}
    ROUTER[${I}]=`netstat -rn | grep '^default' | awk '{print $2}'`
  fi

  while :
  do
    NetPlusHost ${NETADDR[${I}]} ${START_NUM}
    START_ADDR=$IP
    NetPlusHost ${NETADDR[${I}]} ${END_NUM}
    END_ADDR=$IP

    print "  Selected values for interface \"${INTF}\" ";
    print "    host address:\t${IPADDR[${I}]}";
    print "    net mask:\t\t${NETMASK[${I}]}";
    print "    net address:\t${NETADDR[${I}]}";
    print "    host name:\t\t${NAME[${I}]}";
    print "    net name:\t\t${NETNAME[${I}]}";
    if [ ${START_NUM[${I}]} -ne 0 ]; then
      print "    first unit address:\t${START_ADDR}";
      print "    last unit address:\t${END_ADDR}";
    else
      print "    no DHCP configured"
    fi
    print "    firmware server:\t${FWSRVR[${I}]}";
    print "    router:\t\t${ROUTER[${I}]}";
    YesOrNo "  Accept as is?" "Y";
    if [ ${?} -eq 0 ]; then
      break;
    fi

    # get new ip address from user
    if [ "${DHCPONLY[${I}]}" = "N" ]; then
      while :
      do
        print -n "  new host address: [${IPADDR[${I}]}] ";
        read ANS
        :
        if [ -z "${ANS}" ]; then
  	  ANS=${IPADDR[${I}]};
        fi
        CheckIPA "${ANS}";
        if [ ${?} -eq 0 ]; then
  	  IPADDR[${I}]=${ANS};
  	  FWSRVR[${I}]=${ANS};
	  ROUTER[${I}]=${ANS};
  	  break;
        else
  	  print "Error:  invalid IP address\n";
        fi
      done

      MaxHostNum ${NETMASK[${I}]}
      MHN=$H
  
      # get new netmask from user
      while :
      do
        print -n "  new netmask: [${NETMASK[${I}]}] ";
        read ANS
        :
        if [ -z "${ANS}" ]; then
  	  ANS=${NETMASK[${I}]};
        fi
        CheckIPA "${ANS}";
        if [ ${?} -eq 0 ]; then
          MaxHostNum $ANS
          if [ ${?} -ne 0 ]; then
            print -u2 "Error: invalid netmask \"$ANS\""; 
    	    continue;
          fi
  	  NETMASK[${I}]=${ANS};
          MHN=$H
  	  break;
        else
  	  print "Error:  invalid netmask\n";
        fi
      done
  
      IP2Net ${IPADDR[${I}]} ${NETMASK[${I}]}
      NETADDR[${I}]=$NET
  
      # Check for duplicate network addresses
      fgrep "${NETADDR[${I}]}" ${NETWORKS} >/dev/null;
      if [ $? -eq 0 ]; then
        print -u2 "Error: network address \"${NETADDR[$I]}\" is already in use";
	YesOrNo "Configure multiple interfaces on this network? " "N"
	if [ $? -eq 0 ]; then
	  locmac=`/usr/sbin/eeprom | sed -n 's/local-mac-address?=//p'`
	  if [ "$locmac" != "true" ]; then
	    print -u2 "Error: must set prom variable local-mac-address? to \"true\""
	    print -u2 "See eeprom(1M)"
            IPADDR[${I}]=${SVIP};
            FWSRVR[${I}]=${SVIP};
            NETMASK[${I}]=${SVMSK};
            NETADDR[${I}]=${SVNET};
            continue;
	  fi
	  MULTINET[${I}]="Y"
	else
          IPADDR[${I}]=${SVIP};
          FWSRVR[${I}]=${SVIP};
	  ROUTER[${I}]=${SVIP};
          NETMASK[${I}]=${SVMSK};
          NETADDR[${I}]=${SVNET};
          continue;
	fi
      fi
  
      if [ ${NETADDR[${I}]} = ${IPADDR[${I}]} ]; then
        print -u2 "Error: host address \"${IPADDR[${I}]}\" is equal to net address";
        IPADDR[${I}]=${SVIP};
        FWSRVR[${I}]=${SVIP};
        ROUTER[${I}]=${SVIP};
        NETMASK[${I}]=${SVMSK};
        NETADDR[${I}]=${SVNET};
        continue;
      fi
  
      IP2Host ${IPADDR[${I}]} ${NETMASK[${I}]}
      if [ $H -eq $MHN ]; then
        print -u2 "Error: host address \"${IPADDR[${I}]}\" is subnet broadcast address";
        IPADDR[${I}]=${SVIP};
        FWSRVR[${I}]=${SVIP};
        ROUTER[${I}]=${SVIP};
        NETMASK[${I}]=${SVMSK};
        NETADDR[${I}]=${SVNET};
        continue;
      fi
  
      NAME[${I}]="${HOSTNAME}-${INTF}";
  
      # get new host name from user
      while :
      do
        print -n "  new host name: [${NAME[${I}]}] ";
        read ANS
        :
        if [ -z "${ANS}" ]; then
  	ANS=${NAME[${I}]};
        fi
        getent hosts "${ANS}";
        if [ ${?} -ne 0 ]; then
  	NAME[${I}]=${ANS};
  	break;
        else
  	print "Error:  host name already in use\n";
        fi
      done
  
      # make new net name
      NETNAME[${I}]="${CORONA_NAME}-${INTF}";
  
#      # get new netname from user
#      while :
#      do
#        print -n "  new net name: [${NETNAME[${I}]}] ";
#        read ANS
#        :
#        if [ -z "${ANS}" ]; then
#  	ANS=${NETNAME[${I}]};
#        fi
#        getent networks "${ANS}";
#        if [ ${?} -ne 0 ]; then
#  	  NETNAME[${I}]=${ANS};
#  	  break;
#        else
#  	  print "Error:  netname already in use\n";
#        fi
#      done
    fi

    # get new Corona desktop unit starting ip address from user
    if [ ${START_NUM[${I}]} -ge $MHN ]; then
      let "START_NUM[${I}]} = 4"
    fi
    NetPlusHost ${NETADDR[${I}]} ${START_NUM[${I}]}
    START_ADDR=$IP
    while :
    do
      print -n "  new first ${CORONA_TITLE} address: [${START_ADDR}] ";
      read ANS
      :
      if [ -z "${ANS}" ]; then
	ANS=${START_ADDR};
      fi
      if [ "$ANS" = "0" ]; then
	START_NUM[${I}]=0
	YesOrNo "Disable setup of DHCP for this interface? " "N"
	if [ $? -eq 0 ]; then
	  break
	fi
      fi
      CheckIPA "${ANS}";
      if [ ${?} -eq 0 ]; then
	IP2Net "${ANS}" ${NETMASK[${I}]}
	if [ $NET != ${NETADDR[${I}]} ]; then
	  print -u2 "Error: new first address not in correct subnet";
	  continue;
	fi
	IP2Host "${ANS}" ${NETMASK[${I}]}
	if [ $H -eq $MHN ]; then
	  print -u2 "Error: new first address is subnet broadcast address"; 
	  continue;
	fi
	if [ $H -eq 0 ]; then
	  print -u2 "Error: new first address is subnet address"; 
	  continue;
	fi
	START_NUM[${I}]=$H
	break;
      else
	print "Error:  invalid IP address\n";
      fi
    done

    # get new Corona desktop unit ending ip address from user
    if [ ${START_NUM[${I}]} -ne 0 ]; then
      if [ ${END_NUM[${I}]} -ge $MHN -o ${END_NUM[${I}]} -lt ${START_NUM[${I}]} ]
      then
        let "END_NUM[${I}] = $MHN - 1"
      fi
      NetPlusHost ${NETADDR[${I}]} ${END_NUM[${I}]}
      END_ADDR=$IP
      while :
      do
        print -n "  new last ${CORONA_TITLE} address: [${END_ADDR}] ";
        read ANS
        :
        if [ -z "${ANS}" ]; then
  	  ANS=${END_ADDR};
        fi
        CheckIPA "${ANS}";
        if [ ${?} -eq 0 ]; then
  	  IP2Net "${ANS}" ${NETMASK[${I}]}
  	  if [ $NET != ${NETADDR[${I}]} ]; then
  	    print -u2 "Error: new last address not in correct subnet";
  	    continue;
  	  fi
  	  IP2Host "${ANS}" ${NETMASK[${I}]}
  	  if [ $H -eq $MHN ]; then
  	    print -u2 "Error: new last address is subnet broadcast address"; 
  	    continue;
  	  fi
          if [ $H -lt ${START_NUM[${I}]} ]; then
  	    print "Error: last unit address is less than first unit address";
  	    continue;
          fi
  	  END_NUM[${I}]=$H
  	  break;
        else
  	  print "Error:  invalid IP address\n";
  	  continue;
        fi
      done
    fi

    while :
    do
      print -n "  new firmware server: [${FWSRVR[${I}]}] ";
      read ANS
      :
      if [ -z "${ANS}" ]; then
	ANS=${FWSRVR[${I}]};
      fi
      CheckIPA "${ANS}";
      if [ ${?} -eq 0 ]; then
	if [ ${ANS} = ${NETADDR[$I]} ]; then
	  print "Error: firmware server is equal to network address";
	  continue;
	fi
	FWSRVR[${I}]=${ANS};
	break;
      else
	print "Error:  invalid IP address\n";
	continue;
      fi
    done

    while :
    do
      print -n "  new router: [${ROUTER[${I}]}] ";
      read ANS
      :
      if [ -z "${ANS}" ]; then
	ANS=${ROUTER[${I}]};
      fi
      CheckIPA "${ANS}";
      if [ ${?} -eq 0 ]; then
	if [ ${ANS} = ${NETADDR[$I]} ]; then
	  print "Error: router is equal to network address";
	  continue;
	fi
  	IP2Net "${ANS}" ${NETMASK[${I}]}
  	if [ $NET != ${NETADDR[${I}]} ]; then
  	  print -u2 "Error: router is not in correct subnet";
  	  continue;
  	fi
  	IP2Host "${ANS}" ${NETMASK[${I}]}
  	if [ $H -eq $MHN ]; then
  	  print -u2 "Error: router address is subnet broadcast address"; 
  	  continue;
  	fi
	ROUTER[${I}]=${ANS};
	break;
      else
	print "Error:  invalid IP address\n";
	continue;
      fi
    done
  done

  # check if there are /etc/hostname.??? files for all of the given intfs
  if [ "${DHCPONLY[${I}]}" = "N" ]; then
    if [ -f "${HOSTNAME_R}.${INTF}" ]; then
      # exists, so check if it is setup for newt net service
      RESULT=`cat "${HOSTNAME_R}.${INTF}"`;
      if [ "${RESULT}" != "${NAME[${I}]}" ]; then
        # not the right contents, so complain and bail
        print "Warning: hostfile \"${HOSTNAME_R}.${INTF}\" contents invalid" >&2;
        print "         contains \"${RESULT}\", should contain \"${NAME[${I}]}\"" >&2;
        YesOrNo "         should the file be updated?" "Y";
        if [ ${?} -eq 0 ]; then
  	print "${NAME[${I}]}" > "${HOSTNAME_R}.${INTF}";
        else
  	print "      remove file and try again";
  	exit 1;
        fi
      fi;
    else
      # doesnt exist, so create it
      print "${NAME[${I}]}" > "${HOSTNAME_R}.${INTF}";
    fi
    print "### successfully setup \"${HOSTNAME_R}.${INTF}\" file";
  
    # check if this name is in the /etc/hosts file
    FIX=0;
    RESULT=`getent hosts ${NAME[${I}]}`
    if [ -z "${RESULT}" ]; then
      # name isnt there, so check if this ipaddr is in the /etc/hosts file
      RESULT=`getent hosts ${IPADDR[${I}]}`
      if [ -z "${RESULT}" ]; then
        # name and ipaddr arent there, so add this line to the /etc/hosts file
        print "${IPADDR[${I}]}\t${NAME[${I}]} # SUNRAY ADD - DO NOT MODIFY" >> "${HOSTS}";
      else
        # name isnt there, but ipaddr is, so fix this
        YesOrNo "Warning:  IP address \"${IPADDR[${I}]}\" appears in \"${HOSTS}\" with another hostname, fix?" "Y";
        if [ ${?} -eq 0 ]; then
  	FIX=1;
        else
  	print "Error:  conflict in ${HOSTS} file, resolve and install again" >&2;
  	exit 1;
        fi
      fi
    else
      # name is already in /etc/hosts, so check if it has the right ipaddr
      RESULT=`getent hosts ${IPADDR[${I}]}`
      if [ -z "${RESULT}"  ]; then
        # name is there but ipaddr is not correct, so fix it?
        YesOrNo "hostname \"${NAME[${I}]}\" appears in \"${HOSTS}\" file with another IP address, fix?" "Y";
        if [ ${?} -eq 0 ]; then
  	FIX=1;
        else
  	print "Error:  conflict in ${HOSTS}, resolve and install again" >&2;
  	exit 1;
        fi
      fi
    fi
  
    # update the /etc/hosts file
    if [ ${FIX} -eq 1 ]; then
      StartFileUpdate "${HOSTS}";
  
      # remove anything that matches netname and ipaddr and add new line
      rm -f /tmp/tmpfile.$$;
      sed -e "/[ 	]${NAME[${I}]}[ 	$]/s/^/# SUNRAY DEL /" \
      	-e "/^[ 	]*${IPADDR[${I}]}[ 	]/s/^/# SUNRAY DEL /" "${HOSTS}.$$" > /tmp/tmpfile.$$;
      print "${IPADDR}\t${NAME[${I}]} # SUNRAY ADD - DO NOT MODIFY" >> /tmp/tmpfile.$$;
      mv -f /tmp/tmpfile.$$ "${HOSTS}.$$";
  
      EndFileUpdate "${HOSTS}";
    fi
    print "### successfully setup \"${HOSTS}\" file";
  
  fi
  
  # update the /etc/netmasks file
  addNetMask ${NETADDR[${I}]} ${NETMASK[${I}]}
  print "### successfully setup \"${NETMASKS}\" file";

  addNetwork ${NETNAME[${I}]} ${NETADDR[${I}]}
  print "### successfully setup \"${NETWORKS}\" file";
  
  if [ "${DHCPONLY[${I}]}" = "N" ]; then
    # ifconfig the new interface
    ifconfig ${INTF} plumb;
    ifconfig ${INTF} "${IPADDR[${I}]}" up netmask "${NETMASK[${I}]}" broadcast +;
    print "### finished install of \"${INTF}\" interface";
  fi

  # go on to the next one
  let "I = I + 1";
  let "N = N + 1";
done

# Force databases to be reread by cache
/etc/init.d/nscd stop 2>&1 >/dev/null;
/etc/init.d/nscd start 2>&1 >/dev/null;

AddIntfs "${INTF_ADD}"
if [ -n "${INTF_ADD}" ]; then
  print "### Building network tables - this will take a few minutes";
  BuildNetworkTables "${INTF_ADD}"
  CheckEtcSystem;
fi

if [ -n "${INTF_ADD}" ]; then
  print "### Configuring firmware version for ${CORONA_TITLE}";
  let "I = 0"
  for INTF in ${INTF_ADD}; do
    if [ "${MULTINET[${I}]}" = "N" -a "${START_NUM[${I}]}" -ne 0 ]; then
      $UTSBIN/utfwadm -A -a -n ${INTF} -z
    fi
    let "I = I + 1"
  done

  # setup the automatic logging functions
  print "### Configuring ${CORONA_TITLE} Logging Functions";
  let MINUTE="$RANDOM % 50 + 5";
  rm -f /tmp/tmpfile.$$;
  $UTLIB/utlog -a -n "${INTF_ADD}" \
  	-s all -f user.info -d /var/opt/SUNWut/log/messages -z;
  $UTLIB/utlog -a -n "${INTF_ADD}" \
        -s all -f local1.info -d /var/opt/SUNWut/log/admin_log -z;
  
  (
  	crontab -l root 2>/dev/null | grep -v lib/utlog;
  	print "$MINUTE 3 * * * $UTLIB/utlog -c -d /var/opt/SUNWut/log/messages 2>/dev/null 1>/dev/null";
  	print "33 3 * * * $UTLIB/utlog -c -d /var/opt/SUNWut/log/admin_log 2>/dev/null 1>/dev/null";
  ) > /tmp/tmpfile.$$;
  crontab -r root
  crontab /tmp/tmpfile.$$
  rm -f /tmp/tmpfile.$$ 2>/dev/null;
fi

GetDHCPpid

# Don't use this code because of bug 4103373
#if [ -n "${dhcppid}" ]; then
#  kill -HUP "${dhcppid}"
#else
#  ${INIT_DHCP} start
#fi

if [ -n "${dhcppid}" ]; then
  kill "${dhcppid}"
fi
${INIT_DHCP} start

rm /tmp/*.$$ 2>/dev/null;
exit 0
