#! /usr/bin/sh
#
# ident "@(#)es-details 1.9     03/01/14 SMI"
#
# Copyright 2002-2003 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#

# Globals
#
PROGNAME=`/bin/basename $0`; export PROGNAME

# To set the $ESROOT environment variable
#

BASEDIR=`/usr/bin/pkgparam SUNWescom BASEDIR`
SCRIPT_DIR=${BASEDIR}/SUNWsymon/sbin

. ${SCRIPT_DIR}/es-common.sh

set_basedir

# Needed directories
#
VARDIR="/var/opt/SUNWsymon"
FAMDIR="$VARDIR/cfg"

##############################################################################
#
# General library of functions
#
##############################################################################

# subfunction: println
# inputs:
#    message to display
# outputs: none
#
println ()
{
   eval /bin/echo `/usr/bin/gettext "$1"`
}

# subfunction: errmsg
# inputs:
#    error message
# outputs: none
#
errmsg ()
{
   # Print out the error message.
   #
   /bin/gettext "ERROR: "
   eval /bin/echo `/usr/bin/gettext "$1"`
   /bin/echo ""
}


# subfunction: print_usage
# inputs : none
# outputs: none
#
print_usage ()
{
    # Print usage for this command.
    #
    /bin/echo ""
    println 'Usage: $2 [ -a ] [ -f inputfile ] -n Node_Object_Type [ -o ] [ -u ]\n' "$PROGNAME"
    /bin/gettext '  Specify the tabs for a given Node_Object_Type.  The -f and -u\n  options are mutually exclusive.\n  where the options represent:\n'
    /bin/gettext '    a                  : All tabs provided by Sun Management Center\n                         will be included.\n'
    /bin/gettext '    f inputfile        : Input file which contains list of tabs to\n                         be used for the specified Node_Object_Type.\n'
    /bin/gettext '    n Node_Object_Type : Corresponds to the Node_Object_Type\n                         entered in the es-device script.\n'
    /bin/gettext '    o                  : Overwrite previously created tab file.\n                         Only meaningful when used with the -f\n                         option.\n'
    /bin/gettext '    u                  : Undo.  Restores the Node_Object_Type\n                         to its original state.\n'
    /bin/echo ""
    exit 1
}

# subfunction: clean_input
# inputs: string
# output: string with leading & trailing spaces removed
#
clean_input()
{
   # Use the awk gsub command to strip out leading/trailing blanks.
   #
   /bin/echo "$1" | /bin/nawk ' \
       BEGIN { FS="\n"} \
       { \
          gsub("^[ \t]*", "", $0); \
          gsub("[ \t]*$", "", $0); \
          print $0 \
       }'
}

# subfunction: rmline
# inputs :
#    filename
#    line number to delete in file
# outputs: none
#
rmline()
{
   rl_file="$1"
   rl_line="$2"

   rl_temp=$rl_line","$rl_line"d"

   /bin/ed $rl_file << end > /dev/null 2>&1
$rl_temp
w
q
end
}

# subfunction: addline
# inputs : 
#   filename
#   line number to add text in file
#   text to add
# outputs: none
#
addline()
{
   al_file="$1"
   al_line="$2"
   al_text="$3"

   al_temp=$al_line"i"

   /bin/ed $al_file << end > /dev/null 2>&1
$al_temp
$al_text
.
w
q
end
}

#############################################################################
#
# Start of the main program
#
#############################################################################

# The following environment variable is needed to run the es-chelp script
#
if [ -z "$ESROOT" ]
then
    /bin/gettext "ESROOT must be set" >&2
    exit 1
fi

# Init flags
#
overwrite=0
undo=0
argcount=0
all_include=0
val_err=0
usage_err=0

