#!/bin/sh

# 	@(#) install_explorer_plugin.src 1.7@(#) 04/21/05 10:28:53

# install embedded script as explorer plug-in, for current explorer version

# Explorer will refuse to execute a plugin which does not have the
# correct EXP_VERSION. FInd out what version is installed and
# embed that into the sneep plugin.

explodetails=`pkginfo -l SUNWexplo 2> /dev/null`
if test $? -ne 0 ; then
	echo explorer does not seem to be installed. 1>&2
	exit 1
fi

# Get BASEDIR from explorer package details
ebase=`echo "$explodetails" | awk '/BASEDIR:/ {print $2}' `

#	explore base has changed from /opt/SUNWexplo to /opt. Adapt.
test -d $ebase/SUNWexplo && ebase=$ebase/SUNWexplo

ecommon=$ebase/lib/exp_common	# file containing explorer version setting
if test ! -f $ecommon ; then
	
	echo Cannot determine explorer version from $ecommon 1>&2
	exit 1
fi

eversion=`sed -n 's/^EXP_VERSION=//p' $ecommon`		# get explorer version

# install the plugin in the tools directory
plugin=$ebase/tools/chassis_serial	# the plugin name and location

# if plugin already exists, see if it is already configured for 
# this version of explorer.  If so, do not change it, because
# if may have been delivered as part of explorer and not by this script.

if test -r $plugin ; then
	pversion=`sed -n 's/^EXP_VERSION=//p' $plugin`	# get plugin version
	if test "$pversion" = "$eversion" ; then
		echo $plugin is up to date: no change 1>&2
		exit 0
	fi
fi

# plugin is missing or for another version of explorer. Update it.

rm -f $plugin		# remove any previous sneep plugin
touch $plugin 2> /dev/null 
if test $? -ne 0 ; then
	echo Cannot create plugin $plugin 1>&2
	exit 1
fi

# create plugin from the embedded source here,
# substituting in the proper explorer version.
# Do it in two pieces -one with substitutions, one without.


# first section uses substitutions to set EXP_VERSION

cat > $plugin << END_PLUGIN_SECTION
#!/bin/ksh -p
#
#  Copyright 1996-2005 Sun Microsystems, Inc. All rights reserved.
#
#
# Explorer Version
EXP_VERSION=$eversion
#
# This explorer plug-in is created by 
#	/opt/SUNWsneep/bin/install_explorer_plugin
# which can be used to update EXP_VERSION if explorer refuses to
# run this script after being updated.

END_PLUGIN_SECTION


# remainder is done without substitutions to preserve script intact
# quoting any part of  END_PLUGIN_SECTION prevents substitutions

cat >> $plugin << \END_PLUGIN_SECTION

# File for Chassis Serial Number in explorer output
csnfile=sysconfig/chassis_serial
#
# Source tools functions
. ${EXP_LIB}/exp_tools

# Run always: skip which_gate used in most explorer plugins
script=${0##*/}
# which_gate_exit $script default all


# collect serial using SUNWsneep if it is installed,
# otherwise see if the SUNWsneep previously was used to 
# save the serial in the eeprom nvramrc.

if sneepdir=`pkginfo -r SUNWsneep 2> /dev/null ` ; then
 # get Chassis Serial Number using "SUNWsneep" if possible
 get_cmd "$sneepdir/bin/sneep"         $csnfile

else
 # If SUNWsneep is not installed,
 # look in system eeprom for CSN in SUNWsneep format
 entry=` /usr/sbin/eeprom nvramrc 2> /dev/null \
	| awk '/ChassisSerialNumber/ {print $3}' `

 test -z "$entry" && entry="unknown"	# provide default value if not found
 get_cmd "/usr/bin/echo $entry" 	$csnfile
fi

END_PLUGIN_SECTION

test -f $plugin && chmod ugo=rx $plugin		# set script permissions
