#!/bin/sh
#ident "@(#)host_example 1.6	97/01/24 SMI"
#
# NOTE: This is only an example script.  It has not been tested or verified.
#	It is ONLY for illustration purposes.  You will need to customize this
#	script to satisfy your specific needed.
# NOTE: Any output produced by this script will be returned and shown to the
#	user after the AutoClient operation is done.
#
# Example 'hostmgr' script which could be used for multiple things.  This
# script is run on the AutoClient server only. The following environmental
# variables (described in the admenviron(5) man page) get passed in along with
# example values.  In general, the format is:
#	ADM_*=org_value[,new_value]
#
# ADM_CLIENT_ARCHITECTURE	{sparc,i386}
# ADM_CLIENT_DISCONN		{Y,N}
# ADM_CLIENT_DISKCONF		{1disk,2disk,.....}
# ADM_CLIENT_ETHERNET_ADDR	<user specified Ethernet address>
# ADM_CLIENT_HOSTNAME		<user specified host name. i.e. aclient1>
# ADM_CLIENT_IP_ADDR		<user specified IP address>
# ADM_CLIENT_MACHINE		<user specified platform. i.e. sun4c>
# ADM_CLIENT_OS_NAME		<user specified OS name. i.e. Solaris>
# ADM_CLIENT_OS_VERSION		<user specified OS version. i.e. 2.5>
# ADM_CLIENT_ROOT		<user specified root path: i.e. /export/root/aclient1>
# ADM_CLIENT_SERVER		<user specified server: i.e. ac_server>
# ADM_CLIENT_SWAPSIZE		<user specified swap size (in Megs): i.e. 64>
# ADM_CLIENT_TIMEZONE		<user specified timezone. i.e. US/Mountain>
# ADM_CLIENT_TYPE		{AUTOCLIENT, STANDALONE, GENERIC}
# ADM_OPERATION			{PREADD,POSTADD,PREMOD,POSTMOD,PREDEL,POSTDEL}
# PATH				/usr/sbin:/usr/bin
#
# If modifying, converting, or deleting a host, the optional 'new_value'
# may be provided for the following.  Example values are shown.
#
# ADM_CLIENT_DISKCONF		1disk,2disk
# ADM_CLIENT_ETHERNET_ADDR	a:b:a:b:a:b,a:b:a:b:a:c
# ADM_CLIENT_SWAPSIZE		32,64
# ADM_CLIENT_TIMEZONE		US/Mountain,US/Central
# ADM_CLIENT_TYPE		AUTOCLIENT,STANDALONE
#
# Note: when converting from an AutoClient to a Standalone the new values
# for many of the variables will be NULL (empty).  For example:
#	ADM_CLIENT_DISKCONF=1disk,
# When converting from a Standalone to an AutoClient the original values will
# be NULL (empty). For example:
#	ADM_CLIENT_DISKCONF=,1disk
#
# If adding a new AutoClient ADM_OPERATION can be PREADD and/or POSTADD.  If
# converting from one machine type to another, or modifying an existing host
# ADM_OPERATION can be PREMOD and/or POSTMOD.  If deleting an AutoClient,
# ADM_OPERATION can be PREDEL and/or POSTDEL.
#
##############################################################################
# The following are just samples on things you might want to do in this
# script.  This script shows how a single script can do everything.  If you
# prefer, you can have one script per operation type.
#

# Parse the value of an environment variable into the original part and the
# new part.
#
parse_variable() {
    ORG=`echo $1 | awk -F, '{ print $1 }'`
    NEW=`echo $1 | awk -F, '{ print $2 }'`
}