# Handle input arguments.  Call clean_input to remove leading/trailing
# spaces on arguments.
#
while getopts af:n:ou OPT
do
    case $OPT in
        a)
            all_include=1
            ;;
        f)
            infile=`clean_input "$OPTARG"`
            if [ ! -f "$infile" ]
            then
               errmsg 'Input file $2 does not exist' "$infile"
               usage_err=1
            fi

            argcount=`/bin/expr $argcount + 1`
            ;;
        n)
            node_object_type=`clean_input "$OPTARG"`
            ;;
        o)
            overwrite=1
            ;;
        u)
            undo=1
            argcount=`/bin/expr $argcount + 1`
            ;;
    esac
done

# Usage checks.
#
if [ "$node_object_type" = "" ]
then
   errmsg "The Node_Object_Type must be specified."
   usage_err=1
fi

if [ $argcount -eq 0 ]
then
   errmsg "Either option -f or -u must be specified."
   usage_err=1
fi

if [ $argcount -gt 1 ]
then
   errmsg "Options -f or -u cannot both be specified."
   usage_err=1
fi

if [ $overwrite -eq 1 -a $undo -eq 1 ]
then
   errmsg "Options -o and -u cannot both be specified."
   usage_err=1
fi

if [ $all_include -eq 1 -a $undo -eq 1 ]
then
   errmsg "Options -a and -u cannot both be specified."
   usage_err=1
fi
   
if [ $usage_err -eq 1 ]
then
   print_usage
fi

# Verify the user is root.  Error if otherwise.
#
check_root

# Set output files.
#
outfilename="${node_object_type}-tabs-j.x"
outfile="$FAMDIR/${node_object_type}-tabs-j.x"
familyfile="$FAMDIR/${node_object_type}-family-j.x"

# Handle undo option
#
if [ $undo -eq 1 ]
then
   if [ ! -f ${familyfile}.bak ]
   then
      errmsg 'es-details has not yet been run with Node_Object_Type $2.' "$node_object_type"
      /bin/echo ""
      exit 1
   else
      # Back out the changes and exit.
      #
      `/bin/mv "${familyfile}.bak" "${familyfile}"`
      `/bin/rm -f "$outfile"`
      exit 0
   fi
fi

# Set default tab to the Info tab.
#
default_key="details-info-help"

# Look up DEFAULT tab value in the inputfile, if any.
#
default_cnt=0
default_present=0
user_default=0
default_selected=0

ln=1
num_lines=`/bin/grep -c ".*" $infile`
while [ $ln -le $num_lines ]
do

   line=`/bin/sed -n -e ${ln},${ln}p $infile`
   key=`/bin/echo     $line | /bin/awk '{ print $1 }'`

   # Covert key to uppercase.
   #
   uc_key=`/bin/echo ${key} | tr '[a-z]'  '[A-Z]' `

   if [ "$uc_key" = "DEFAULT" ] 
   then

      # Count number of DEFAULT keys.  It is an error to have more than one.
      #
      default_cnt=`/bin/expr $default_cnt + 1`

      tabname=`/bin/echo $line | /bin/awk '{ print $2 }'`
      uc_tabname=`/bin/echo ${tabname} | tr '[a-z]'  '[A-Z]' `

      # Validate DEFAULT statement. 
      # tabname validation
      #
      if [ "$tabname" = "" ]
      then
          errmsg 'Tab name not specified for DEFAULT statement on line $2.' "$ln"
          val_err=1
      else

         # Keep track of the tabname selected since we must verify later that
         # this tab is actually in the input file.
         #
         default_tab="$uc_tabname"
         default_selected=1

         case $uc_tabname in

            BROWSER)
               default_key="details-object-browser-help"
               ;;

            ALARMS)
               default_key="details-alarm-manager-help"
               ;;

            MODULES)
               default_key="details-modules-help"
               ;;

            VIEWLOG)
               default_key="details-log-viewer-help"
               ;;

            APPLICATIONS)
               default_key="details-plugin-help"
               ;;

            HARDWARE)
               default_key="details-configuration-help"
               ;;

            *)

               # The default tab must be a user-defined tab.
               # Since the default_key must be set to the
               # help key, we will set the default_key when
               # the APPEND statement is processed.
               #
               user_default=1
         esac

         # If the -a option was selected, and it is not a user-defined
         # tab, then the default tab is present.
         #
         if [ $user_default -eq 0 -a $all_include -eq 1 ]
         then
            default_present=1
         fi
      fi
   fi 

   ln=`/bin/expr $ln + 1`
