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

(( $# == 3 )) || {
	print "usage: jdhostadd name enet ip"
	exit 1
}

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

print Adding $1

# Add entry into dhcp network database (creates it if necessary).  Do this
# first so failure won't corrupt the other configuration files.

net=$(net_number $3)

cid=$(client_id $2)

pntadm -A $3 -c JavaStation -e -1 -f 'MANUAL+PERMANENT' -i $cid -m $1 $net
ec=$?
(( $ec < 4 )) || {
	print "pntadm returned error $ec"
	exit 1
}

# Put hostname/IP address mapping in /etc/hosts (remove duplicates)

hosts=/etc/hosts
[[ -f $hosts ]] || {
	print "$hosts does not exist"
	exit 1
}

trap "rm -f $hosts.$$" EXIT

nawk -f - $hosts > $hosts.$$ <<-FINIS
\$1 == "$3" || \$2 == "$1"	{ next }
				{ print }
END				{ print "$3\t$1" }
FINIS

cp $hosts.$$ $hosts

rm -f $hosts.$$

trap - EXIT

# Create /etc/ethers if needed

touch /etc/ethers

# Put hostname/ethers address mapping in /etc/ethers (remove duplicates)

ethers=/etc/ethers
[[ -f $ethers ]] || {
	print "$ethers does not exist"
	exit 1
}

trap "rm -f $ethers.$$" EXIT

nawk -f - $ethers > $ethers.$$ <<-FINIS
\$1 == "$2" || \$2 == "$1"	{ next }
				{ print }
END				{ print "$2\t$1" }
FINIS

cp $ethers.$$ $ethers

rm -f $ethers.$$

trap - EXIT

# Verify pkgadd created /tftpboot/inetboot.jd

cd /tftpboot > /dev/null

inetboot=inetboot.jd
[[ -f $inetboot ]] || {
	print "$inetboot does not exist"
	exit 1
}

# Create link /tftpboot/IP.SUN4M -> ./inetboot.jd

hexip=$(hexIP $3)
[[ -L $hexip.SUN4M ]] || {
	ln -s $inetboot $hexip.SUN4M
	(( $? == 0 )) || {
		print "$hexip.SUN4M not linked"
		exit 1
	}
}

cd - > /dev/null

#If needed, uncomment out tftpd entry in /etc/inetd.conf and restart inetd

inetd=/etc/inetd.conf
[[ -f $inetd ]] || {
	print "$inetd does not exist"
	exit 1
}

trap "rm -f $inetd.$$" EXIT

nawk -f - $inetd > $inetd.$$ <<-FINIS
\$1 == "#tftp"	{ \$1 = "tftp"; found = "true" }
		{ print }
END		{ if (found)
			exit 0
		  exit 1
		}
FINIS
(( $? == 0 )) && {
	cp $inetd.$$ $inetd
	ps -e | nawk '
	/inetd/	{ system("kill -HUP " $1) }
	'
}

rm -f $inetd.$$

trap - EXIT

# Verify pkgadd created /export/root/JavaDesktop/kona

kona=/export/root/JavaDesktop/kona
[[ -f $kona ]] || {
	print "$kona does not exist"
	exit 1
}

# Create /etc/bootparams if needed

touch /etc/bootparams

# Put root path in /etc/bootparams (remove duplicates)

bootparams=/etc/bootparams
[[ -f $bootparams ]] || {
	print "$bootparams does not exist"
	exit 1
}

trap "rm -f $bootparams.$$" EXIT

server=$(uname -n)
nawk -f - $bootparams > $bootparams.$$ <<-FINIS
\$1 == "$1"	{ next }
		{ print }
END		{ print "$1\troot=$server:/export/root/JavaDesktop",
			"boottype=:js"
		}
FINIS

cp $bootparams.$$ $bootparams

rm -f $bootparams.$$

trap - EXIT

# Add files to hosts, ethers, bootparams, services lookup in /etc/nsswitch.conf

nsswitch=/etc/nsswitch.conf
[[ -f $nsswitch ]] || {
	print "$nsswitch does not exist"
	exit 1
}

trap "rm -f $nsswitch.$$" EXIT

nawk -f - $nsswitch > $nsswitch.$$ <<-FINIS
\$1 == "hosts:"	&& \$2 != "files"	{
		sub(/hosts:[ \t]+/, "hosts:\tfiles ") }
\$1 == "ethers:" && \$2 != "files"	{
		sub(/ethers:[ \t]+/, "ethers:\tfiles ") }
\$1 == "bootparams:" && \$2 != "files"	{
		sub(/bootparams:[ \t]+/, "bootparams:\tfiles ") }
\$1 == "services:" && \$2 != "files"	{
		sub(/services:[ \t]+/, "services:\tfiles ") }
		{ print }
FINIS

cp $nsswitch.$$ $nsswitch

rm -f $nsswitch.$$

trap - EXIT

# If needed, add entry to /etc/dfs/dfstab:
#
#	share -F nfs -o ro -d "Java Desktop" /export/root/JavaDesktop
#

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"	{ found="true" }
		{ print }
END		{ if (found)
			exit 1
		  print "share -F nfs -o ro -d \"Java Desktop\"",
			"/export/root/JavaDesktop"
		  exit 0
		}
FINIS
(( $? == 0 )) && {
	cp $dfstab.$$ $dfstab
	share -F nfs -o ro -d "Java Desktop" /export/root/JavaDesktop
}
rm -f $dfstab.$$

trap - EXIT

print $1 added
