#!/bin/sh
#
#
#    Copyright (c) 1996-2000 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#
# NAME
#      burninscript - set diagnostics post/burnin scripts
# 
# SYNOPSIS
#      burninscript type event [ path | -show ]
# 
# AVAILABILITY
#      admin
# 
# DESCRIPTION
#      This  command  selects the scripts to be run by the diagnos-
#      tics daemon for various events.  When  the  specified  event
#      occurs  for the specified board type then the script will be
#      run. The mode becomes active as soon as this command is exe-
#      cuted so that it does not require a reboot to take effect.
# 
# OPTIONS
#      type      Specify  board  type.  Possible values include CP,
#                GP, WAN, and SWITCH.   For  normal  operations  on
#                switch blades, SWITCH should be specified.
# 
#      event     Specify  event  type.   Valid  settings are POST1,
#                POST2 and BURNIN.
# 
#      path      Specify path name for  script  to  run.   If  this
#                parameter  is  ommited,  then  the current setting
#                will be displayed.
# 
#      -show     The current setting will be displayed.
# 
# EXAMPLES
#      > burninscript SWITCH POST1 -show
#      POST1 script for SWITCH boards is switchpost1.sh.
# 
# SEE ALSO
#      burninname(1d), burninlevel(1d)
# 

#
#
# Load library -- must be first.
#
home="/fabos/share"
util="diagcommon.sh"
ok=0
for f in "./$util" "$FABOSHOME/share/$util" "$home/$util" ; do
	if [ -r $f ] ; then
		. $f
		ok=1
		break;
	fi
done
if [ $ok -ne 1 ] ; then
	echo "Error -- could not locate $util"
	exit 3
fi


#
# Program customization
#

config_string='diag.script.$typ.$event' # config string to update
config_name='$event script for $typ boards'	# User name of config.
config_default="-none-"		# default value
config_mode=$STRING		# config mode
syntax="`/bin/basename $0` type event [ path \| -show ]" ; export syntax

#
# burninscript()
#

if [ $# -lt 2 ] ; then err "Missing parameters" ; fi
typ=$1
event=$2

config_string=`eval echo $config_string`
config_name=`eval echo $config_name`
checkForShow "$config_name" "$config_string" "$config_mode" "$config_default" "$3"

#
# Make sure file exists
#
ok=0
for f in "$3" "$FABOSHOME/share/$3" "$home/$3" ; do
	if [ -x $f ] ; then
		ok=1
		break;
	fi
done

if [ $ok = 0 ] ; then
	err "$3 is not executable"
	exit 3
fi


#
# Make sure this is good setting
#
ok=`eval testConfig $config_string $config_mode`
if [ $? != 0 ] ; then exit 3 ; fi
if [ $ok = 0 ] ; then
	echo "$config_string is not in database."
	echo "Are you sure [y/n d:n] ?"
	read ans
	if [ "$ans" != "y" ] ; then exit 3 ; fi
fi
cur_val=`getConfig $config_string $config_mode $config_default`
if [ "$cur_val" != "$f" ]
then
	setConfig $config_string $config_mode $f
	echo $config_name is now $f
	updateConfig
else
	echo $config_name is still $f
fi
exit 0