done

# Error if user specified more than one DEFAULT tab.
#
if [ "$default_cnt" -gt 1 ] 
then
   errmsg "Only one tab can be defined as the DEFAULT."
   val_err=1
fi

# Initialize INCLUDE flags
#
if [ $all_include -eq 0 ] 
then
   tab_browser=0
   tab_alarms=0
   tab_modules=0
   tab_viewlog=0
   tab_applications=0
   tab_hardware=0
else
   # The -a option indicates that all Sun Management Center tabs will
   # be included, so just set the flags to true.
   #
   tab_browser=1
   tab_alarms=1
   tab_modules=1
   tab_viewlog=1
   tab_applications=1
   tab_hardware=1
fi

# Validate input file.
#
tab_cnt=0

ln=1
num_lines=`/bin/grep -c ".*" $infile`
while [ $ln -le $num_lines ]
do

   line=`/bin/sed -n -e ${ln},${ln}p $infile`
   key=`/bin/echo     $line | /bin/awk '{ print $1 }'`

   # Covert key to uppercase.
   #
   uc_key=`/bin/echo ${key} | tr '[a-z]'  '[A-Z]' `

   case ${uc_key} in

      # Skip over comments, blank lines, and DEFAULT statements (which
      # were already processed above).
      #

      \#*) ;;

      "") ;;

      DEFAULT) ;;

      INCLUDE)

         # Error if the INCLUDE statement has less than 2 fields.
         #
         testline=`/bin/echo "$line" | /bin/awk '{ if (NF >= 2) print 1 }'`
         if [ "$testline" -ne 1 ]
         then
            errmsg 'Tab name not specified for INCLUDE statement on line $2.' "$ln"
            val_err=1
         fi

         # The for loop allows the user to specify more than one tab on the
         # same INCLUDE statement.
         #
         fld_cnt=0
         for include_key in $line
         do

            # fld_cnt is used to skip the first field, which must be the
            # INCLUDE keyword.
            #
            fld_cnt=`/bin/expr $fld_cnt + 1`

            if [ $fld_cnt -eq 1 ]
            then

               # Skip over INCLUDE statement.
               #
               continue
            fi

            tab_cnt=`/bin/expr $tab_cnt + 1`
            uc_tabname=`/bin/echo ${include_key} | tr '[a-z]'  '[A-Z]' `

            # Validate that the specified default tab is present in an
            # INCLUDE statement.
            #
            if [ $user_default -eq 0 -a "$default_tab" = "$uc_tabname" ]
            then
               default_present=1
            fi

            # Set appropriate flags.
            #
            case $uc_tabname in

               BROWSER)
                  tab_browser=1
                  ;;

               ALARMS)
                  tab_alarms=1
                  ;;

               MODULES)
                  tab_modules=1
                  ;;

               VIEWLOG)
                  tab_viewlog=1
                  ;;

               APPLICATIONS)
                  tab_applications=1
                  ;;
 
               HARDWARE)
                  tab_hardware=1
                  ;;

               *)
                  errmsg '$2 is not a valid Sun Management Center tab on line $3.' "$include_key" "$ln"
                  val_err=1
                  ;;

            esac
         done
         ;;

      APPEND)

         tab_cnt=`/bin/expr $tab_cnt + 1`
         tabname=`/bin/echo $line | /bin/awk '{ print $2 }'`
         uc_tabname=`/bin/echo ${tabname} | tr '[a-z]'  '[A-Z]' `
         helpval=`/bin/echo $line | /bin/awk '{ print $3 }'`
         java=`/bin/echo    $line | /bin/awk '{ for (n=4; n <= NF; n++) { printf "%s ", $n } }'`

         # Validate APPEND statement. 
         # tabname validation
         #
         if [ "$tabname" = "" ]
         then
             errmsg 'Tab name not specified for APPEND statement on line $2.' "$ln"
             val_err=1
         else
            testtab=`/bin/echo "$tabname" | /bin/awk \
               'BEGIN { FS=":" } { if (NF == 2 && length($1) > 0 && length($2) > 0) print 1 }'`
            if [ "$testtab" -ne 1 ]
            then
               errmsg 'Tab name $2 must be in format file:key on line $3.' "$tabname" "$ln"
               val_err=1
            fi
         fi

         # helpval validation
         #
         if [ "$helpval" = "" ]
         then
             errmsg 'Help key not specified for user-defined tab $2 on line $3.' "$tabname" "$ln"
             val_err=1
         else
            testkey=`/bin/echo "$helpval" | /bin/awk \
               'BEGIN { FS=":" } { if (NF == 2 && length($1) > 0 && length($2) > 0) print 1 }'`
            if [ "$testkey" -ne 1 ]
            then
               errmsg 'Help Key $2 must be in format key:file on line $3.' "$helpval" "$ln"
               val_err=1
            fi
         fi
         
         # java validation
         #
         if [ "$java" = "" ]
         then
             errmsg 'Java class not specified for user-defined tab $2 on line $3.' "$tabname" "$ln"
             val_err=1
         fi

         # Validate that the specified default tab is present in an ADD
         # statement.
         #
         if [ $user_default -eq 1 -a "$default_tab" = "$uc_tabname" ]
         then
            helpkey=`/bin/echo "$helpval" | /bin/awk 'BEGIN { FS=":" } { print $1 }'`
            default_key="$helpkey"
            default_present=1
         fi
         ;;

      *)
         # Invalid key
         #
         errmsg 'Key $2 is not a valid key on line $3.' "$key" "$ln"
         val_err=1
         ;;

   esac

   ln=`/bin/expr $ln + 1`
