#!/bin/ksh -p
#pragma ident "@(#)jdhostdel	1.7 96/11/14 SMI"
#
# Copyright 14 Nov 1996 Sun Microsystems, Inc. All Rights Reserved
#
# Synopsis: jdhostdel name

(( $# == 1 )) || {
        print "usage: jdhostdel name"
        exit 1
}

fullname=$(whence $0)
dirname=${fullname%/*}
FPATH=${dirname%%/bin*}/lib

print Deleting $1

# Remove <hostname> entry from /etc/bootparams

bootparams=/etc/bootparams
[[ -f $bootparams ]] && {
	trap "rm -f $bootparams.$$" EXIT
 
	nawk -f - $bootparams > $bootparams.$$ <<-FINIS
	\$1 == "$1"     { next }
        	        { print }
	/JavaDesktop/	{ found="true" }
	END		{ if (found)
				exit 0
			  exit 1
			}
	FINIS
	(( $? == 1 )) && no_more=true

	cp $bootparams.$$ $bootparams
 
	rm -f $bootparams.$$
 
	trap - EXIT
}

# If all JD entries removed from /etc/bootparams, remove entry in dfstab

[[ $no_more == true ]] && {
	dfstab=/etc/dfs/dfstab
	[[ -f $dfstab ]] || {
        	print "$dfstab does not exist"
        	exit 1
	}
 
	trap "rm -f $dfstab.$$" EXIT
 
	nawk -f - $dfstab > $dfstab.$$ <<-FINIS
	\$NF == "/export/root/JavaDesktop"	{ next }
                				{ print }
	FINIS

	cp $dfstab.$$ $dfstab
        unshare -F nfs /export/root/JavaDesktop

	rm -f $dfstab.$$
 
	trap - EXIT

	# If /etc/bootparams is empty, remove it

	ls=$(ls -s /etc/bootparams)
	(( ${ls% *} == 0 )) && 	rm -f /etc/bootparams
}

# Remove <hostname> entry from /etc/ethers

ethers=/etc/ethers
[[ -f $ethers ]] && {
	trap "rm -f $ethers.$$" EXIT
 
	nawk -f - $ethers > $ethers.$$ <<-FINIS
	\$2 == "$1"	{ next }
			{ print }
	FINIS
 
	cp $ethers.$$ $ethers
 
	rm -f $ethers.$$
 
	trap - EXIT

	# If /etc/ethers is empty, remove it

	ls=$(ls -s /etc/ethers)
	(( ${ls% *} == 0 )) && rm -f /etc/ethers
}

# Verify existence of /etc/hosts

hosts=/etc/hosts
[[ -f $hosts ]] || {
        print "$hosts does not exist"
        exit 1
}
 
# Delete link /tftpboot/IP.SUN4M

hexip=$(hexIP $(nawk -v name=$1 '$2 == name { print $1; exit }' $hosts))
rm -f /tftpboot/$hexip.SUN4M

# If all links deleted, we should recomment tftpd in /etc/inetd.conf, but
# we really can't because somebody else may be using it

# Remove <hostname> entry from dhcp database.  It would be better to do
# this last so failure won't leave extra data in files, but we still
# need /etc/hosts to get the IP address.

ip=$(name_to_ip $1)
net=$(net_number $ip)

pntadm -D $1 $net
ec=$?
(( $ec < 4 )) || {
	print "pntadm returned error $ec"
	exit 1
}

# Remove <hostname> entry from /etc/hosts

trap "rm -f $hosts.$$" EXIT
 
nawk -f - $hosts > $hosts.$$ <<-FINIS
\$2 == "$1"	{ next }
                { print }
FINIS
 
cp $hosts.$$ $hosts
 
rm -f $hosts.$$
 
trap - EXIT

# Should restore /etc/nsswitch.conf if all Java Desktops removed from server
# However because of the add algorithm, we can't get it back to original 
# form in all cases (e.g. if first field was files).

print $1 deleted



