#!/bin/ksh
#
# Install a client image from a cpio archive area.  Arguments are the archive
# area, with either and absolute pathname of a name like machine:nfs-partition
# and the name of a hostinfo file.  Hostinfo files have to contain the
# setting of the following shell variables:
# 
# NODENAME - the name of the machine
# CURRENT - name of current archive
# 

PATH=/usr/sbin:/usr/bin
export PATH

BASE=/export

SHOULDREBOOT=


########### misc stuff - jme #################

start_tick()
{
	while true
	do
		sleep 5
		echo ".\c"
	done &
	TICKPID=$!
}

stop_tick()
{
	[ -n "$TICKPID" ] && {
		kill -9 $TICKPID
		TICKPID=
		sleep 1
	}
}


cpioit()
{
	start_tick; cpio -idmucB < $1; stop_tick
}
	
tarit()
{
	start_tick; tar xf $1; echo; stop_tick
}
	


acquire_lock()
{
	[ $# -ne 1 ] && {
		echo "Bad param to acquire_lock()"
		cleanup 1
	}
	[ -n "$_ACQUIRED" ] && {
		echo "acquire_lock: lock already acquired"
		cleanup 1
	}

	_LOCKFILE=/tmp/LOCK.${1}
	_TRY=/tmp/TRYLOCK.${1}.$$
	echo "acquire_lock (${_LOCKFILE}): \c"

	while true
	do
		touch $_TRY
		ln -n $_TRY $_LOCKFILE 2>/dev/null
		if [ $? -eq 0 ]
		then
			rm $_TRY
			_ACQUIRED=Y
			_alreadysleeping=
			echo " Got it."
			return
		else
			[ -z "$_alreadysleeping" ] && \
				echo " Busywait \c"
			_alreadysleeping=Y
			(( _delay = $RANDOM %  5 + 6 ))
			echo "${_delay},\c"
			sleep $_delay
		fi
	done
}

release_lock()
{
	if [ -n "$_ACQUIRED" ]
	then
		echo "release_lock: releasing $_LOCKFILE"
		rm $_LOCKFILE
		_ACQUIRED=
		echo
	else
		echo "release_lock: must call acquire_lock() first"
		cleanup 1
	fi
}


##############################################




#
# Clean up after ourselves.  This means we unmount anything we mounted
# and get rid of bogus directories
#
cleanup()
{
     rm $_LOCKFILE $_TRY /tmp/admin.$$ 2>/dev/null
     stop_tick # just in case

     [ -n "$archmounted" ] && {
          echo "Unmounting arch area"
          cd /
          umount $archmount > /dev/null 2>&1
          rmdir $archmount > /dev/null 2>&1
     }   
     [ -n "$_hostmount" ] && {
	echo "Unmounting hostinfo area"
	cd /
	umount $_hostmount >/dev/null 2>&1
	rmdir $_hostmount >/dev/null 2>&1
     }
 
     case $1 in
        0) echo "Done.";;
        *) echo "Exiting ... " ;;
     esac

     trap 0 2 3 9 15
     exit $1
}




sourcehostinfo()
{
	if [ `expr "$hostinfo" : '.*\:.*'` -ne 0 ]
	then
		_hostmount=/tmp/hostinfo.$$
		mkdir $_hostmount
		mount $hostinfo $_hostmount

		mount|grep $_hostmount > /dev/null 2>&1 || {
			echo "Couldn't mount hostinfo $hostinfo."
			cleanup 1
		}
		filetosource=$_hostmount
	else
		filetosource=$hostinfo
	fi

	if [ -f $filetosource ]
	then
		oldenv=`env`
    		. $filetosource
		eval $env
	else
    		echo Can\'t access $hostinfo hostinfo file.
    		cleanup 1
	fi
}




checkether()
{
    _ether=$1
    _funky=`echo $_ether|tr ":" " "`
    [ -z "$_funky" ] && return 1
    set $_funky
    [ "$#" -ne 6 ] && return 1
    for i in $_funky
    do
	if [ `expr $i : '[0-9a-fA-F][0-9a-fA-F]*'` -eq 0 ]
	then
		return 1
	fi
    done
    return 0
}