done

# Error if there were no tabs specified in the inputfile.
#
if [ "$tab_cnt" -eq 0 ]
then
   errmsg 'Input file $2 contains no APPEND or INCLUDE statements' "$infile"
   val_err=1
fi

if [ $default_selected -eq 1 -a $default_present -eq 0 ]
then
   errmsg 'The specified default tab $2 was not found in the inputfile.' "$default_tab"
   val_err=1
fi

# Verify that the specified instance name is valid.
#
if [ ! -f "$familyfile" ]
then
   errmsg '$2 is not a valid Node_Object_Type.' "$node_object_type"
   val_err=1
fi

# Exit if there were any validation problems.
#
if [ $val_err -eq 1 ]
then
   exit 1
fi

# Verify specified tab file does not exist, or that the -o option was used.
#
if [ -f "$outfile" ]
then
   if [ $overwrite -eq 1 ]
   then
      /bin/rm $outfile
   else
      errmsg "The tabs for this object have already been specified, use -o option to overwrite."
      /bin/echo ""
      exit 1
   fi
fi

# Check the family file and verify that it is for a Sun Management Center
# agent/module.  This means that the specified family file must have the
# %windowID value in the commandSpec() and navigateCommand lines set as
# per the following matrix:
#
# --------------------------------------------------------------------
# ----      ------------------       -------------------
# Type      commandSpec format       Value for %windowID
# ----      ------------------       -------------------
# --------------------------------------------------------------------
# host      commandSpec(hostdetail)  tools-hostdetails-j.x
# platform  commandSpec(hostdetail)  tools-hostdetails-j.x
# module    commandSpec(moddetail)   .templates.consoles.server-agent-details
# proxy     commandSpec(moddetail)   .templates.consoles.server-agent-details
# --------------------------------------------------------------------
#
# We are guaranteed that the commandSpec and navigateCommand lines will
# be in the family file if it was created with the es-device script.
#
# The prev_details_string is initially set to "tools-hostdetails-j.x"
# assuming that we are searching for the agent type. If the serach is
# not successful, then we search for a module type, and make the value
# of prev_details_string as ".templates.consoles.server-agent-details".
#
update_family=1

