#! /bin/sh
# Copyright 03/31/00 Sun Microsystems, Inc. All Rights Reserved.
#
# pragma ident  "@(#)es-device	1.4  00/03/31 Sun Microsystems"
#

# set -x

#
# This file contains the method to read from file and validate data
# It also contains the validate functions
#

# subfunction: print_usage
# inputs : none
# outputs: none
#
print_usage ()
{
    /bin/echo ""
    eval /bin/echo `/usr/bin/gettext 'Usage: $PROGNAME -ad Filename'`
    /usr/bin/gettext "   where the options represent:\n"
    /usr/bin/gettext "     a : Add new device\n"
    /usr/bin/gettext "(for example: ./es-device -a /tmp/userdata)\n\n"
    /usr/bin/gettext "     d : Delete device\n"
    /usr/bin/gettext "(for example: ./es-device -d /tmp/userdata)\n\n"

}

check_root() {
    ME=`/usr/ucb/whoami`

    [ "$ME" = "root" ] && return 0

    echo ""
    /usr/bin/gettext "This action is limited to superuser only.\n"
    eval echo `/usr/bin/gettext 'Exiting $PROGNAME.'`
    echo ""
    /usr/bin/sleep 2
    exit 1

}

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

# subfunction: findline
# inputs : 
# outputs: 
#
findline()
{
   fl_file=$1
   fl_col=$2
   fl_key=$3

   if [ ! -f $fl_file ]
   then
      fl_val=-1
   else
      fl_val=`/bin/nawk -F "$4" 'BEGIN { ln = -1 } \
              { if ($'$fl_col' == "'$fl_key'") ln = NR } \
              END { print ln }' $fl_file`
   fi

   /bin/echo "$fl_val"
}

# subfunction: rmline
# inputs : 
# outputs: 
#
rmline()
{
   rl_file=$1
   rl_line=$2

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

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

# subfunction: create_node_family_file
# inputs : 
#    object type
#    family file name
#    large icon file name
#    small icon file name
#    username
# outputs: none
#
create_node_family_file()
{
   cff_Node_object_type=$1
   cff_Family_file_name=$2
   cff_Large_icon_file=$3
   cff_Small_icon_file=$4
   cff_Username=$5
   cff_outfile=`/bin/echo ${USER_CFG_DIR}/${cff_Family_file_name}`

   if [ ! -f "$cff_outfile" ]
   then

      case "$cff_Node_object_type" in
         host | platform | module | proxy | snmp | icmp | none)
            /bin/echo "# Family name: $cff_Family_file_name" >> $cff_outfile
            /bin/echo "# Written by:  $cff_Username" >> $cff_outfile
            /bin/echo "#" >> $cff_outfile
            ;;
         *)
            errormsg "create_node_family_file" \
               "Error! Invalid node object type <$cff_Node_object_type>"
            return
            ;;
      esac

      case "$cff_Node_object_type" in
         host | platform)
            /bin/echo "[ load sun-server-family-j.x ]" >> $cff_outfile
            /bin/echo "" >> $cff_outfile
            ;;

         module | proxy)
            ;;

         snmp | icmp | none)
            /bin/echo "[ load ping-base-family-j.x ]" >> $cff_outfile
            /bin/echo "" >> $cff_outfile
            ;;
      esac

      /bin/echo "largeIcon(DFT) = $cff_Large_icon_file" >> $cff_outfile
      /bin/echo "smallIcon(DFT) = $cff_Small_icon_file" >> $cff_outfile
      /bin/echo "" >> $cff_outfile

      if [ "$cff_Node_object_type" = "module" -o \
           "$cff_Node_object_type" = "proxy" ]
      then
         /bin/echo "navigateCommand = launchUniqueFrame %windowID \
.templates.consoles.server-agent-details nodeName=%nodeName \
topologyDescUrl=%topologyDescUrl agentHost=%targetHost agentPort=%targetPort \
objectUrl=%targetUrl contextUrl=%contextUrl" >> $cff_outfile
         /bin/echo "" >> $cff_outfile


         /bin/echo "commands = attedit edit enable disable unload detail" \
               >> $cff_outfile
         /bin/echo "commandLabel(edit) = \
base.console.ConsoleGeneric:modules.edit" >> $cff_outfile

         /bin/echo "commandSpec(edit) = launchUniqueDialog %windowID \
.templates.tools.moduleeditor agentHost=%targetHost \
agentPort=%targetPort moduleId=%targetModuleId \
moduleSpec=%targetModuleSpec" >> $cff_outfile
         /bin/echo "" >> $cff_outfile

         /bin/echo "commandLabel(enable) = \
base.console.ConsoleGeneric:modules.enable" >> $cff_outfile

         /bin/echo "commandSpec(enable) = requestModuleOperation %windowID \
%targetUrl enable ignore" >> $cff_outfile
         /bin/echo "" >> $cff_outfile

         /bin/echo "commandLabel(disable) = \
base.console.ConsoleGeneric:modules.disable" >> $cff_outfile

         /bin/echo "commandSpec(disable) = requestModuleOperation %windowID \
%targetUrl disable ignore" >> $cff_outfile
         /bin/echo "" >> $cff_outfile


         /bin/echo "commandLabel(unload) = \
base.console.ConsoleGeneric:modules.unload" >> $cff_outfile

         /bin/echo "commandSpec(unload) = launchUniqueDialog %windowID \
.templates.tools.confirmunload objectUrl=%targetUrl" >> $cff_outfile
         /bin/echo "" >> $cff_outfile


         /bin/echo "commandLabel(detail) = \
base.console.ConsoleGeneric:standard.hostdet" >> $cff_outfile

         /bin/echo "commandSpec(detail) = launchUniqueFrame %windowID \
.templates.consoles.server-agent-details nodeName=%nodeName \
topologyDescUrl=%topologyDescUrl agentHost=%targetHost agentPort=%targetPort \
objectUrl=%targetUrl contextUrl=%contextUrl" >> $cff_outfile
         /bin/echo "" >> $cff_outfile


         /bin/echo "commandLabel(attedit) = \
base.console.ConsoleGeneric:standard.attedit" >> $cff_outfile

         /bin/echo "commandSpec(attedit) = launchUniqueDialog %windowID \
.templates.tools.attributeeditor objectUrl=%targetUrl objectPath=%pathName \
objectName=%nodeName" >> $cff_outfile
         /bin/echo "" >> $cff_outfile

      fi
   else
#      errormsg "create_node_family_file" \
#         'File \\<$cff_outfile\\> already exists'
       return
   fi
   /usr/bin/chmod 666 "$cff_outfile"
}