checkip()
{
    _IPADDR=$1
    _funky=`echo $_IPADDR|tr "." " "`
    [ -z "$_funky" ] && return 1
    set $_funky
    [ "$#" -ne 4 ] && return 1
    for i in $_funky
    do
	[ $i -lt 1 -o $i -gt 255 ] && return 1
    done
    return 0
}

setvars()
{
	[ -z "$NODENAME" ] && {
		echo "$progname: NODENAME is not specified"
		cleanup 1
	}

	[ -z "$CURRENT" ] && {
		echo "$progname: CURRENT is not specified"
		cleanup 1
	}

	case "$karch" in
		i86pc)	Bootmethod="rpl"
			Bootdir="/rplboot"
			Bootprog="rpld"
			Majorarch=i386
			;;

		sun4*)	Bootmethod="tftp"
			Bootdir="/tftpboot"
			Bootprog="tftp"
			Majorarch=sparc
			;;

		*)	echo "$progname: unknown architecture \"$karch\""
			cleanup 1
	esac

	[ -z "$USERLOC" ] && USERLOC=$BASE/exec/$CURRENT/$Majorarch

        if [ -f $archmount/${karch}.root ]
        then
                archorpkg=Archive
        elif [ -d $archmount/SUNWcsr ]
        then
                archorpkg=Package
        else
		echo "$progname: OS area must be an archive or a package directory"
	fi

	[ -z "$ROOTLOC" ] && ROOTLOC=$BASE/root/$NODENAME
	[ -z "$SWAPLOC" ] && SWAPLOC=$BASE/swap/$NODENAME
	[ -z "$SWAPSIZE" ] && SWAPSIZE=8
	[ -z "$NETIF" ] && NETIF=le0

	TESTDIR=${TESTDIR:-/autohome}
	[ -z "$TESTLOC" ] && TESTLOC=$ROOTLOC/$TESTDIR

	#[ -z "$ETHER" ] && ETHER=`ypmatch $NODENAME ethers | awk '{print $1}'`
	[ -z "$ETHER" ] && ETHER=`nismatch name=$NODENAME ethers.org_dir | awk '{print $1}'`
	[ -z "$ETHER" ] && {
		echo "$progname: Can't find Ethernet address for $NODENAME"
		cleanup 1
	}
	checkether $ETHER || {
		echo "$progname: Invalid ETHER address: $ETHER"
		cleanup 1
	}

	#[ -z "$IPADDR" ] && IPADDR=`ypmatch $NODENAME hosts | awk '{print $1}'`
	[ -z "$IPADDR" ] && IPADDR=`nismatch $NODENAME hosts.org_dir | awk '{print $3}'`
	[ -z "$IPADDR" ] && {
		echo "progname: Can't find Internet address for $NODENAME"
		cleanup 1
	}
	checkip $IPADDR || {
		echo "$progname: Invalid IP address: $IPADDR"
		cleanup 1
	}

}

createdirs()
{
    if [ -d $ROOTLOC ]
    then
	echo "Removing old root area ($ROOTLOC).\c"
	start_tick
	rm -rf $ROOTLOC
	stop_tick
	echo
    fi
    mkdir -p $ROOTLOC
    [ ! -d $USERLOC ] && mkdir -p $USERLOC

    # since we have to swap somewhere, we'll swap to the server via NFS
    # (the vfstab entry is created later)
    echo "Creating local swap file"
    mkdir -p `dirname $SWAPLOC` 2>/dev/null
    rm -f $SWAPLOC
    mkfile ${SWAPSIZE}m $SWAPLOC
    [ $? -ne 0 ] && {
	echo "Couldn't make swaploc."
	cleanup 1
    }
    chmod 1600 $SWAPLOC

    mkdir -p $TESTLOC
}



copydirs()
{
	if [ "$archorpkg" = Archive ]
	then
		copycpio
	else
		copypkg
	fi
}