prev_details_string="tools-hostdetails-j.x"

cmdSpcLine=`/bin/grep -n "^commandSpec(hostdetail)" "$familyfile" | /bin/grep hostdetail`
navCmdLine=`/bin/grep -n "^navigateCommand" "$familyfile"`

if [ "$cmdSpcLine" -eq "" ]
then
    cmdSpcLine=`/bin/grep -n "^commandSpec(moddetail)" "$familyfile" | /bin/grep moddetail`
    prev_details_string=".templates.consoles.server-agent-details"
fi

if [ "$cmdSpcLine" -eq "" -o "$navCmdLine" -eq "" ]
then
   errmsg "Can only modify the family file for Sun Management Center agents and modules."
   /bin/echo ""
   exit 1
else

   # Reform the commandSpec(hostdetail) line
   #
   cs_modify_line_num=`/bin/echo "$cmdSpcLine" | /bin/awk 'BEGIN { FS=":" } { print $1 }'`
   cs_modify_line=`/bin/echo     "$cmdSpcLine" | /bin/awk 'BEGIN { FS=":" } { print $2 }'`

   cs_modify_field_num=`/bin/echo "$cs_modify_line" | /bin/awk ' \
      { \
         for (n=1; n <= NF; n++) { \
            if ( $n == "%windowID" ) { \
               printf "%d", n + 1; \
               break; \
            } \
         } \
      }'`

   cs_modify_field=`/bin/echo $cs_modify_line | /bin/awk '{ print $'$cs_modify_field_num' }'`

   # Reform the navigateCommand line
   #
   nc_modify_line_num=`/bin/echo "$navCmdLine" | /bin/awk 'BEGIN { FS=":" } { print $1 }'`
   nc_modify_line=`/bin/echo     "$navCmdLine" | /bin/awk 'BEGIN { FS=":" } { print $2 }'`

   nc_modify_field_num=`/bin/echo "$nc_modify_line" | /bin/awk ' \
      { \
         for (n=1; n <= NF; n++) { \
            if ( $n == "%windowID" ) { \
               printf "%d", n + 1; \
               break; \
            } \
         } \
      }'`

   nc_modify_field=`/bin/echo $nc_modify_line | /bin/awk '{ print $'$nc_modify_field_num' }'`

   if [ "$cs_modify_field" = "$outfilename" -a \
        "$nc_modify_field" = "$outfilename" ]
   then

      # If the family file is already pointing to the $outfilename, then this
      # means that the es-details script has been run before, so no need to
      # update the family file again.
      #
      update_family=0
   else
      if [ "$cs_modify_field" = "$prev_details_string" -a \
           "$nc_modify_field" = "$prev_details_string" ]
      then

         # Replace field specified in modify_field_num with the newly created x-file name.
         #
         nc_update_line=`/bin/echo "$nc_modify_line" | \
            /bin/awk '{ $'$nc_modify_field_num'="'$outfilename'"; print $0 }'`

         # Replace field specified in modify_field_num with the newly created x-file name.
         #
         cs_update_line=`/bin/echo "$cs_modify_line" | \
            /bin/awk '{ $'$cs_modify_field_num'="'$outfilename'"; print $0 }'`

      else
         errmsg "Can only modify the family file for Sun Management Center agents."
         /bin/echo ""
         exit 1
      fi
   fi
fi

# Now all validation is done.  Start creating the two output files.
#

# TAB OUTPUT FILE
#