# Routine to save the 'state' of a client allowing it to be copied to another
# host.
save_ac_state_information() {
    # This is just a sample of what you might want to do.  Lets say, though,
    # you want to place all saved files in /usr/tmp/<clientname>.
    dstdir=/usr/tmp/$ADM_CLIENT_HOSTNAME
    mkdir $dstdir
    mkdir $dstdir/crontabs
    cp $ADM_CLIENT_ROOT/var/spool/cron/crontabs/* $dstdir/crontabs
    if [ -d $ADM_CLIENT_ROOT/var/spool/calendar ]; then
	mkdir $dstdir/calendar
	cp $ADM_CLIENT_ROOT/var/spool/calendar/* $dstdir/calendar
    fi
}

# Determine if we are creating a new AutoClient.  If ADM_OPERATION is *ADD we
# know we are adding a new AutoClient.  Also, if the new field in
# ADM_CLIENT_TYPE is "AUTOCLIENT", then we know we are adding a new AutoClient
# via the 'convert to' functionality.
#
parse_variable $ADM_CLIENT_TYPE
org_clienttype=$ORG
new_clienttype=$NEW
new_ac=0
if [ "$ADM_OPERATION" = "PREADD" -o \
     "$ADM_OPERATION" = "POSTADD" -o \
     "$new_clienttype" = "AUTOCLIENT" ]; then
    new_ac=1
fi

# Determine if the operation is a PRE or a POST operation
pre=0
post=0
case $ADM_OPERATION in
    PRE*)pre=1;;
    POST*)post=1;;
esac

# Determine the type of operation
add=0
mod=0
del=0
case $ADM_OPERATION in
    *ADD) add=1;;
    *MOD) mod=1;;
    *DEL) del=1;;
esac

# Now decide what needs to be done.
#
if [ $new_ac -eq 1 ]; then
    # We are creating a new AutoClient.  Check to see what the operation is.
    # Remember that this script is being run on the AutoClient server and not
    # on the AutoClient itself.
    if [ $pre -eq 1 ]; then
	# What ever gets put here would get called before the AutoClient gets
	# created.  The types of things that might be done here is setting up
	# of the server to support AutoClients.  For example you might want to
	# add pkg 'X' to your server so your client's have access to it.
	echo "About to add an AutoClient.  Am in the PREADD script."
	exit 0		# Probably want to exit just to be safe.
    fi
    if [ $post -eq 1 ]; then
	# What ever gets put here would get called after the AutoClient gets
	# created.
	#
	# NOTE: This script gets called after the client's root area has been
	# created (and patched) but BEFORE the bootparams map for the client
	# is added.  The types of things that might be done here are:
	#   - editing the client's vfstab to mount site specific file systems
	#   - adding additional pkgs to the client's root area
	#   - sending email to a sysadmin letting them know about the new
	#     client
	#   - any other customizations you want to do to the client's root
	#     before it boots.
	# For example:
	echo "server:/some/path - /some_mount nfs - yes -" >> $ADM_CLIENT_ROOT/etc/vfstab
	echo "About to add an AutoClient.  Am in the POSTADD script."
	exit 0		# Probably want to exit just to be safe.
    fi
    exit 0		# Probably want to exit just to be safe.
fi

# We are not creating a new AutoClient so we are either modifying an existing
# one or deleting one.
if [ $mod -eq 1 ]; then
    # We know we are modifying an AutoClient.  Now this could be a modify of
    # the AutoClient or a convert of an AutoClient to a Standalone.
    if [ "$new_clienttype" = "STANDALONE" ]; then
	# We know we are converting from an AutoClient to a Standalone.
	# Some of the things we may want to do in this case is to save off the
	# users calendar file, crontabs, etc. so they can be reinstalled on
	# the machine after being converted to a standalone.
	save_ac_state_information
	echo "Converting AutoClient to Standalone"
	exit 0		# Probably want to exit just to be safe.
    else
	# So we are just modifying something about the AutoClient.  We may
	# need to parse out the new values from the original values
	parse_variable $ADM_CLIENT_DISKCONF
	org_diskconf=$ORG
	new_diskconf=$NEW
	parse_variable $ADM_CLIENT_ETHERNET_ADDR
	org_ethernet=$ORG
	new_ethernet=$NEW
	parse_variable $ADM_CLIENT_SWAPSIZE
	org_swapsize=$ORG
	new_swapsize=$NEW
	parse_variable $ADM_CLIENT_TIMEZONE
	org_timezone=$ORG
	new_timezone=$NEW
	# Now you can do just about anything you want to to the client.  For
	# example let's say you want to make sure the client has a certain pkg
	# installed.  In this case you might do something like:
	pkginfo -q -R $ADM_CLIENT_ROOT SUNWfubar
	if [ $? -eq 1 ]; then
	    pkgdir=/some/directory/where/the/pkg/can/be/found
	    pkgadd -R $ADM_CLIENT_ROOT -d $pkgdir SUNWfubar
	fi
	echo "Modifying the AutoClient"
	exit 0		# Probably want to exit just to be safe.
    fi
    exit 0		# Probably want to exit just to be safe.
fi

if [ $del -eq 1 ]; then
    # We know we are deleting the AutoClient.
    # Some of the things we may want to do in this case is to save off the
    # users calendar file, crontabs, etc. so they can be reinstalled on
    # whatever ends up being the users new machine.
    save_ac_state_information
    echo "Deleting AutoClient"
    exit 0		# Probably want to exit just to be safe.
fi