# subfunction: create_group_family_file
# inputs : 
#    family file name
#    large icon file name
#    small icon file name
#    username
# outputs: none
#
create_group_family_file()
{
   cff_Family_file_name=$1
   cff_Large_icon_file=$2
   cff_Small_icon_file=$3
   cff_Username=$4
   cff_outfile=`/bin/echo ${USER_CFG_DIR}/${cff_Family_file_name}`

   if [ ! -f "$cff_outfile" ]
   then
      /bin/echo "# Family name: $cff_Family_file_name" >> $cff_outfile
      /bin/echo "# Written by:  $cff_Username" >> $cff_outfile
      /bin/echo "#" >> $cff_outfile

      /bin/echo "largeIcon(DFT) = $cff_Large_icon_file" >> $cff_outfile
      /bin/echo "smallIcon(DFT) = $cff_Small_icon_file" >> $cff_outfile

      /bin/echo "" >> $cff_outfile

      /bin/echo "commands = pasteinto attedit detail alarmaction" >> $cff_outfile

      /bin/echo "" >> $cff_outfile

      /bin/echo "commandLabel(pasteinto) = base.console.ConsoleTopology:edit.pasteinto" >> $cff_outfile
      /bin/echo "commandSpec(pasteinto) = pasteInto %windowID %targetUrl" >> $cff_outfile

      /bin/echo "" >> $cff_outfile

      /bin/echo "commandLabel(attedit) = base.console.ConsoleGeneric:standard.attedit" >> $cff_outfile
      /bin/echo "commandSpec(attedit) = launchUniqueDialog %windowID .templates.tools.attributeeditor objectUrl=%targetUrl objectPath=%pathName objectName=%nodeName" >> $cff_outfile

      /bin/echo "" >> $cff_outfile

      /bin/echo "commandLabel(detail) = base.console.ConsoleGeneric:standard.hostdet" >> $cff_outfile
      /bin/echo "commandSpec(detail) = launchUniqueFrame %windowID .templates.consoles.nonagent-details nodeName=%nodeName topologyDescUrl=%topologyDescUrl agentHost=%targetHost agentPort=%targetPort objectUrl=%targetUrl" >> $cff_outfile

      /bin/echo "" >> $cff_outfile

      /bin/echo "commandLabel(alarmaction) = base.console.ConsoleGeneric:standard.alarmaction" >> $cff_outfile
      /bin/echo "commandSpec(alarmaction) = launchUniqueDialog %windowID .templates.tools.attributeeditor objectUrl=%targetUrl/entityInfoTable/entityInfoEntry objectPath=%pathName objectName=%nodeName" >> $cff_outfile
      /usr/bin/chmod 666 "$cff_outfile"
    fi

    return
}

# subfunction: create_segment_family_file
# inputs : 
#    family file name
#    large icon file name
#    small icon file name
#    username
# outputs: none
#
create_segment_family_file()
{
   cff_Family_file_name=$1
   cff_Large_icon_file=$2
   cff_Small_icon_file=$3
   cff_Username=$4
   cff_outfile=`/bin/echo ${USER_CFG_DIR}/${cff_Family_file_name}`

   if [ ! -f "$cff_outfile" ]
   then
      /bin/echo "# Family name: $cff_Family_file_name" >> $cff_outfile
      /bin/echo "# Written by:  $cff_Username" >> $cff_outfile
      /bin/echo "#" >> $cff_outfile

      /bin/echo "largeIcon(DFT) = $cff_Large_icon_file" >> $cff_outfile
      /bin/echo "smallIcon(DFT) = $cff_Small_icon_file" >> $cff_outfile
      /usr/bin/chmod 666 "$cff_outfile"
    fi

    return
}

# subfunction: addline
# inputs : 
#   filename
#   line to add
# outputs: none
#
addline()
{
   al_file=$1
   al_line="$2"

   al_varfile="$USER_CFG_DIR/$al_file"
   al_optfile="$SUNMC_CFG_DIR/$al_file"
   
   if [ ! -f "$USER_CFG_DIR/$al_file" ]
   then
      /bin/cp "$al_optfile" "$al_varfile"
      /usr/bin/chmod 666 "$al_varfile"
   fi

   /usr/bin/grep -s "$families_comment1" $al_varfile > /dev/null

   if [ $? -ne 0 ]
   then
      `/bin/echo "#" >> $al_varfile`
      `/bin/echo "${families_comment1}" >> $al_varfile`
      `/bin/echo "${families_comment2}" >> $al_varfile`
      `/bin/echo "#" >> $al_varfile`
   fi

   `/bin/echo "$al_line" >> $al_varfile`
}

# subfunction: findcomment
# inputs : 
#    filename
# outputs: line number of comment
#
findcomment()
{
   fc_file="$1"

   if [ ! -f "$fc_file" ]
   then
      /bin/echo "-1"
   else
      fc_val=`/bin/awk 'BEGIN { ln = -1 } \
         { if ( $0 == "'"$families_comment1"'" ) ln = NR } \
         END { print ln }' $fc_file`
      /bin/echo "$fc_val"
   fi
}

# New low-level functions
#

# subfunction: findline_after_comment
# inputs :
#   filename
#   column to searh for key
#   key
# outputs:
#   line number containing key, or -1 if not found
#
findline_after_comment()
{
   fac_file="$1"
   fac_col="$2"
   fac_key="$3"

   fac_found=`findline "$fac_file" $fac_col "$fac_key" "$4"`

   if [ "$fac_found" -ne "-1" ]
   then
      # Found line must be after the comment, or else the
      # line isn't a user-added device.
      #
      fac_comment=`findcomment "$fac_file"`

      if [ "$fac_comment" -eq "-1" ]
      then
         /bin/echo -1
         return
      fi

      if [ "$fac_found" -gt "$fac_comment" ]
      then
         /bin/echo $fac_found
      else
         /bin/echo "-1"
      fi
   else
      /bin/echo "-1"
   fi
}


# subfunction: remove_line_after_comment
# inputs :
#   filename
#   column to search for key
#   key
# outputs:
#   line number that was removed, or -1 if no line removed
#
remove_line_after_comment()
{
   rlac_file="$1"
   rlac_col="$2"
   rlac_key="$3"

   rlac_line=`findline_after_comment "$rlac_file" "$rlac_col" "$rlac_key" "$4"`

   if [ "$rlac_line" -ne "-1" ]
   then
      rmline "$rlac_file" "$rlac_line"
      delete_if_empty "$rlac_file"
   fi

#  /bin/echo "$rlac_line"
}

# subfunction: delete_if_empty
# inputs :
#   filename
# outputs: none
#
delete_if_empty()
{
   die_file="$1"
   die_comment_temp=`findcomment "$die_file"`
   die_lc=`/bin/wc -l $die_file | awk '{ print $1 }'`
   die_comment=`expr $die_comment_temp + 2`

   # Go ahead and remove the file
   #
   if [ "$die_lc" -eq "$die_comment" ]
   then
      /bin/rm -f $die_file
   fi
}

# New high-level functions
#

# subfunction: remove_discovery_entry
# inputs :
#   object_type
# outputs: none
#
remove_discovery_entry()
{
   rde_object_type="$1"
   rde_file="$USER_CFG_DIR/$DISCOVERY_CFG_FILE"

   rde_line=`remove_line_after_comment $rde_file 3 $rde_object_type "[ \t]*"`

#   /bin/echo "Search for=<$rde_object_type> in file <$rde_file>"
#   /bin/echo "Return value=<$rde_line>"
}