# Build tab x-file header.
# Note that by inheriting from basedetail that the INFO tab gets
# included automatically.
#
/bin/echo "[ inherit .templates.consoles.basedetail ]" >> $outfile
/bin/echo "" >> $outfile
/bin/echo "consoleParam:preferredTab = $default_key" >> $outfile
/bin/echo "" >> $outfile
/bin/echo "tabset = {" >> $outfile
/bin/echo "" >> $outfile
 
# Now process the requested INCLUDE tabs.  Note that they must be added in this
# order: BROWSER, ALARMS, MODULES, VIEWLOG, APPLICATIONS, and HARDWARE.
#
if [ $tab_browser -eq 1 ]
then
   /bin/echo "    [ load details-tab-browser-j.x ]" >> $outfile

   /bin/echo "" >> $outfile
   /bin/echo "    # Note that this set is predefined in the details-tab-browser-j.x file" >> $outfile
   /bin/echo "    # This is necessary so that the [ Another Details... ] feature works properly." >> $outfile
   /bin/echo "    #" >> $outfile
   /bin/echo "    browser = {" >> $outfile
   /bin/echo "        views = {" >> $outfile
   /bin/echo "            hierarchyView = {" >> $outfile
   /bin/echo "                popup-root = {" >> $outfile
   /bin/echo "                    another = {" >> $outfile
   /bin/echo "                        actionCommand = launchFrame %windowID $outfilename nodeName=%nodeName topologyDescUrl=%topologyDescUrl agentHost=%targetHost agentPort=%targetPort objectUrl=%targetUrl contextUrl=%contextUrl modName=%modName" >> $outfile
   /bin/echo "                    }" >> $outfile
   /bin/echo "                }" >> $outfile
   /bin/echo "            }" >> $outfile
   /bin/echo "        }" >> $outfile
   /bin/echo "    }" >> $outfile
   /bin/echo "" >> $outfile
fi

if [ $tab_alarms -eq 1 ]
then
   /bin/echo "    [ load details-tab-alarms-j.x ]" >> $outfile
fi
if [ $tab_modules -eq 1 ]
then
   /bin/echo "    [ load details-tab-modules-j.x ]" >> $outfile
fi
if [ $tab_viewlog -eq 1 ]
then
   /bin/echo "    [ load details-tab-viewlog-j.x ]" >> $outfile
fi
if [ $tab_applications -eq 1 ]
then
   /bin/echo "    [ load details-tab-applications-j.x ]" >> $outfile
fi
if [ $tab_hardware -eq 1 ]
then
   /bin/echo "    [ load details-tab-hardware-j.x ]" >> $outfile
fi