#
# cpio stuff from the arch onto the created dirs.  Do generic stuff,
# then architectuure specific stuff, then remove the bogus
# arch directories
#
copycpio() {
    echo "Copying generic root stuff.\c"
    cd $ROOTLOC
    cpioit ${archmount}/generic.root

    echo "Copying architecture-specific root stuff.\c"
    cpioit ${archmount}/${karch}.root

    touch reconfigure

    acquire_lock exportusr
    [ ! -d $USERLOC/usr ] && {
    	echo "Copying generic usr stuff.\c"
    	cd $USERLOC
    	cpioit ${archmount}/generic.usr

	#echo "Moving generic usr/kvm stuff to holding area for later reference."
	#mv usr/kvm usr/kvm.generic
	#ln -s ../usrkvm usr/kvm

	# fix pkgadd & pkginstall permissions
	chmod 555 usr/sbin/pkgadd
	chmod 555 usr/sbin/installf
	chmod 555 usr/sadm/install/bin/pkginstall

    	#echo "Copying terminfo stuff.\c"
    	#cd $USERLOC
#    	mount -F lofs $USERLOC/usr $ROOTLOC/usr
    	#tarit ${archmount}/generic.terminfo
#        umount $ROOTLOC/usr
    }
    release_lock

    #echo "Copying generic usr/kvm stuff to root area.\c"
    #mkdir $ROOTLOC/usr/kvm
    #cd $USERLOC/usr/kvm.generic
    #find . -print | cpio -pdm $ROOTLOC/usr/kvm/.

    echo "Copying architecture-specific usr stuff to root area.\c"
    cd $ROOTLOC
    cpioit ${archmount}/${karch}.usr
    #mv usr/kvm usrkvm

}

#
# Same as copycpio but add the packages rather than cpio archives.
#
copypkg() {
    echo "Adding the packages:"
    _curwd=`pwd`
    cd $archmount

    # if .order file exists in the package directory, use the .order
    # file as a sequence of package applications.
    # If not, then just add all of the packages in the package directory
    [ -f .order ] && PACKAGES=`cat .order`
    if [ -n "$PACKAGES" ]
    then
        for p in $PACKAGES
        do
                PKGS="$PKGS `ls -d $p $p.* 2>/dev/null`"
        done
    else
        PKGS=*
    fi

    pkgsetup /tmp/admin.$$

    echo "Adding root packages.\c"
    pkgaddit root $ROOTLOC

    touch $ROOTLOC/reconfigure

    acquire_lock exportusr
    [ ! -d $USERLOC/usr ] && {
        echo "Adding usr packages.\c"
	pkgaddit usr $USERLOC

	#cd $USERLOC
#        echo "Moving generic usr/kvm stuff to holding area for later reference."
#        mv usr/kvm usr/kvm.generic
	#ln -s ../usrkvm usr/kvm

        # fix pkgadd & pkginstall permissions
#        chmod 555 usr/sbin/pkgadd
#        chmod 555 usr/sbin/installf
#        chmod 555 usr/sadm/install/bin/pkginstall

    }
    release_lock
 
#    echo "Copying generic usr/kvm stuff to root area.\c"
#    mkdir $ROOTLOC/usr/kvm
#    cd $USERLOC/usr/kvm.generic
#    find . -print | cpio -pdm $ROOTLOC/usr/kvm/.


    echo "Adding architecture-specific usr/kvm packages.\c"
    cd $archmount
    pkgaddit kvm $ROOTLOC
    cd $ROOTLOC
    mv usr/kvm usrkvm

    rm -f /tmp/admin.$$

    cd $_curwd

}

pkgsetup()
{
        cat <<EOF > $1
basedir=default
mail=
instance=nocheck
partial=nocheck
runlevel=nocheck
idepend=nocheck
rdepend=nocheck
setuid=nocheck
conflict=nocheck
action=nocheck
space=nocheck
EOF
}