# subfunction: remove_properties_entry
# inputs :
#   i18n key
#   filename
# outputs: none
#
remove_properties_entry()
{
   rpe_i18n_key="$1"
   rpe_file="$2"

   rpe_line=`remove_line_after_comment $rpe_file 1 $rpe_i18n_key "=[ \t]*"`

#   /bin/echo "Search for=<$rpe_i18n_key> in file <$rpe_file>"
#   /bin/echo "Return value=<$rpe_line>"
}

# subfunction: remove_family_file
# inputs :
#   filename
# outputs: none
#
remove_family_file()
{
   rfe_file="$1"

   if [ -f $rfe_file ]
   then
      small_icon_file=`/bin/cat $rfe_file | /bin/awk '{ if ($1 == "largeIcon(DFT)") print $3 }'`
      large_icon_file=`/bin/cat $rfe_file | /bin/awk '{ if ($1 == "smallIcon(DFT)") print $3 }'`

      if [ -f "$USER_CFG_DIR/$small_icon_file" ]
      then
         rm -f "$USER_CFG_DIR/$small_icon_file"
      fi

      if [ -f "$USER_CFG_DIR/$large_icon_file" ]
      then
         rm -f "$USER_CFG_DIR/$large_icon_file"
      fi

      rm -f $rfe_file
   fi
}


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

# subfunction: set_global_variable
# inputs : 
# outputs: 
#
set_global_variable ()
{

        # Check for a blank line
        if [ "${parameter}" = "" ]
	then
		return
	fi

        if [ "${value}" = "" ]
	then
                errormsg "set_global_variable" \
                   "Value not specified for <${parameter}>"
		return
	fi

	#convert the parameter to uppercase
	uc_parameter=`/bin/echo ${parameter} | tr '[a-z]'  '[A-Z]' `
	parameter=${uc_parameter}

        case ${parameter} in 
           USER_NAME)                global_user_name=${value} ;;
	   OBJECT_TYPE)              global_object_type=${value} ;; 
           MONITOR_VIA)              global_monitor_via=${value} ;; 
           NODE_TYPE)                global_node_type=${value} ;;
           NODE_OBJECT_TYPE)         global_node_object_type=${value} ;; 
           I18N_KEY)                 global_i18n_key=${value} ;;
           PROPERTIES_FILE)          global_properties_file=${value} ;; 
           SEARCH_PARAMETER)         global_search_parameter=${value} ;;
           LARGE_ICON)               global_large_icon=${value} ;;
           SMALL_ICON)               global_small_icon=${value} ;;
           GROUP_TYPE)               global_group_type=${value} ;;
           GROUP_OBJECT_TYPE)        global_group_object_type=${value} ;;
           SEGMENT_TYPE)             global_segment_type=${value} ;;
           SEGMENT_OBJECT_TYPE)      global_segment_object_type=${value} ;;
           DEVICE_ID)                global_device_id=${value} ;;
	   *)                        errormsg "set_global_variable" \
                                        "Invalid parameter <${parameter}>" ;;
	esac
        return

}

# subfunction: unset_global_variables
# inputs : 
# outputs: 
#
unset_global_variables ()
{
    global_user_name=""
    global_object_type=""
    global_monitor_via=""
    global_node_type=""
    global_node_object_type=""
    global_i18n_key=""
    global_properties_file=""
    global_search_parameter=""
    global_large_icon=""
    global_small_icon=""
    global_group_type=""
    global_group_object_type=""
    global_segment_type=""
    global_segment_object_type=""
    global_device_id=""
    return
}

# subfunction: process_delete_data
# inputs : 
# outputs: 
#
process_delete_data ()
{
        #convert the value to uppercase
        lc_value=`/bin/echo ${global_object_type} | tr '[A-Z]' '[a-z]' `
        global_object_type=${lc_value}

        case ${global_object_type} in
            node) delete_node ;;
            group) delete_group ;;
            segment) delete_segment ;;
            *) errormsg "process_delete_data" \
                  "Object type <$global_object_type> missing in data file" ;;
        esac
        return
}

