#!/bin/sh
#
# Boot client configuration
# This script totally relies on proper NIS setup
# and does not even try to modify /etc files,
# unlike add_install_client by SunSoft
#

machine=`uname -n`
directory=`pwd`
rev=`uname -r | awk -F. '{print $1}'`

if [ "$machine" = "tkussup1" -a "$directory" = "/export/install/config/tftpboot" ] ; then
	TFTPDIR=.
else
	TFTPDIR=/tftpboot
fi
export TFTPDIR

ECHO_N="echo -n"
NOCR=" "
PSFLAGS="-augwwwx"
if [ "$rev" = "5" ] ; then
	ECHO_N="echo "
	NOCR=" \c "
	PSFLAGS="-ef"
fi

while [ -z "$name" ] ; do
    $ECHO_N "Client host name                         : $NOCR"
    read answ
    if ypmatch $answ hosts > /dev/null 2>&1 ; then
	name=$answ
	for i in ethers bootparams ; do
	    if ypmatch $name $i > /dev/null 2>&1 ; then true
	    else
		echo Jumpstart client $name is not present in $i map
		exit 1
	    fi
	done
    else
	echo Host $answ not in hosts NIS map - try again
    fi
done

while [ -z "$os" ] ; do
    $ECHO_N "Client OS - [ sunos/solaris ]            : $NOCR"
    read answ
    answ=`echo $answ | dd conv=lcase 2>/dev/null`
    case $answ in
	sunos) os=$answ ;;
	solaris)os=$answ ;;
	*) echo Unknown os $answ ;;
    esac
done

while [ -z "$karch" ] ; do
    $ECHO_N "Client kernel architecture (sun4c/m/d/u) : $NOCR"
    read answ
    answ=`echo $answ | dd conv=lcase 2>/dev/null`
    case $answ in
	sun4c) karch=$answ ;;
	sun4m) karch=$answ ;;
	sun4d) karch=$answ ;;
	sun4u) karch=$answ ;;
	*) echo Unknown kernel architecture $answ ;;
    esac
done

inetboot=$os.$karch
ipaddr=`ypmatch $name hosts | awk '{print $1}' |\
	awk -F. '{printf("%02x%02x%02x%02x\n", $1,$2,$3,$4)}' |\
	dd conv=ucase 2>/dev/null `


entry=${ipaddr}.`echo $karch|dd conv=ucase 2>/dev/null`

cd $TFTPDIR && (rm -f $ipaddr $entry ; ln -s $inetboot $entry)

#while [ -z "$yesno" ] ; do
#    $ECHO_N "Do you want to re-initialise inetd configuration now [y] : $NOCR"
#    read answ
#    case $answ in
#    "") yesno=yes ;;
#    "y*") yesno=yes ;;
#    "n*") yesno=no ;;
#    *)    ;;
#done
#
## Do not forget to Modify tftp line in
#if [ "yesno" = "yes" ] ; then
#        pid=`ps $PSFLAGS | grep inetd" | awk '{print 1}'`
#        kill -HUP $pid  && echo inetd reconfigured
#fi