pkgaddit()
{
    pkgtype=$1
    pkgbase=$2
    FSPKG=
    for p in $PKGS
    do
	sunw_pkgtype=`grep "^SUNW_PKGTYPE=" $p/pkginfo`
	[ -z "$sunw_pkgtype" ] && {
		echo "$0: WARNING: couldn't determine SUNW_PKGTYPE for package $p: will skip loading package"
		continue
	}
	[ "$sunw_pkgtype" = "SUNW_PKGTYPE=$pkgtype" ] && FSPKG="$FSPKG $p"
    done
    # first find packages which have no dependencies
    pkgs=
    for p in $FSPKG
    do
        [ ! -f $p/install/depend ] &&  pkgs="$pkgs $p"
    done
    # now grab packages with dependencies
    for p in $FSPKG
    do
        [ -f $p/install/depend ] &&  pkgs="$pkgs $p"
    done
    for pkg in $pkgs
    do
        add_package $pkgbase $pkg $karch $Majorarch
    done
    echo ""
}


#       usage: add_package <package> <architecture> <machine_type>
#
#       install package on the server based on the 
#       BASEDIR value in the pkginfo file.
#
add_package() {

	base=$1
        package=$2
        destarch=$3
        destmach=$4
        [ ! -f $package/pkginfo ] && {
                echo "WARNING: Package $package not found on server"
                return
        }
        pkgarch=`grep "^ARCH=" $package/pkginfo | sed -e "s/^ARCH=//" `
        if [ "(" $pkgarch != "${destmach}" ")" -a "(" $pkgarch != "${destmach}.${destarch}" ")" ]
        then
                return
        fi
	
	echo "$package \c"
        [ ! -d /tmp/xdir ] && mkdir /tmp/xdir
        pkgbase=`grep "^BASEDIR=" $package/pkginfo | sed 's/^.*=//'`
        pkgbase=${base}$pkgbase

        /usr/sbin/pkgadd -d $archmount -R $base -a /tmp/admin.$$ $package >/tmp/xdir/$package.pkgadd 2>&1
        error=$?
        if [ $error -ne 0 ]
        then
                echo "WARNING: could not install $package (errorcode=$error)"
                echo "         please consult /tmp/xdir/$package.pkgadd for details"
                echo "Removing $package"
                /usr/sbin/pkgrm -n -R $pkgbase $package >/dev/null 2>&1
        fi
 
}

########### Server routines #############

setserver_dfstab()
{
	[ ! -f /etc/dfs/dfstab ] && {
		touch /etc/dfs/dfstab
		echo "Note: had to create a /etc/dfs/dfstab"
		SHOULDREBOOT=Y
	}

	acquire_lock dfstab

	grep "$BASE/exec$" /etc/dfs/dfstab >/dev/null || \
	echo "share -F nfs -o ro $BASE/exec" >> /etc/dfs/dfstab

	cp /etc/dfs/dfstab /etc/dfs/dfstab.old
	(
		egrep -v "$ROOTLOC|$SWAPLOC" /etc/dfs/dfstab.old
		echo "share -F nfs -o rw=${NODENAME},root=${NODENAME} $ROOTLOC"
		echo "share -F nfs -o rw=${NODENAME},root=${NODENAME} $SWAPLOC"
	) > /etc/dfs/dfstab

	shareall

	release_lock
}

setserver_bootparams()
{
	rpl_config=
	if [ "${Bootmethod}" = "rpl" ]; then
		rpl_config=" numbootfiles=3 \
		bootfile=/rplboot/${IPADDR}.hw.com:45000 \
		bootfile=/rplboot/${IPADDR}.glue.com:35000 \
		bootfile=/rplboot/${IPADDR}.inetboot:8000 \
		bootaddr=35000"
	fi

	acquire_lock bootparams

	[ ! -f /etc/bootparams ] && touch /etc/bootparams
	cp /etc/bootparams /etc/bootparams.old
	(
		grep -v "^$NODENAME" /etc/bootparams.old
		echo "$NODENAME root=${localhost}:$ROOTLOC swap=${localhost}:$SWAPLOC $rpl_config"
	) > /etc/bootparams

	if ps -ef | grep -v grep | grep bootparamd >/dev/null
	then
		:
	else
		echo "Note: bootparamd doesn't appear to be running."
		SHOULDREBOOT=Y
	fi

	# modify /etc/nsswitch.conf, if necessary
	if grep "^bootparams:[ 	]*nis[ 	]*\[NOTFOUND=return\]" /etc/nsswitch.conf >/dev/null
	then
		cp /etc/nsswitch.conf /etc/nsswitch.conf-
		awk -F: '
		{
			if ($1 == "bootparams")
				printf("%s: files\n",$1);
			else
				print
		}' < /etc/nsswitch.conf- > /etc/nsswitch.conf

		echo "Note: had to modify /etc/nsswitch.conf for bootparams to be found locally"
		SHOULDREBOOT=Y
	fi

	release_lock
}