# subfunction:delete_node
# inputs :
# outputs:
#
delete_node ()
{

        # check if required parameters are present
        if [ "$global_user_name" = "" -o "$global_node_type" = "" -o "$global_monitor_via" = "" -o "$global_node_object_type" = "" -o "$global_i18n_key" = "" ]
        then
             errormsg "delete_node" \
                  "Invalid data in input file"
             return
        fi 

        family_name=`/bin/echo ${global_node_object_type}`
        family_file=`/bin/echo ${USER_CFG_DIR}/${family_name}-family-j.x`
        properties_file=`/bin/echo ${USER_PROPERTIES_DIR}/${global_user_name}.properties`
        key=`/bin/echo $global_i18n_key | cut -d ":" -f2`

        if [ ! -f "$family_file" ]
        then
            errormsg "delete_node" \
               "<$family_file> does not exist, verify input"
            return
        fi

        # First convert to lower case
        lc_value=`/bin/echo ${global_monitor_via} | tr '[A-Z]' '[a-z]' `
        global_monitor_via=${lc_value}
        lc_value=`/bin/echo ${global_node_type} | tr '[A-Z]' '[a-z]' `
        global_node_type=${lc_value}

        case ${global_monitor_via} in
        # Remove the host or platform entry only
        host|platform) 
                       # Check if node_object_type is in any other
                       # *families-j.x file in /var/opt/SUNWsymon/cfg
                       # If yes, don't remove entries from discovery-j.x
                       # and don't remove corresponding family file.

                       # Currently, Enforcing device name to be unique
                       #/usr/bin/grep -s "$family_name" ${USER_CFG_DIR}/*families-j.x > /dev/null

                       #if [ $? -ne 0 ]
                       #then
                       # remove_family_file "$family_file"
                       #fi

                       remove_family_file "$family_file"
                       remove_discovery_entry "$family_name" 
                       remove_properties_entry "$key" "$properties_file"
                      ;;
        # Remove the proxy or module entry only
        proxy|module) families_file=`/bin/echo module-module-families-j.x`
                      user_file=`/bin/echo ${USER_CFG_DIR}/${families_file}`

                      if [ -f "$user_file" ] && [ -r "$user_file" ]
                      then
                          remove_line_after_comment "$user_file" 1 "$family_name" "[ \t]*"
                      fi

                       # Check if node_object_type is in any other
                       # *families-j.x file in /var/opt/SUNWsymon/cfg
                       # If yes, and don't remove corresponding family file.
                       /usr/bin/grep -s "$family_name" ${USER_CFG_DIR}/*families-j.x > /dev/null

                       if [ $? -ne 0 ]
                       then
                           remove_family_file "$family_file"
                       fi
                       remove_properties_entry "$key" "$properties_file"
                       ;;
        # Remove the snmp or icmp or none entry only
        snmp|icmp|none) families_file=`/bin/echo ${global_monitor_via}-${global_node_type}-families-j.x`
                        user_file=`/bin/echo ${USER_CFG_DIR}/${families_file}`
                        if [ -f "$user_file" ] && [ -r "$user_file" ]
                        then
                            remove_line_after_comment "$user_file" 1 "$family_name" "[ \t]*"
                      fi

                       # Check if node_object_type is in any other
                       # *families-j.x file in /var/opt/SUNWsymon/cfg
                       # If yes, don't remove entries from discovery-j.x
                       # and don't remove corresponding family file.
                       # These 3 can share the family file
                       /usr/bin/grep -s "$family_name" ${USER_CFG_DIR}/*families-j.x > /dev/null

                       if [ $? -ne 0 ]
                       then
                           remove_family_file "$family_file"
                       fi
                       remove_discovery_entry "$family_name"
                       remove_properties_entry "$key" "$properties_file"
                      ;;
        esac
        return
}

# subfunction:delete_group
# inputs :
# outputs:
#
delete_group ()
{
        # check if required parameters are present
        if [ "$global_user_name" = "" -o "$global_group_type" = "" -o "$global_group_object_type" = "" -o "$global_i18n_key" = "" ]
        then
             errormsg "delete_group" \
                  "Invalid data in input file"
             return
        fi 

        family_name=`/bin/echo ${global_group_object_type}`
        family_file=`/bin/echo ${USER_CFG_DIR}/${family_name}-family-j.x`
        properties_file=`/bin/echo ${USER_PROPERTIES_DIR}/${global_user_name}.properties`

        if [ ! -f "$family_file" ]
        then
            errormsg "delete_group" \
               "<$family_file> does not exist, verify input"
            return
        fi

        # First convert to lower case
        lc_value=`/bin/echo ${global_group_type} | tr '[A-Z]' '[a-z]' `
        global_group_type=${lc_value}

        case ${global_group_type} in
        general) families_file="group-general-families-j.x"
                 ;;
        ipbased) families_file="group-ipbased-families-j.x"
                 ;;
        *)       errormsg "delete_group"
                     "Invalid group type <${global_group_type}>"
                 return
                 ;;
        esac

        user_file=`/bin/echo ${USER_CFG_DIR}/${families_file}`
        if [ -f "$user_file" ] && [ -r "$user_file" ]
        then
           remove_line_after_comment "$user_file" 1 "$family_name" "[ \t]*"
        fi

        # Check if node_object_type is in any other
        # *families-j.x file in /var/opt/SUNWsymon/cfg
        # If yes, remove corresponding family file.
        /usr/bin/grep -s "$family_name" ${USER_CFG_DIR}/*families-j.x > /dev/null

        if [ $? -ne 0 ]
        then
           key=`/bin/echo $global_i18n_key | cut -d ":" -f2`
           remove_properties_entry "$key" "$properties_file"
           remove_family_file "$family_file"
        fi

        return
}

# subfunction:delete_segment
# inputs :
# outputs:
#
delete_segment ()
{
        # check if required parameters are present
        if [ "$global_user_name" = "" -o "$global_segment_type" = "" -o "$global_segment_object_type" = "" -o "$global_i18n_key" = "" ]
        then
             errormsg "delete_segment" \
                  "Invalid data in input file"
             return
        fi 

        family_name=`/bin/echo ${global_segment_object_type}`
        family_file=`/bin/echo ${USER_CFG_DIR}/${family_name}-family-j.x`
        properties_file=`/bin/echo ${USER_PROPERTIES_DIR}/${global_user_name}.properties`

        if [ ! -f "$family_file" ]
        then
            errormsg "delete_segment" \
               "<$family_file> does not exist, verify input"
            return
        fi

        # First convert to lower case
        lc_value=`/bin/echo ${global_segment_type} | tr '[A-Z]' '[a-z]' `
        global_segment_type=${lc_value}

        case ${global_segment_type} in
        bus) families_file="segment-bus-families-j.x"
                 ;;
        ring) families_file="segment-ring-families-j.x"
                 ;;
        *)       errormsg "delete_segment"
                     "Invalid segment type <${global_segment_type}>"
                 return
                 ;;
        esac

        user_file=`/bin/echo ${USER_CFG_DIR}/${families_file}`
        if [ -f "$user_file" ] && [ -r "$user_file" ]
        then
           remove_line_after_comment "$user_file" 1 "$family_name" "[ \t]*"
        fi

        # Check if node_object_type is in any other
        # *families-j.x file in /var/opt/SUNWsymon/cfg
        # If yes, remove corresponding family file.
        /usr/bin/grep -s "$family_name" ${USER_CFG_DIR}/*families-j.x > /dev/null

        if [ $? -ne 0 ]
        then
           key=`/bin/echo $global_i18n_key | cut -d ":" -f2`
           remove_properties_entry "$key" "$properties_file"
           remove_family_file "$family_file"
        fi

        return
}

# subfunction: process_add_data
# inputs : 
# outputs: 
#
process_add_data ()
{
    validate_object_type
    if [ "${error_flag}" -eq 1 ]
    then
        write_to_error_log
    else
        add_device
    fi
    unset_global_variables
    return
}

# subfunction: validate_object_type
# inputs : 
# outputs: 
#
validate_object_type ()
{
        #convert the value to uppercase
        lc_value=`/bin/echo ${global_object_type} | tr '[A-Z]' '[a-z]' `
        global_object_type=${lc_value}
        case ${global_object_type} in
            node)    validate_node_data ;;
            group)   validate_group_data ;;
            segment) validate_segment_data ;;
            *)       errormsg "validate_object_type" \
                        "Invalid object type <${global_object_type}>" ;;
        esac
        return
}

# subfunction: validate_node_data
# inputs : 
# outputs: 
#
validate_node_data ()
{
        validate_monitor_via
        validate_node_type
        validate_node_object_type
        validate_i18n_key
        validate_properties_file
        validate_search_parameter
        validate_icon_file $global_user_name $global_large_icon
        validate_icon_file $global_user_name $global_small_icon
        return
}

# subfunction: validate_monitor_via
# inputs : 
# outputs: 
#
validate_monitor_via ()
{
    lc_value=`/bin/echo ${global_monitor_via} | tr '[A-Z]' '[a-z]' `
    global_monitor_via=${lc_value}
    case ${global_monitor_via} in
        host|platform|module|proxy|snmp|icmp|none) ;;
        *)  errormsg "validate_monitor_via " \
               "<$global_monitor_via> is invalid type" ;;
    esac
    return
}

# subfunction: validate_node_type
# inputs : 
# outputs: 
#
validate_node_type ()
{
    lc_value=`/bin/echo ${global_node_type} | tr '[A-Z]' '[a-z]' `
    global_node_type=${lc_value}
    case ${global_node_type} in
	router|concentrator|workstation|printer|pc|server|module) ;;
        *)   errormsg "validate_node_type" \
                "<$global_node_type> is invalid node type" ;;
    esac
    return
}

# subfunction: validate_node_object_type
# inputs : 
# outputs: 
#
validate_node_object_type ()
{
        # First make sure everything is in lowercase
        lc_value=`/bin/echo ${global_monitor_via} | tr '[A-Z]' '[a-z]' `
        global_monitor_via=${lc_value}
        lc_value=`/bin/echo ${global_node_type} | tr '[A-Z]' '[a-z]' `
        global_node_type=${lc_value}

        case ${global_monitor_via} in
        # validate object for host and platform
        host|platform) family_file=`/bin/echo ${global_node_object_type}-family-j.x`
                       sunmc_file=`/bin/echo ${SUNMC_CFG_DIR}/${family_file}`
                       user_file=`/bin/echo ${USER_CFG_DIR}/${family_file}` 
                       if [ -f "$user_file" ] || [ -f "$sunmc_file" ]
                       then
                           errormsg "validate_node_object_type" \
                              "<$user_file> or <$sunmc_file> exists"
                       fi
                       ;;
        # validate object for modules and proxy
        module|proxy) families_file=`/bin/echo module-module-families-j.x`
                      sunmc_file=`/bin/echo ${SUNMC_CFG_DIR}/${families_file}`
                      user_file=`/bin/echo ${USER_CFG_DIR}/${families_file}`
                      value=`/bin/echo ${global_node_object_type}`
                      if [ -f "$user_file" ] && [ -r "$user_file" ]
                      then
                          line=`findline $user_file 1 $value "[ \t]*"`
                          if [ ${line} -ne -1 ]
                          then
                              errormsg "validate_node_object_type" \
                                 "Node object type exists in <$user_file> line <$line>"
                          fi
                      else 
                          if [ -f "$sunmc_file" ] && [ -r "$sunmc_file" ]
                          then
                              line=`findline $sunmc_file 1 $value "[ \t]*"`
                              if [ ${line} -ne -1 ]
                              then
                                  errormsg "validate_node_object_type" \
                                     "Node object type exists in <$sunmc_file> line <$line>"
                              fi
                          fi
                      fi
                      ;;
        # validate object for snmp, icmp and none
    	snmp|icmp|none) families_file=`/bin/echo ${global_monitor_via}-${global_node_type}-families-j.x` 
                        sunmc_file=`/bin/echo ${SUNMC_CFG_DIR}/${families_file}`
                        user_file=`/bin/echo ${USER_CFG_DIR}/${families_file}` 
                        value=`/bin/echo ${global_node_object_type}`
                        if [ -f "$user_file" ] && [ -r "$user_file" ]
                        then
                            line=`findline $user_file 1 $value "[ \t]*"`
                            if [ ${line} -ne -1 ]
                            then
                                errormsg "validate_node_object_type" \
                                   "Node object type exists in <$user_file> line <$line>"
                            fi
                        else 
                            if [ -f "$sunmc_file" ] && [ -r "$sunmc_file" ]
                            then
                                line=`findline $sunmc_file 1 $value "[ \t]*"`
                                if [ ${line} -ne -1 ]
                                then
                                    errormsg "validate_node_object_type" \
                                       "Node object type exists in <$sunmc_file> line <$line>"
                                fi
                            fi
                        fi
                        ;;
        *)              errormsg "validate_node_object_type" \
                           "Invalid object type <${global_monitor_via}>" ;;
        esac
        return
}

# subfunction: validate_i18n_key
# inputs : 
# outputs: 
#
validate_i18n_key ()
{
    #convert the value to uppercase
    lc_value=`/bin/echo ${global_object_type} | tr '[A-Z]' '[a-z]' `
    global_object_type=${lc_value}
    case ${global_object_type} in
    node) validate_node_i18n_key
          ;;
    group) validate_group_i18n_key 
          ;;
    segment) validate_segment_i18n_key
             ;;  
    *)       errormsg "validate_i18n_key" \
                "Invalid object type <${global_object_type}>" ;;
    esac
    return
}

# subfunction: validate_node_i18n_key
# inputs : 
# outputs: 
#
validate_node_i18n_key ()
{
        # First make sure everything is in lowercase
        lc_value=`/bin/echo ${global_monitor_via} | tr '[A-Z]' '[a-z]' `
        global_monitor_via=${lc_value}
        lc_value=`/bin/echo ${global_node_type} | tr '[A-Z]' '[a-z]' `
        global_node_type=${lc_value}

        case ${global_monitor_via} in
        # validate object for host and platform
        host|platform) family_file=`/bin/echo ${global_node_object_type}-family-j.x`
                       sunmc_file=`/bin/echo ${SUNMC_CFG_DIR}/${family_file}`
                       user_file=`/bin/echo ${USER_CFG_DIR}/${family_file}` 
                       if [ -f "$user_file" ] || [ -f "$sunmc_file" ]
                       then
                           errormsg "validate_node_object_type" \
                              "<$user_file> or <$sunmc_file> exists"
                       fi
                       ;;
        # validate object for modules and proxy
        module|proxy) families_file=`/bin/echo module-module-families-j.x`
                      sunmc_file=`/bin/echo ${SUNMC_CFG_DIR}/${families_file}`
                      user_file=`/bin/echo ${USER_CFG_DIR}/${families_file}`
                      if [ -f "$user_file" ] && [ -r "$user_file" ]
                      then
                          line=`findline $user_file 2 $global_i18n_key "[ \t]*"`
                          if [ ${line} -ne -1 ]
                          then
                              errormsg "validate_i18n_key" \
                                 "Node object type exists in <$user_file> line <$line>"
                          fi
                      else 
                          if [ -f "$sunmc_file" ] && [ -r "$sunmc_file" ]
                          then
                              line=`findline $sunmc_file 2 $global_i18n_key "[ \t]*"`
                              if [ ${line} -ne -1 ]
                              then
                                  errormsg "validate_i18n_key" \
                                     "Node object type exists in <$sunmc_file> line <$line>"
                              fi
                          fi
                      fi
                      ;;
        # validate object for snmp, icmp and none
    	snmp|icmp|none) families_file=`/bin/echo ${global_monitor_via}-${global_node_type}-families-j.x` 
                        sunmc_file=`/bin/echo ${SUNMC_CFG_DIR}/${families_file}`
                        user_file=`/bin/echo ${USER_CFG_DIR}/${families_file}` 
                        if [ -f "$user_file" ] && [ -r "$user_file" ]
                        then
                            line=`findline $user_file 2 $global_i18n_key "[ \t]*"`
                            if [ ${line} -ne -1 ]
                            then
                                errormsg "validate_i18n_key" \
                                   "Node object type exists in <$user_file> line <$line>"
                            fi
                        else 
                            if [ -f "$sunmc_file" ] && [ -r "$sunmc_file" ]
                            then
                                line=`findline $sunmc_file 2 $global_i18n_key "[ \t]*"`
                                if [ ${line} -ne -1 ]
                                then
                                    errormsg "validate_i18n_key" \
                                       "Node object type exists in <$sunmc_file> line <$line>"
                                 fi
                            fi
                        fi
                        ;;
        *)              errormsg "validate_i18n_key" \
                           "Invalid i18n key: <${global_i18n_key}>"
        esac
    return
}

# subfunction: validate_group_i18n_key
# inputs : 
# outputs: 
#
validate_group_i18n_key ()
{
    lc_value=`/bin/echo ${global_group_type} | tr '[A-Z]' '[a-z]' `
    global_group_type=${lc_value}
    case ${global_group_type} in
        general) families_file=`/bin/echo group-general-families-j.x`
                 ;;
        ipbased) families_file=`/bin/echo group-ipbased-families-j.x`
                 ;;
        *)   errormsg "validate_group_i18n_type" \
                "<$global_group_type> is invalid group type"
             return
            ;;

    esac

    sunmc_file=`/bin/echo ${SUNMC_CFG_DIR}/${families_file}`
    user_file=`/bin/echo ${USER_CFG_DIR}/${families_file}`
    if [ -f "$user_file" ] && [ -r "$user_file" ]
    then
       line=`findline $user_file 2 $global_i18n_key "[ \t]*"`
       if [ ${line} -ne -1 ]
       then
          errormsg "validate_group_i18n_key" \
             "Group object type exists in <$user_file> line <$line>"
       fi
    else
       if [ -f "$sunmc_file" ] && [ -r "$sunmc_file" ]
       then
          line=`findline $sunmc_file 2 $global_i18n_key "[ \t]*"`
          if [ ${line} -ne -1 ]
          then
             errormsg "validate_group_i18n_key" \
                "Group object type exists in <$sunmc_file> line <$line>"
          fi
       fi
    fi
    return

}


# subfunction: validate_segment_i18n_key
# inputs :
# outputs:
#
validate_segment_i18n_key ()
{
    lc_value=`/bin/echo ${global_segment_type} | tr '[A-Z]' '[a-z]' `
    global_segment_type=${lc_value}
    case ${global_segment_type} in
        bus) families_file=`/bin/echo segment-bus-families-j.x`
                 ;;
        ring) families_file=`/bin/echo segment-ring-families-j.x`
                 ;;
        *)   errormsg "validate_segment_i18n_type" \
                "<$global_segment_type> is invalid segment type"
             return
            ;;

    esac

    sunmc_file=`/bin/echo ${SUNMC_CFG_DIR}/${families_file}`
    user_file=`/bin/echo ${USER_CFG_DIR}/${families_file}`
    if [ -f "$user_file" ] && [ -r "$user_file" ]
    then
       line=`findline $user_file 2 $global_i18n_key "[ \t]*"`
       if [ ${line} -ne -1 ]
       then
          errormsg "validate_segment_i18n_key" \
             "Segment object type exists in <$user_file> line <$line>"
       fi
    else
       if [ -f "$sunmc_file" ] && [ -r "$sunmc_file" ]
       then
          line=`findline $sunmc_file 2 $global_i18n_key "[ \t]*"`
          if [ ${line} -ne -1 ]
          then
             errormsg "validate_segment_i18n_key" \
                "Segment object type exists in <$sunmc_file> line <$line>"
          fi
       fi
   fi
        return
}

# subfunction: validate_properties_file
# inputs : 
# outputs: 
#
validate_properties_file ()
{

        file=`/usr/bin/basename ${global_properties_file}`
        file_prefix=`/bin/echo ${file} | cut -d "." -f 1`
        if [ ${file_prefix} != ${global_user_name} ]
        then
        #    errormsg "validate_properties_file" 
        #       'If file exists it will be overwritten with new file'
        #else
            errormsg "validate_properties_file" \
               "Invalid property file name. Must be <${global_user_name}.properties>"
        fi
        return
}

# subfunction: validate_search_parameter
# inputs : 
# outputs: 
#
validate_search_parameter ()
{
        /bin/echo "$global_search_parameter" | /usr/bin/grep -s "^.*|.*|[\t ]*$" > /dev/null
        if [ $? -ne 0 ]
        then
           errormsg "validate_search_parameter" \
              "Invalid sysoid value <$global_search_parameter>"
        fi

   return
}

# subfunction: validate_icon_file
# note   : Takes in username and icon filename for arguments
# inputs : 
# outputs: 
#
validate_icon_file ()
{
        #user_file=`/usr/bin/basename $2`
        #icon_file=`/bin/echo $USER_CFG_DIR/$1-$user_file`
        if [ ! -f $2 ]
        then
           errormsg "validate_icon_file"
              "Check if file exists and permissions"
        fi
        return
}

# subfunction: validate_group_data
# inputs : 
# outputs: 
#
validate_group_data ()
{
        validate_group_type
        validate_group_object_type
        validate_i18n_key
        validate_properties_file
        validate_icon_file $global_user_name $global_large_icon
        validate_icon_file $global_user_name $global_small_icon
        return
}

# subfunction: validate_group_type
# inputs :
# outputs:
#
validate_group_type ()
{
    lc_value=`/bin/echo ${global_group_type} | tr '[A-Z]' '[a-z]' `
    global_group_type=${lc_value}
    case ${global_group_type} in
        general|ipbased) ;;
        *)   errormsg "validate_group_type" \
                "<$global_group_type> is invalid group type" ;;
    esac
    return
}

# subfunction: validate_group_object_type
# inputs :
# outputs:
#
validate_group_object_type ()
{

    lc_value=`/bin/echo ${global_group_type} | tr '[A-Z]' '[a-z]' `
    global_group_type=${lc_value}
    case ${global_group_type} in
        # Set families file for general group
    	general) families_file="group-general-families-j.x"
                 ;;
        # Set families file for ipbased group
    	ipbased) families_file="group-ipbased-families-j.x"
                 ;;
        # Any other type means error
        *)      errormsg "validate_group_object_type" \
                   "Invalid object type <${global_group_object_type}>"
                return
                ;;
        esac

        sunmc_file=`/bin/echo ${SUNMC_CFG_DIR}/${families_file}`
        user_file=`/bin/echo ${USER_CFG_DIR}/${families_file}` 
        value=`/bin/echo ${global_group_object_type}`
        if [ -f "$user_file" ] && [ -r "$user_file" ]
        then
           line=`findline $user_file 1 $value "[ \t]*"`
           if [ ${line} -ne -1 ]
           then
              errormsg "validate_group_object_type" \
                 "Group object type exists in <$user_file> line <$line>"
           fi
        else 
           if [ -f "$sunmc_file" ] && [ -r "$sunmc_file" ]
           then
              line=`findline $sunmc_file 1 $value "[ \t]*"`
              if [ ${line} -ne -1 ]
              then
                 errormsg "validate_group_object_type" \
                    "Group object type exists in <$sunmc_file> line <$line>"
              fi
           fi
        fi
        return
}

# subfunction: validate_segment_data
# inputs : 
# outputs: 
#
validate_segment_data ()
{
        validate_segment_type
        validate_segment_object_type
        validate_i18n_key
        validate_properties_file
        validate_icon_file $global_user_name $global_large_icon
        validate_icon_file $global_user_name $global_small_icon
        return
}

# subfunction: validate_segment_type
# inputs : 
# outputs: 
#
validate_segment_type ()
{
    lc_value=`/bin/echo ${global_segment_type} | tr '[A-Z]' '[a-z]' `
    global_segment_type=${lc_value}
    case ${global_segment_type} in
        bus|ring) ;;
        *)   errormsg "validate_segment_type" \
                "<$global_segment_type> is invalid segment type" ;;
    esac
    return

}

# subfunction: validate_segment_object_type
# inputs : 
# outputs: 
#
validate_segment_object_type ()
{

    lc_value=`/bin/echo ${global_segment_type} | tr '[A-Z]' '[a-z]' `
    global_segment_type=${lc_value}
    case ${global_segment_type} in
        # Set families file for bus segment
    	bus) families_file="segment-bus-families-j.x"
                 ;;
        # Set families file for ring segment
    	ring) families_file="segment-ring-families-j.x"
                 ;;
        # Any other type means error
        *)      errormsg "validate_segment_object_type" \
                   "Invalid object type <${global_segment_object_type}>"
                return
                 ;;
        esac

        sunmc_file=`/bin/echo ${SUNMC_CFG_DIR}/${families_file}`
        user_file=`/bin/echo ${USER_CFG_DIR}/${families_file}` 
        value=`/bin/echo ${global_segment_object_type}`
        if [ -f "$user_file" ] && [ -r "$user_file" ]
        then
           line=`findline $user_file 1 $value "[ \t]*"`
           if [ ${line} -ne -1 ]
           then
              errormsg "validate_segment_object_type" \
                 "Segment object type exists in <$user_file> line <$line>"
           fi
        else 
           if [ -f "$sunmc_file" ] && [ -r "$sunmc_file" ]
           then
              line=`findline $sunmc_file 1 $value "[ \t]*"`
              if [ ${line} -ne -1 ]
              then
                 errormsg "validate_segment_object_type" \
                    "Segment object type exists in <$sunmc_file> line <$line>"
              fi
           fi
        fi
        return
}

# subfunction: write_to_error_log
# inputs :
# outputs:
#
write_to_error_log ()
{
        if [ "$global_error_log" ]
        then
           /bin/echo "$global_error_log" >> ${LOGFILE}
        fi

        global_error_log=""
        global_cr=""

        error_flag=0
        return
}

# subfunction: errormsg
# inputs :
#   function name
#   error string
# outputs: none
#
errormsg ()
{
        write_to_error_log

        # reset error_flag back to 1 so that when the record is
        # finished processing it will call write_to_error_log again
        # to finish placing the current record in the error_log.
        #
        error_flag=1

        # Add errormsg as a comment to the error_log file
        #
        #em_msgstr1=`/usr/bin/gettext "$2"`
        #em_msgstr2=`eval /bin/echo "$em_msgstr1"`
        #/bin/echo "# $ERROR_LOG_KEY $1 : $em_msgstr2" >> ${LOGFILE}
        /bin/echo "# $ERROR_LOG_KEY $1: $2" >> ${LOGFILE}
}

# subfunction: add_device
# inputs : 
# outputs: 
#
add_device ()
{
        case ${global_object_type} in
            node) add_node ;;
            group) add_group ;;
            segment) add_segment ;;
            *) errormsg "add_device" \
                  "Invalid object type <${global_object_type}>" ;;
        esac
        return
}

# subfunction: add_node
# inputs : 
# outputs: 
#
add_node ()
{
        family_name=`/bin/echo ${global_node_object_type}`
        family_file=`/bin/echo ${family_name}-family-j.x`
        file=`/usr/bin/basename ${global_large_icon}`
        large_icon_file=`/bin/echo ${global_user_name}-${file}`
        file=`/usr/bin/basename ${global_small_icon}`
        small_icon_file=`/bin/echo ${global_user_name}-${file}`
        discovery_value=`/bin/echo ${global_i18n_key} ${global_search_parameter} ${family_name}`
        properties_file=`/bin/echo ${USER_PROPERTIES_DIR}/${global_user_name}.properties`


        case ${global_monitor_via} in
        # Add the host or platform device
        host|platform) create_node_family_file $global_monitor_via $family_file $large_icon_file $small_icon_file $global_user_name                        
                       # add to discovery file - Only for discovery filter
                       # purpose in 2.1 update
                       addline "$DISCOVERY_CFG_FILE" "$discovery_value" 
                       ;;
        # Add the module or proxy device
        module|proxy) families_file=`/bin/echo module-module-families-j.x`
                      addline "$families_file" "$family_name $global_i18n_key"
                      create_node_family_file $global_monitor_via $family_file $large_icon_file $small_icon_file $global_user_name
                       # for 3.0 we need to filter based on modules also
                       # addline "$DISCOVERY_CFG_FILE" "$discovery_value" 
                       ;;
        # Add the snmp or icmp or non-monitored device
        snmp|icmp|none) families_file=`/bin/echo ${global_monitor_via}-${global_node_type}-families-j.x` 
                        addline "$families_file" "$family_name $global_i18n_key"
                        create_node_family_file $global_monitor_via $family_file $large_icon_file $small_icon_file $global_user_name
                        addline "$DISCOVERY_CFG_FILE" "$discovery_value" 
                        ;;
        *)              errormsg "add_node" \
                           "Invalid node type <${global_monitor_via}>"
                        return
                        ;;
        esac

        # copy properties file to /var/opt/SUNWsymon/classes
        if [ -f "$global_properties_file" ] && [ -r "$global_properties_file" ]
        then
           if [ -f "$properties_file" ]
           then
              /usr/bin/rm $properties_file
           fi
           `/bin/echo "#" >> $properties_file`
           `/bin/echo "${families_comment1}" >> $properties_file`
           `/bin/echo "${families_comment2}" >> $properties_file`
           `/bin/echo "#" >> $properties_file`
           `/bin/cat $global_properties_file >> $properties_file`
        else
            errormsg "add_node" \
               "Cannot copy <$global_properties_file> file, check if file exists and permissions"
            return
        fi
 
        # copy icon files to /var/opt/SUNWsymon/cfg
        if [ -f "$global_large_icon" ] && [ -r "$global_large_icon" ]
        then
            /bin/cp "$global_large_icon" "$USER_CFG_DIR/$large_icon_file"
        else
           errormsg "add_node" \
              "Cannot copy <$global_large_icon> file, check if file exists and permissions"
           return
        fi

        if [ -f "$global_small_icon" ] && [ -r "$global_small_icon" ]
        then
            /bin/cp "$global_small_icon" "$USER_CFG_DIR/$small_icon_file"
        else
           errormsg "add_node" \
              "Cannot copy <$global_small_icon> file, check if file exists and permissions"
           return
        fi

        return
}

# subfunction: add_group
# inputs : 
# outputs: 
#
add_group ()
{
        family_name=`/bin/echo ${global_group_object_type}`
        family_file=`/bin/echo ${family_name}-family-j.x`
        file=`/usr/bin/basename ${global_large_icon}`
        large_icon_file=`/bin/echo ${global_user_name}-${file}`
        file=`/usr/bin/basename ${global_small_icon}`
        small_icon_file=`/bin/echo ${global_user_name}-${file}`
        properties_file=`/bin/echo ${USER_PROPERTIES_DIR}/${global_user_name}.properties`

        case ${global_group_type} in
        general) families_file="group-general-families-j.x"
                 ;;
        ipbased) families_file="group-ipbased-families-j.x"
                 ;;
        *)       errormsg "add_group" 
                    "Invalid group type <${global_group_type}>"
                 return
                 ;;
        esac

        addline "$families_file" "$family_name $global_i18n_key"
        create_group_family_file $family_file $large_icon_file $small_icon_file $global_user_name

        # copy properties file to /var/opt/SUNWsymon/classes
        if [ -f "$global_properties_file" ] && [ -r "$global_properties_file" ]
        then
           if [ -f "$properties_file" ]
           then
              /usr/bin/rm $properties_file
           fi
           `/bin/echo "#" >> $properties_file`
           `/bin/echo "${families_comment1}" >> $properties_file`
           `/bin/echo "${families_comment2}" >> $properties_file`
           `/bin/echo "#" >> $properties_file`
           `/bin/cat $global_properties_file >> $properties_file`
        else
            errormsg "add_group" \
               "Cannot copy <$global_properties_file> file, check if file exists and permissions"
            return
        fi

        # copy icon files to /var/opt/SUNWsymon/cfg
        if [ -f "$global_large_icon" ] && [ -r "$global_large_icon" ]
        then
            /bin/cp "$global_large_icon" "$USER_CFG_DIR/$large_icon_file"
        else
           errormsg "add_group" \
              "Cannot copy <$global_large_icon> file, check if file exists and permissions"
            return
        fi

        if [ -f "$global_small_icon" ] && [ -r "$global_small_icon" ]
        then
            /bin/cp "$global_small_icon" "$USER_CFG_DIR/$small_icon_file"
        else
           errormsg "add_group" \
              "Cannot copy <$global_small_icon> file, check if file exists and permissions"
            return
        fi

       return
}

# subfunction: add_segment
# inputs : 
# outputs: 
#
add_segment ()
{
        family_name=`/bin/echo ${global_segment_object_type}`
        family_file=`/bin/echo ${family_name}-family-j.x`
        file=`/usr/bin/basename ${global_large_icon}`
        large_icon_file=`/bin/echo ${global_user_name}-${file}`
        file=`/usr/bin/basename ${global_small_icon}`
        small_icon_file=`/bin/echo ${global_user_name}-${file}`
        properties_file=`/bin/echo ${USER_PROPERTIES_DIR}/${global_user_name}.properties`

        case ${global_segment_type} in
        bus) families_file="segment-bus-families-j.x"
                 ;;
        ring) families_file="segment-ring-families-j.x"
                 ;;
        *)       errormsg "add_segment" 
                    "Invalid segment type <${global_segment_type}>"
                 return
                 ;;
        esac

        addline "$families_file" "$family_name $global_i18n_key"
        create_segment_family_file $family_file $large_icon_file $small_icon_file $global_user_name

        # copy properties file to /var/opt/SUNWsymon/classes
        if [ -f "$global_properties_file" ] && [ -r "$global_properties_file" ]
        then
           if [ -f "$properties_file" ]
           then
              /usr/bin/rm $properties_file
           fi
           `/bin/echo "#" >> $properties_file`
           `/bin/echo "${families_comment1}" >> $properties_file`
           `/bin/echo "${families_comment2}" >> $properties_file`
           `/bin/echo "#" >> $properties_file`
           `/bin/cat $global_properties_file >> $properties_file`
        else
            errormsg "add_segment" \
               "Cannot copy <$global_properties_file> file, check if file exists and permissions"
            return
        fi

        # copy icon files to /var/opt/SUNWsymon/cfg
        if [ -f "$global_large_icon" ] && [ -r "$global_large_icon" ]
        then
            /bin/cp "$global_large_icon" "$USER_CFG_DIR/$large_icon_file"
        else
           errormsg "add_segment" \
              "Cannot copy <$global_large_icon> file, check if file exists and permissions"
            return
        fi

        if [ -f "$global_small_icon" ] && [ -r "$global_small_icon" ]
        then
            /bin/cp "$global_small_icon" "$USER_CFG_DIR/$small_icon_file"
        else
           errormsg "add_segment" \
              "Cannot copy <$global_small_icon> file, check if fileexists and permissions"
            return
        fi

       return
}

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

PROGNAME=`/usr/bin/basename $0`; export PROGNAME

# Check if superuser is executing this script
check_root

if [ $# -ne 2 ] ; then
    echo ""
    print_usage
    exit 1
fi

while getopts ad OPT
do
    case $OPT in
        a)
            global_operator="add"
            ;;
        d)
            global_operator="delete"
            ;;
        \?)
            print_usage
            exit 1
            ;;
    esac
done


# Check if the SUNWessrv package is installed
# If it is get the installed directory - BASEDIR
    /usr/bin/pkginfo -qi SUNWessrv
    if [ $? -ne 0 ] 
    then
        errormsg "Main" \
            "Package <$2> is not installed on this system"
        exit 1
    fi
    SUNMC_INSTALL_DIR=`/usr/bin/pkgparam SUNWessrv BASEDIR`

# Initialize rest of the variables
DATAFILE=`/usr/bin/basename $2`
VAROPTDIR=/var/opt/SUNWsymon
export VAROPTDIR
LOGDIR=$VAROPTDIR/install
LOGFILE="$LOGDIR/$PROGNAME.$DATAFILE"
    if [ ! -f "$LOGFILE" ] ; then
        /usr/bin/touch "$LOGFILE" >/dev/null 2>&1
        /usr/bin/chmod 640 "$LOGFILE"
        if [ $? -ne 0 ] ; then
            echo ""
            errormsg "Main" \
               "Unable to create logfile <$LOGFILE>"
            echo ""
            exit 1
        fi
    fi
export LOGFILE

SUNMC_CFG_DIR="$SUNMC_INSTALL_DIR/SUNWsymon/classes/base/console/cfg"
USER_CFG_DIR="$VAROPTDIR/cfg"
DISCOVERY_CFG_FILE="discovery-j.x"
USER_PROPERTIES_DIR="$VAROPTDIR/classes"
USER_ICON_DIR="$VAROPTDIR/cfg"
ERROR_LOG_KEY="ERRORLOG>"

error_flag=0
global_error_log=""
global_cr=""

families_comment1="# USER DEFINED INFORMATION FOLLOW"
families_comment2="# DO NOT REMOVE THIS COMMENT"

# clear all the global variables
unset_global_variables

# Check if file exists
if [ ! -f $2 ] 
then
    eval /bin/echo `/usr/bin/gettext '$2 file does not exist'`
    exit 1

fi

# Check if file has read permissions
if [ ! -r $2 ] 
then
    eval /bin/echo `/usr/bin/gettext '$2 file does not have read permissions'`
    exit 1

fi

# Read and process the file information
cat $2 | while read parameter value
    do
        first_value_word=`/bin/echo "$value" | /bin/awk '{ print $1 }'`
        if [ "$parameter" != "#" -o "$first_value_word" != "$ERROR_LOG_KEY" ]
        then
           global_error_log=`/bin/echo ${global_error_log}${global_cr}${parameter} ${value}`
           global_cr="\n"
        fi

        case ${parameter} in
            \#*) continue 
                 ;;
             "") continue
                 ;;
             -*) case ${global_operator} in
                    add)    process_add_data 
                            ;;
                    delete) process_delete_data
                            ;;
                    *)      echo "Invalid Operation: Operation not supported\n"
                            i;;
                 esac
                 ;;
             *)  set_global_variable 
                 ;;

        esac
    done
