#! /bin/csh
set noglob
######Message Texts######
set mesg1 = "creating diskless swap directory   "
set done = "done."
set mesg2 = "creating /dev/enet*  "
set mesg3 = "creating /dev/pty*  "
set mesg4 = "Enter name for new diskless station: "
set mesg5 = "That name has already been used, please enter another: "
set mesg6 = "Is "
set mesg6b = " a graphics station? (y or n) "
set mesg7 = "  Please respond 'y' or 'n'"
set mesg10 = "Bootstrap string for diskless station will be:"
set mesg12 = " Type <return> when ready."
set mesg13 = "Node address file has not been set up yet, use admin to do so."
set mesg14 = "One moment please"
#########################
#
# csh script to install support for a new diskless station.
#
# for the first attempt, we will only worry about:
#
#        /tmp
#        /usr/tmp
#        /etc/ttys
#        /etc/ttytype
#        /etc/rc
#        /etc/rc.local
#        /etc/utmp
#        /usr/adm
#        /usr/lib/crontab
#

# make sure set_nodes has been run
set HID = `fgrep /bin/hostname /etc/rc.local | awk '{print $2}'`
if ( $HID == UNKNOWN ) then 
	echo $mesg13
	sleep 10
	exit(0)
endif
#
# now set up diskless swap area if it does not exist
# also devices for enet and pty's
#
if ( ! -e /usr/spool/diskless ) then
    echo -n $mesg1
#    mkdir /usr/spool/diskless
#    chmod 700 /usr/spool/diskless
    echo $done
endif
if ( ! -e /dev/enet0 ) then
    echo -n $mesg2
    set olddir=$cwd
    chdir /dev
#    ./MAKEDEV enet0
    chdir $olddir
    echo $done
endif
if ( ! -e /dev/ptyp0 ) then
    echo -n $mesg3
    set olddir=$cwd
    chdir /dev
#    ./MAKEDEV pty0
    chdir $olddir
    echo $done
endif
#
# get hostname for new diskless node and
# assign next available address
#
echo -n $mesg4" "
get_newhost:
set NEWHOST = ($<)
if ( $#NEWHOST == 0 ) then
	goto get_newhost
endif
fgrep -s " "$NEWHOST" " /etc/hosts
if ( $status != 1 ) then 
	echo -n $mesg5" "
	goto get_newhost
endif

g_loop:
echo -n $mesg6$NEWHOST$mesg6b" "
set GRAPHICS = ($<)
if ( ($GRAPHICS != y) && ($GRAPHICS != n)  ) then
    echo $mesg7
    goto g_loop
endif

echo $mesg14
set NET = `awk '{print $1" " $2}' /etc/hosts | grep -w $HID | awk '{FS="."} {print $1"."$2}'`
fgrep $NET /etc/hosts | awk '{FS="."} {print $3"."$4}' | awk '{print $1}' > temp
set i = 0
set j = 1
low_loop:
fgrep -s $i"."$j temp
if ( $status != 1) then
	set j = `expr $j + 1`
	if ( $j > 255 ) then
		set j = 1
		set i = `expr $i + 1`
	endif
	goto low_loop
endif
set MID = $j
/bin/rm temp

#
# do file installations, with special cases for /etc/rc.local and
# /etc/ttys if diskless node is graphics.
#

set SID = `awk '{print $1" " $2}' /etc/hosts | grep -w $HID | awk '{FS="."} {print $4}' | awk '{print $1}'`
echo $mesg10
echo ""
echo "	ex(0,0)${HID}:/vmunix HOST=$NEWHOST MID=$MID SID=$SID"
echo ""
echo $mesg12
set FOO = ($<)