#setserver_ethers()
#{
#    ypmatch $NODENAME ethers 2>/dev/null | grep $ETHER > /dev/null || {
#	grep $NODENAME /etc/ethers > /dev/null 2>&1 || \
#	    echo "$ETHER	$NODENAME" >> /etc/ethers
#    }
#}
#
#setserver_hosts()
#{
#    ypmatch $NODENAME hosts 2>/dev/null | grep $IPADDR > /dev/null || {
#	grep $NODENAME /etc/hosts > /dev/null 2>&1 || \
#	    echo "$IPADDR	$NODENAME" >> /etc/hosts
#    }
#}


setserver_bootdir()
{
	acquire_lock $Bootmethod

	[ ! -d $Bootdir ] && {
		mkdir $Bootdir
		chmod 755 $Bootdir
		[ $Bootdir = /tftpboot ] && {
			cd $Bootdir
			ln -s . tftpboot
			SHOULDREBOOT=Y
		}
		echo "Note: had to create the $Bootdir directory"
	}

	inetname=inetboot.${Majorarch}.${CURRENT}
	[ ! -f $Bootdir/$inetname ] && {
		file=$USERLOC/usr/lib/fs/nfs/inetboot
		[ ! -f $file ] && {
			echo "$progname: Can't find inetboot file in cpioarchive!"
			cleanup 1
		}
		cp $file $Bootdir/$inetname
		chmod 755 $Bootdir/$inetname
	}

	release_lock

	if [ $Bootmethod = tftp ]
	then
		hexip=`echo $IPADDR |\
		awk -F. '{
			IP_HEX = sprintf("%0.2x%0.2x%0.2x%0.2x", $1,$2,$3,$4)
			print IP_HEX
		}' | tr '[a-z]' '[A-Z]'`

		hexiparch=${hexip}.`echo ${karch} | tr '[a-z]' '[A-Z]'`

		rm -f /tftpboot/$hexiparch
		ln -s $inetname /tftpboot/$hexiparch

		if grep tftp /etc/inetd.conf >/dev/null
		then
			sed 's/^[ 	]*#[ 	]*tftp/tftp/' /etc/inetd.conf > /tmp/inetd.conf.$$
			diff /etc/inetd.conf /tmp/inetd.conf.$$ >/dev/null
			[ $? -ne 0 ] && {
				cp /tmp/inetd.conf.$$ /etc/inetd.conf
				echo "Note: had to turn on tftp in /etc/inetd.conf"
				SHOULDREBOOT=Y
			}
			rm /tmp/inetd.conf.$$
		else
			echo "******** Can't find tftp in /etc/inetd.conf"
		fi

	else # rpl method

		# hard coded, this will have to be fixed later
		Board=smc

		if [ ! -r ${Bootdir}/${Board}.com.$CURRENT ] 
		then
        		if [ "$archorpkg" = Archive ]
        		then
				#cp ${archmount}/${Board}.com ${Bootdir}/${Board}.com.$CURRENT
				cp /net/dunk/sdet/merge/cpiotemplate_i386/${Board}.com ${Bootdir}/${Board}.com.$CURRENT
			else
				cp ${USERLOC}/usr/lib/fs/nfs/drv.i86pc/${Board}.com ${Bootdir}/${Board}.com.$CURRENT
			fi
		fi
		if [ ! -r ${Bootdir}/gluecode.com.$CURRENT ] 
		then
			if [ "$archorpkg" = Archive ]
                        then
				#cp ${archmount}/gluecode.com ${Bootdir}/gluecode.com.$CURRENT
				cp /net/dunk/sdet/merge/cpiotemplate_i386/gluecode.com ${Bootdir}/gluecode.com.$CURRENT
			else
				cp ${USERLOC}/usr/lib/fs/nfs/drv.i86pc/gluecode.com ${Bootdir}/gluecode.com.$CURRENT
                        fi
                fi


		OLDWD=`pwd`
		cd ${Bootdir}
 
		rm -f ${IPADDR}.hw.com ${IPADDR}.glue.com ${IPADDR}.inetboot
		ln -s ${Board}.com.$CURRENT ${IPADDR}.hw.com
		ln -s gluecode.com.$CURRENT ${IPADDR}.glue.com
		ln -s $inetname ${IPADDR}.inetboot
		cd ${OLDWD}


		if ps -ef | grep "[ /]rpld[ 	]" > /dev/null
		then
			: # found it, no need to start
		else
			[ ! -x /usr/sbin/rpld ] && {
				echo "$progname: rpld not found or executable"
				cleanup 1
			}
			echo "starting rpld"
			/usr/sbin/rpld -a
		fi

	fi

}


