#!/bin/ksh -p
#pragma ident "@(#)jdhostcfg	1.9 96/11/23 SMI"
#
# Copyright 23 Nov 1996 Sun Microsystems, Inc. All Rights Reserved
#
# Synopsis: jdhostcfg config_file

[[ $(id) == uid=0\(root\)* ]] || {
	print "Must be run as root"
	exit 1
}

[[ $(uname -r) == 5.5* ]] || {
	print "Solaris 2.5 or 2.5.1 required"
	exit 2
}

pkginfo -q SUNWjdse || {
	print "SUNWjdse must be installed"
	exit 3
}

(( $# == 1 )) || {
	print "usage: jdhostcfg config_file"
	exit 4
}

[[ -f $1 && -r $1 && -s $1 ]] || {
	print "Configuration not readable"
	exit 5
}

fullname=$(whence $0)
dirname=${fullname%/*}
export PATH=$dirname:$PATH

# Records seperated by newline (\n).  Fields seperated by pipe (|).
# i.e. Type|hostname|Enet addr|IP addr
# Where type is +, -, % for add, del, mod
#
# Examples:
#
# +|bohemia|08:00:20:19:da:72|129.146.249.163
# -|bohemia
# %|bohemia|08:00:20:7a:4:48
#
# Global data is represented as:
# *|dns domain|dns server|subnet mask|router

nawk '
BEGIN	{ FS = "|"; print "Configuration started" }
$1 == "+" { status=system("jdhostadd " $2 " " $3 " " $4)
	    if (status) {
		print "Addition of " $2 " failed"
		exit
	    }
	  }
$1 == "-" { status=system("jdhostdel " $2)
	    if (status) {
		print "Deletion of " $2 " failed"
		exit
	    }
	  }
$1 == "%" { status=system("jdhostmod " $2 " " $3)
	    if (status) {
		print "Modification of " $2 " failed"
		exit
	    }
	  }
$1 == "*" { for (i = 2; i < 6; i++)	# remove blank values
		if (! $i)
			$i = "-"
	    status=system("jdhostgbl " $2 " " $3 " " $4 " " $5)
	    if (status) {
		print "Global updates failed"
		exit
	    }
	  }
END	  { if (status) {
		print "Configuration failed"
		exit 1
	    } else
		print "Configuration complete"
	  }
' $1
(( $? == 0 )) || exit 6

# What processes are running?

ps=/tmp/ps.$$
trap "rm -f $ps" EXIT
ps -e > $ps

# Start up necessary services

st=0
grep -s nfs /etc/dfs/sharetab > /dev/null
(( $? == 0 )) && st=1

tb=0
[[ -d /tftpboot ]] && tb=1

nawk -v sharetab=$st -v tftpboot=$tb '
/nfsd/		{ nfsd=$1; next }
/mountd/	{ mountd=$1; next }
/in.rarpd/	{ rarpd=rarpd " " $1; next }
/rpc.boot/	{ bootparamd=$1; next }
/in.dhcpd/	{ dhcpd=$1; next }
END		{ if (sharetab) {
			if (! nfsd)
				system("/usr/lib/nfs/nfsd -a 16")
			if (! mountd)
				system("/usr/lib/nfs/mountd")
		  } else {
			if (nfsd)
				system("kill " nfsd)
			if (mountd)
				system("kill " mountd)
		  }
		  if (tftpboot) {
			if (! rarpd)
				system("/usr/sbin/in.rarpd -a")
			if (! bootparamd)
				system("/usr/sbin/rpc.bootparamd")
		  } else {
			if (rarpd)
				system("kill " rarpd)
			if (bootparamd)
				system("kill " bootparamd)
		  }
		  if (dhcpd)
			system("kill -HUP " dhcpd)
		  else
			system("/usr/lib/inet/in.dhcpd")
		}
' $ps

rm -f $ps
trap - EXIT