# Process the user-defined tabs defined in APPEND statements last.
#
ln=1
while [ $ln -le $num_lines ]
do

   line=`/bin/sed -n -e ${ln},${ln}p $infile`
   key=`/bin/echo     $line | /bin/awk '{ print $1 }'`

   # Covert key to uppercase.
   #
   uc_key=`/bin/echo ${key} | tr '[a-z]'  '[A-Z]' `

   case ${uc_key} in

      APPEND)

         tabname=`/bin/echo $line | /bin/awk '{ print $2 }'`
         helpval=`/bin/echo $line | /bin/awk '{ print $3 }'`
         java=`/bin/echo    $line | /bin/awk '{ for (n=4; n <= NF; n++) { printf "%s ", $n } }'`

         # Process help key.
         #
         helpkey=`/bin/echo "$helpval" | /bin/awk 'BEGIN { FS=":" } { print $1 }'`
         helpref=`/bin/echo "$helpval" | /bin/awk 'BEGIN { FS=":" } { print $2 }'`

         # Call es-chelp with the "-d" option to remove any existing key with that
         # value.  This insures the key doesn't get added more than once.
         #
         $ESROOT/sbin/es-chelp -d $helpkey

         # Now add the key with the "-a" option.
         #
         $ESROOT/sbin/es-chelp -a $helpkey $helpref

         # Process internationalized tab name.
         #
         tabkey=`/bin/echo "$tabname" | /bin/awk 'BEGIN { FS=":" } { print $2  }'`
         tabref=`/bin/echo "$tabkey"  | /bin/awk 'BEGIN { FS="." } { print $NF }'`

         # Build the outfile.
         #
         /bin/echo "" >> $outfile
         /bin/echo "    $tabref = { [ use TAB-ENTRY ]" >> $outfile
         /bin/echo "        res:label  = $tabname" >> $outfile
         /bin/echo "        res:tabKey = $helpkey" >> $outfile
         /bin/echo "" >> $outfile

         # The following four lines allow the startRequest() and stopRequest()
         # methods in SMApp/SmAppAdaptor to work.
         #
         /bin/echo "        tabActivatedService   = viewer" >> $outfile
         /bin/echo "        tabActivatedCommand   = startRequest" >> $outfile
         /bin/echo "        tabDeactivatedService = viewer" >> $outfile
         /bin/echo "        tabDeactivatedCommand = stopRequest" >> $outfile

         /bin/echo "" >> $outfile
         /bin/echo "        viewer     = { [ use COMPONENT ]" >> $outfile
         /bin/echo "            awx:component = $java" >> $outfile
         /bin/echo "" >> $outfile
         /bin/echo "            res:datasource = .services.client" >> $outfile
         /bin/echo "            resType:datasource = clientapi" >> $outfile
         /bin/echo "            resMap:datasource = setRawDataRequestHandle" >> $outfile
         /bin/echo "" >> $outfile
         /bin/echo "            res:host = %agentHost" >> $outfile
         /bin/echo "            resMap:host = setAgentHost" >> $outfile
         /bin/echo "" >> $outfile
         /bin/echo "            res:port = %agentPort" >> $outfile
         /bin/echo "            resType:port = int" >> $outfile
         /bin/echo "            resMap:port = setAgentPort" >> $outfile
         /bin/echo "" >> $outfile
         /bin/echo "            res:ourl = %objectUrl" >> $outfile
         /bin/echo "            resMap:ourl = setObjectUrl" >> $outfile
         /bin/echo "" >> $outfile
         /bin/echo "            res:stat = statusMsg" >> $outfile
         /bin/echo "            resType:stat = beanobject" >> $outfile
         /bin/echo "            resMap:stat = setWindowStatusField" >> $outfile
         /bin/echo "" >> $outfile
         /bin/echo "            awx:services = activate destruct" >> $outfile
         /bin/echo "            activateService = _self" >> $outfile
         /bin/echo "            activateCommand = init" >> $outfile
         /bin/echo "            destructService = _self" >> $outfile
         /bin/echo "            destructCommand = destructService" >> $outfile
         /bin/echo "" >> $outfile
         /bin/echo "        }" >> $outfile
         /bin/echo "    }" >> $outfile
         ;;

      *) ;;

   esac

   ln=`/bin/expr $ln + 1`
done

/bin/echo "}" >> $outfile
/bin/echo "" >> $outfile

# FAMILY OUTPUT FILE
#

# Only update the family file if necessary.
#
if [ $update_family -eq 1 ]
then
   # Make a backup copy of the family file if one does not exist.  This way
   # the backup is only created the first time es-details is called.
   #
   if [ ! -f ${familyfile}.bak ]
   then
      `/bin/cp ${familyfile} ${familyfile}.bak`
   fi

   # Update the family file to include the new tab x-file.
   # Replace line number nc_modify_line_num with the nc_update_line
   #
   addline "$familyfile" "$nc_modify_line_num" "$nc_update_line"

   nc_modify_line_num=`/bin/expr $nc_modify_line_num + 1`
   rmline "$familyfile" "$nc_modify_line_num"

   # Replace line number cs_modify_line_num with the cs_update_line
   #
   addline "$familyfile" "$cs_modify_line_num" "$cs_update_line"

   cs_modify_line_num=`/bin/expr $cs_modify_line_num + 1`
   rmline "$familyfile" "$cs_modify_line_num"
fi