########### Client routines #############

setclient_hostinfo()
{
    grep loghost $ROOTLOC/etc/hosts >/dev/null || {
        ed $ROOTLOC/etc/hosts >/dev/null <<EOF
/localhost/s/$/ loghost/
w
q
EOF
    }

    for i in $NETIF
    do
	if expr $i : '.*\:.*' >/dev/null
	then
		ifc=`expr $i : '\(.*\)\:.*'`
		addr=`expr $i : '.*\:\(.*\)'`
	else
		ifc=$i
		addr=$NODENAME
	fi
	echo $addr > $ROOTLOC/etc/hostname.${ifc}

        #ENTRY=`ypmatch $addr hosts`
        ENTRY=`nismatch $addr hosts.org_dir | awk '{printf("%s\t%s\n", $3,$2);}'`
        ENTRY=${ENTRY:-"$IPADDR $NODENAME"}
 
        grep "$ENTRY" $ROOTLOC/etc/hosts >/dev/null || echo $ENTRY >> $ROOTLOC/etc/hosts

    done

    for i in $ROOTLOC/etc/net/t*/hosts
    do
	echo "$NODENAME	$NODENAME" >> $i
    done

    echo $_localhost >> $ROOTLOC/etc/hosts

    [ -f /etc/defaultdomain ] && {
	cp /etc/defaultdomain $ROOTLOC/etc/defaultdomain
    	mkdir ${ROOTLOC}/var/yp/binding/`cat /etc/defaultdomain`
    }
    [ -f /etc/defaultrouter ] && \
	cp /etc/defaultrouter $ROOTLOC/etc/defaultrouter

    cp /etc/netmasks $ROOTLOC/etc/netmasks
    mkdir ${ROOTLOC}/swap
}

setclient_vfstab()
{
    cp $ROOTLOC/etc/vfstab $ROOTLOC/etc/vfstab.orig
    (
	grep -v swap $ROOTLOC/etc/vfstab.orig

	#	device					device		mount		FS	fsck	mount	mount
	#	to mount				to fsck		point		type	pass	at boot	options

	echo "${localhost}:$ROOTLOC			-		/		nfs	-	no	rw"
	echo "${localhost}:$USERLOC/usr			-		/usr		nfs	-	no	ro"

	echo "${localhost}:$SWAPLOC			-		/swap		nfs	-       no	-"
	echo "/swap					-		-		swap	-       no	-"
	[ -n "$SWAPFS" ] && echo "$SWAPFS		-		-		swap	-	-	-"
	[ -n "$OWMNT" ] && echo "$OWMNT 		-		/usr/openwin	nfs	-	yes	-"

	[ -n "$HOMEFS" ]  && {
    	HOMEFS_RAW=`echo $HOMEFS | sed 's/dsk/rdsk/'`
	echo "$HOMEFS					$HOMEFS_RAW	$TESTDIR	ufs	1	yes	rw"
	}

	[ -n "$OPTFS" ] && {
    	OPTFS_RAW=`echo $OPTFS | sed 's/dsk/rdsk/'`
	echo "$OPTFS					$OPTFS_RAW	/opt		ufs	2	yes	rw"
	}

    ) > $ROOTLOC/etc/vfstab

}

setclient_misc()
{
#    ed $ROOTLOC/etc/auto_master >/dev/null <<EOF
#g/^\/home/d
#a
#/home   -null
#.
#w
#q
#EOF

#    echo "set path_to_inst_bootstrap=1" >> $ROOTLOC/etc/system

    serv=/net/`echo $SERV_EXPORT | sed 's/://'`
    [ "$Majorarch" = i386 -a ! -f $ROOTLOC/etc/bootrc ] && \
	cp /builds/scripts/bootrc_template $ROOTLOC/etc/bootrc

    [ -f ${serv}/tools/sysfiles/sysetup ] && \
	cp ${serv}/tools/sysfiles/sysetup  $ROOTLOC/etc/init.d/sysetup

    if [ "$ROUTERDISC" = "no" -o `echo $NETIF | wc -w` -gt 1 ]
    then
	[ -f ${serv}/tools/sysfiles/inetinit ] && {
	   echo "Disabling router discovery"
	   cp ${serv}/tools/sysfiles/inetinit $ROOTLOC/etc/init.d/inetinit
	}
    fi

    [ ! -f $ROOTLOC/etc/dfs/fstypes -a -f ${serv}/tools/sysfiles/fstypes ] && {
	    echo "Copying a default /etc/dfs/fstypes file."
	    cp ${serv}/tools/sysfiles/fstypes $ROOTLOC/etc/dfs/fstypes
    }

}




########################### main() #############################

progname=`basename $0`
#

[ $# -ge 2 -a "$1" = -b ] && {
	BASE=$2
	shift
	shift
}

[ $# -ne 3 ] && {
    echo "usage: $progname [-b serverbase ] karch [machine:/]hostinfo arch-area"
    exit 1
}

karch=$1
hostinfo=$2
archarea=$3

# source the machfile
sourcehostinfo

karch=${ARCH:-$karch}


# this is needed because we need to get the server's network address
# for the network card that the client is attatched to; since some
# servers have more than one network card, just running "uname -n"
# won't cut it.  My solution for now is just to have DISKLESSTEST_SERV
# defined in each machfile.
# The long term solution is to do this the way add_install_client does,
# but this is much quicker and easier, but more work to set up and
# maintain the DISKLESSTEST_SERV variable in machfiles - jme

localhost=${DISKLESSTEST_SERV:-`uname -n`}

#_localhost=`ypmatch $localhost hosts 2>/dev/null`
_localhost=`nismatch $localhost hosts.org_dir | awk '{printf("%s\t%s\n", $3,$2);}'`
[ -z "$_localhost" ] && {
	echo "localhost not defined in yp maps."
	cleanup 1
}

#
# check if the arch area name is of the format machine:nfspart.
# If so, mount it, if not, assume it's mounted
#
echo "Getting arch area."
if [ `expr "$archarea" : '.*\:.*'` -ne 0 ]
then
    archmount=/tmp/arch$$
    mkdir $archmount
    mount $archarea $archmount

    mount|grep $archmount > /dev/null 2>&1 || {
        echo "Couldn't mount archarea $archarea."
        cleanup 1
    }
    archmounted="yes"
else
    archmount=$archarea
fi

trap 'cleanup 1' 0 2 3 9 15

[ ! -d $archmount -o ! -r $archmount ] && {
    echo "$progname: Proto area access point $archmount no good."
    cleanup 1
}


echo "Checking parameters and vars."		; setvars
echo "Creating directories."			; createdirs

						  copydirs	# has its own echo statements

echo "Server: Setting up share information."	; setserver_dfstab
echo "Server: Setting up boot parameters."	; setserver_bootparams
echo "Server: Setting up bootdir (tftp/rpl)."	; setserver_bootdir

echo "Client: Setting host info."		; setclient_hostinfo
echo "Client: Setting vfstab."			; setclient_vfstab
echo "Client: Setting misc stuff."		; setclient_misc

[ -n "$SHOULDREBOOT" ] && echo "\nNOTE: You should reboot the boot server"

cleanup 0

