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

# set -x

check_for_l10n() 
{

    L10N_REQUIRED=0
    if [ -n "$LANG" ] && [ "$LANG" != "C" ]; then
        if [ "$LANG" != "zh_TW" ]; then
            L10N_CODE=`/usr/bin/expr "$LANG" : '\(..\).*'`
            LC_MES_CODE=`/usr/bin/expr "$LC_MESSAGES" : '\(..\).*'`
            if [ "$LC_MES_CODE" = "$L10N_CODE" ]; then
              LC_MESSAGES=$LANG
              export LC_MESSAGES
            fi
        else
            L10N_CODE=$LANG
        fi
        [ "$L10N_CODE" != "en" ] && L10N_REQUIRED=1
    else
        L10N_CODE=C
    fi
}

setup_textdomain () 
{

    [ "$L10N_REQUIRED" -eq 0 ] && return

    TEXTDOMAINDIR=$VAROPTDIR/locale
    export TEXTDOMAINDIR

    TEXTDOMAIN=ES_SCRIPTS
    export TEXTDOMAIN

} 

multiline_echolog() {
    eval echo "\"`/usr/bin/gettext "$1"`\""
    if [ "$LOGFILE" != "" ] ; then
        eval echo "\"`/usr/bin/gettext "$1"`\"" >> "$LOGFILE"
    fi
}

echolog() 
{
    eval echo "`/usr/bin/gettext "$1"`"
    if [ "$LOGFILE" != "" ] ; then
        eval echo "`/usr/bin/gettext "$1"`" >> "$LOGFILE"
    fi
    error_flag=1

}

#
# The following function is to be used when reporting warnings
# and errors while processing the file contents. However, the file
# access errors etc should always be reported by directly using the
# echolog() function.
# The -v option sets the do_verbose flag to 1.
#
echo_v_log()
{
    if [ $do_verbose -eq 1 ] ; then
        echolog "$1" "$2" "$3"
    fi
    error_flag=1
}

#
# 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 ()
{
    echo ""
    multiline_echolog 'Usage: $2 -{a|d}[v] Filename\n   where the options represent:\n' "$PROGNAME"
    echo ""
    echolog '     a : Add new device'
    /usr/bin/gettext '     (for example: ./es-device -a /tmp/userdata)'
    echo ""
    echolog '     d : Delete device'
    /usr/bin/gettext '     (for example: ./es-device -d /tmp/userdata)'
    echo ""
    echolog '     v : Verbose option, prints any errors while processing the contents of input file'
    echo "\n"

}

check_root() 
{
    ME=`/usr/bin/id | /usr/bin/cut -f2 -d'(' | /usr/bin/cut -f1 -d')' 2> /dev/null`

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

    echo ""
    echolog "This action is limited to superuser only."
    echolog 'Exiting $2.' "$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
#    hardware module name (eg: Config-Reader4u)
# 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_hardwaremodule=$6
   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
            ;;
         *)
            echo_v_log 'create_node_family_file: Error! Invalid node object type $2' "$cff_Node_object_type"
            return
            ;;
      esac

      case "$cff_Node_object_type" in
         host | platform)
            /bin/echo "[ load sun-host-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) = deviceimages/$cff_Large_icon_file" >> $cff_outfile
      /bin/echo "smallIcon(DFT) = deviceimages/$cff_Small_icon_file" >> $cff_outfile
      /bin/echo "" >> $cff_outfile
      if [ "$cff_Node_object_type" = "host" -o \
           "$cff_Node_object_type" = "platform" ]
      then
         /bin/echo "navigateCommand = launchUniqueFrame %windowID \
tools-hostdetails-j.x nodeName=%nodeName topologyDescUrl=%topologyDescUrl \
agentHost=%targetHost agentPort=%targetPort objectUrl=%targetUrl \
contextUrl=%contextUrl modName=$cff_hardwaremodule" >> $cff_outfile
         /bin/echo "" >> $cff_outfile
         /bin/echo "commandLabel(hostdetail) = \
base.console.ConsoleGeneric:standard.hostdet" >> $cff_outfile
/bin/echo "commandSpec(hostdetail) = launchUniqueFrame %windowID \
tools-hostdetails-j.x nodeName=%nodeName topologyDescUrl=%topologyDescUrl \
agentHost=%targetHost agentPort=%targetPort objectUrl=%targetUrl \
contextUrl=%contextUrl modName=$cff_hardwaremodule" >> $cff_outfile

      fi

      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 moddetail" \
               >> $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(moddetail) = \
base.console.ConsoleGeneric:standard.hostdet" >> $cff_outfile

         /bin/echo "commandSpec(moddetail) = 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
       return
   fi
   /usr/bin/chmod 644 "$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) = deviceimages/$cff_Large_icon_file" >> $cff_outfile
      /bin/echo "smallIcon(DFT) = deviceimages/$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 644 "$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) = deviceimages/$cff_Large_icon_file" >> $cff_outfile
      /bin/echo "smallIcon(DFT) = deviceimages/$cff_Small_icon_file" >> $cff_outfile
      /usr/bin/chmod 644 "$cff_outfile"
    fi

    return
}

# subfunction: create_composite_family_file
# inputs : 
#    family file name
#    large icon file name
#    small icon file name
#    username
# outputs: none
#
create_composite_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) = deviceimages/$cff_Large_icon_file" >> $cff_outfile
      /bin/echo "smallIcon(DFT) = deviceimages/$cff_Small_icon_file" >> $cff_outfile

      /bin/echo "" >> $cff_outfile

      /bin/echo "commands = attedit pasteinto alarmaction" >> $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 "commandLabel(pasteinto) = base.console.ConsoleTopology:edit.pasteinto" >> $cff_outfile
      /bin/echo "commandSpec(pasteinto) = pasteInto %windowID %targetUrl" >> $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 644 "$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 644 "$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 [ "${first_value_word}" = "" -o "${first_value_word}" = "\t" -o "${first_value_word}" = " " ]
	then
                echo_v_log 'set_global_variable: Value not specified for $2' "$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=${first_value_word} ;;
           OBJECT_TYPE)              global_object_type=${first_value_word} ;;
           NODE_OBJECT_TYPE)         global_node_object_type=${first_value_word} ;;
           GROUP_OBJECT_TYPE) global_group_object_type=${first_value_word} ;;
           SEGMENT_OBJECT_TYPE) global_segment_object_type=${first_value_word} ;;
           COMPOSITE_OBJECT_TYPE) global_composite_object_type=${first_value_word} ;;
           MONITOR_VIA)              global_monitor_via=${first_value_word} ;; 
           NODE_TYPE)                global_node_type=${first_value_word} ;;
           I18N_KEY)                 global_i18n_key=${first_value_word} ;;
           PROPERTIES_FILE)          global_properties_file=${first_value_word}
                                     first_char=`/bin/echo $global_properties_file | /usr/bin/cut -c 1`
                                     if [ "${first_char}" != "/" ]
                                     then
                                         global_properties_file="$SUNMC_INSTALL_DIR/$global_properties_file"
                                     fi
                                     ;;
           SEARCH_PARAMETER)         global_search_parameter=${first_value_word} ;;
           LARGE_ICON)               global_large_icon=${first_value_word}
                                     first_char=`/bin/echo $global_large_icon | /usr/bin/cut -c 1`
                                     if [ "${first_char}" != "/" ]
                                     then
                                         global_large_icon="$SUNMC_INSTALL_DIR/$global_large_icon"
                                     fi
                                     ;;
           SMALL_ICON)               global_small_icon=${first_value_word}
                                     first_char=`/bin/echo $global_small_icon | /usr/bin/cut -c 1`
                                     if [ "${first_char}" != "/" ]
                                     then
                                         global_small_icon="$SUNMC_INSTALL_DIR/$global_small_icon"
                                     fi
                                     ;;
           GROUP_TYPE)               global_group_type=${first_value_word} ;;
           SEGMENT_TYPE)             global_segment_type=${first_value_word} ;;
           DEVICE_ID)                global_device_id=${first_value_word} ;;
           HARDWARE_MODULE)          global_hardware_module=${first_value_word} ;;
	   *)                        echo_v_log 'set_global_variable: Invalid parameter $2' "$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_composite_object_type=""
    global_hardware_module=""
    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 ;;
            composite) delete_composite ;;
            "") error_flag=1 ;;
            *) echo_v_log 'process_delete_data: Object type $2 missing in data file' "$global_object_type" ;;
        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
             echo_v_log "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
            echo_v_log 'delete_node: $2 does not exist, verify input' "$family_file"
            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
             echo_v_log "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
            echo_v_log 'delete_group: $2 does not exist, verify input' "$family_file"
            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"
                 ;;
        *)       echo_v_log 'delete_group: Invalid group type $2' "$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
             echo_v_log "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
            echo_v_log 'delete_segment: $2 does not exist, verify input' "$family_file"
            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"
                 ;;
        *)       echo_v_log 'delete_segment: Invalid segment type $2' "$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:delete_composite
# inputs :
# outputs:
#
delete_composite ()
{
        # check if required parameters are present
        if [ "$global_user_name" = "" -o "$global_composite_object_type" = "" -o "$global_i18n_key" = "" ]
        then
             echo_v_log "delete_composite: Invalid data in input file"
             return
        fi 

        family_name=`/bin/echo ${global_composite_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
            echo_v_log 'delete_composite: $2 does not exist, verify input' "$family_file"
            return
        fi

        families_file="composite-objects-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, remove corresponding family file.
	# remove corresponsing discovery file entry
        /usr/bin/grep -s "$family_name" ${USER_CFG_DIR}/*families-j.x > /dev/null
        remove_discovery_entry "$family_name"


        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
    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 ;;
            composite) validate_composite_data ;;
            *)       echo_v_log 'validate_object_type: Invalid value $2 for Object_type' "$global_object_type" ;;
        esac
        return
}

# subfunction: validate_node_data
# inputs : 
# outputs: 
#
validate_node_data ()
{
        validate_user_name
        if [ "${error_flag}" -eq 1 ]
        then
            return
        fi

        validate_monitor_via
        if [ "${error_flag}" -eq 1 ]
        then
            return
        fi

        validate_node_type
        if [ "${error_flag}" -eq 1 ]
        then
            return
        fi

        validate_node_object_type
        if [ "${error_flag}" -eq 1 ]
        then
            return
        fi

        validate_i18n_key
        if [ "${error_flag}" -eq 1 ]
        then
            return
        fi

        validate_properties_file
        validate_search_parameter
        validate_icon_file $global_user_name $global_large_icon
        validate_icon_file $global_user_name $global_small_icon
        validate_hardware_module
        return
}

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

        if [ "${global_user_name}" = "" ]
        then
            echo_v_log "validate_user_name: No value specified for User_name"
            return
        fi

}

# 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) ;;
        *)  echo_v_log 'validate_monitor_via : Invalid value $2 for Monitor_via' "$global_monitor_via" ;;
    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) ;;
        *)   echo_v_log 'validate_node_type:Invalid value $2 for Node_type' "$global_node_type" ;;
    esac
    return
}

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

        # Checkif value exists. If not return error
        if [ "$global_node_object_type" = "" ]
        then
            echo_v_log "validate_node_object_type: No value specified for Node_object_type"
            return
        fi

        # 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
                           echo_v_log 'validate_node_object_type: $2 or $3 exists' "$user_file" "$sunmc_file"
                       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
                              echo_v_log 'validate_node_object_type: Node object type exists in $2 line $3' "$user_file" "$line"
                          fi
                      else 
                          if [ -f "$sunmc_file" ] && [ -r "$sunmc_file" ]
                          then
                              line=`findline $sunmc_file 1 $value "[ \t]*"`
                              if [ ${line} -ne -1 ]
                              then
                                  echo_v_log 'validate_node_object_type: Node object type exists in $2 line $3' "$sunmc_file" "$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
                                echo_v_log 'validate_node_object_type: Node object type exists in $2 line $3' "$user_file" "$line"
                            fi
                        else 
                            if [ -f "$sunmc_file" ] && [ -r "$sunmc_file" ]
                            then
                                line=`findline $sunmc_file 1 $value "[ \t]*"`
                                if [ ${line} -ne -1 ]
                                then
                                    echo_v_log 'validate_node_object_type: Node object type exists in $2 line $3' "$sunmc_file" "$line"
                                fi
                            fi
                        fi
                        ;;
        *)              echo_v_log 'validate_node_object_type: Invalid object type $2' "$global_monitor_via" ;;
        esac
        return
}

# subfunction: validate_i18n_key
# inputs : 
# outputs: 
#
validate_i18n_key ()
{
    if [ "$global_i18n_key" = "" ]
    then
        echo_v_log "validate_i18n_key: No value specified for i18n_key"
        return
    fi

    #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
             ;;  
    composite) validate_composite_i18n_key
             ;;  
    *)       echo_v_log 'validate_i18n_key: Invalid object type $2' "$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
                           echo_v_log 'validate_node_object_type: $2 or $3 exists' "$user_file" "$sunmc_file"
                       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
                              echo_v_log 'validate_i18n_key: Node object type exists in $2 line $3' "$user_file" "$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
                                  echo_v_log 'validate_i18n_key: Node object type exists in $2 line $3' "$sunmc_file" "$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
                                echo_v_log 'validate_i18n_key: Node object type exists in $2 line $3' "$user_file" "$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
                                    echo_v_log 'validate_i18n_key: Node object type exists in $2 line $3' "$sunmc_file" "$line"
                                 fi
                            fi
                        fi
                        ;;
        *)              echo_v_log 'validate_i18n_key: Invalid i18n key: $2' "$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`
                 ;;
        *)   echo_v_log 'validate_group_i18n_type: $2 is invalid group type' "$global_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
          echo_v_log 'validate_group_i18n_key: Group object type exists in $2 line $3' "$user_file" "$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
             echo_v_log 'validate_group_i18n_key: Group object type exists in $2 line $3' "$sunmc_file" "$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`
                 ;;
        *)   echo_v_log 'validate_segment_i18n_type: $2 is invalid segment type' "$global_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
          echo_v_log 'validate_segment_i18n_key: Segment object type exists in $2 line $3' "$user_file" "$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
             echo_v_log 'validate_segment_i18n_key: Segment object type exists in $2 line $3' "$sunmc_file" "$line"
          fi
       fi
    fi
        return
}

# subfunction: validate_composite_i18n_key
# inputs :
# outputs:
#
validate_composite_i18n_key ()
{
    families_file="composite-objects-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
          echo_v_log 'validate_composite_i18n_key: Composite object type exists in $2 line $3' "$user_file" "$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
             echo_v_log 'validate_composite_i18n_key: Composite object type exists in $2 line $3' "$sunmc_file" "$line"
          fi
       fi
   fi
        return
}

# subfunction: validate_properties_file
# inputs : 
# subfunction: validate_properties_file
# inputs : 
# outputs: 
#
validate_properties_file ()
{
        if [ "$global_properties_file" = "" ]
        then
            /usr/bin/gettext  "validate_properties_file: No value specified for Properties_file"
            echo ""
            return
        fi


        file=`/usr/bin/basename ${global_properties_file}`
        file_prefix=`/bin/echo ${file} | cut -d "." -f 1`
        if [ ${file_prefix} != ${global_user_name} ]
        then
            echo_v_log 'validate_properties_file: Invalid property file name. Must be $2' "$global_user_name.properties"
        fi
        return
}

# subfunction: validate_search_parameter
# inputs : 
# outputs: 
#
validate_search_parameter ()
{
        if [ "$global_search_parameter" = "" ]
        then
            /usr/bin/gettext "validate_search_parameter: No value specified for Search_parameter"
            echo ""
            return
        fi

        /bin/echo "$global_search_parameter" | /usr/bin/grep -s "^.*|.*|[\t ]*$" > /dev/null
        if [ $? -ne 0 ]
        then
           echo_v_log 'validate_search_parameter: Invalid sysoid value $2' "$global_search_parameter"
        fi

   return
}

# subfunction: validate_icon_file
# note   : Takes in username and icon filename for arguments
# inputs : 
# outputs: 
#
validate_icon_file ()
{
        if [ "$2" = "" ]
        then
            /usr/bin/gettext "validate_icon_file: No value specified for icon"
            echo ""
            return
        fi

        #user_file=`/usr/bin/basename $2`
        #icon_file=`/bin/echo $USER_CFG_DIR/$1-$user_file`
        if [ ! -f $2 ]
        then
           echo_v_log "validate_icon_file: Check if file exists and permissions"
        fi
        return
}
# subfunction: validate_hardware_module
# note   : Check if hardware module name is given
# inputs : 
# outputs: 
#
validate_hardware_module ()
{
        # First make sure everything is in lowercase
        lc_value=`/bin/echo ${global_monitor_via} | tr '[A-Z]' '[a-z]' `
        global_monitor_via=${lc_value}

        case ${global_monitor_via} in
        # validate object for host and platform
        host|platform) if [ "$global_hardware_module" = "" ]
                       then
                           echo_v_log "validate_hardware_module: Hardware Module name does not exist"
                       fi
                       ;;
        *) ;;
        esac
        return
}
# subfunction: validate_group_data
# inputs : 
# outputs: 
#
validate_group_data ()
{
        validate_user_name
        if [ "${error_flag}" -eq 1 ]
        then
            return
        fi

        validate_group_type
        if [ "${error_flag}" -eq 1 ]
        then
            return
        fi

        validate_group_object_type
        if [ "${error_flag}" -eq 1 ]
        then
            return
        fi

        validate_i18n_key
        if [ "${error_flag}" -eq 1 ]
        then
            return
        fi

        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) ;;
        *)   echo_v_log 'validate_group_type: Invalid value $2 for Group_type' "$global_group_type" ;;
    esac
    return
}

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

    value=`/bin/echo ${global_group_object_type}`

    # Checkif value exists. If not return error
    if [ "$value" = "" ]
    then
        echo_v_log "validate_group_object_type: No value specified for Group_object_type"
        return
    fi
    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
        *)      echo_v_log 'validate_group_object_type: Invalid object type $2' "$global_group_object_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 1 $value "[ \t]*"`
           if [ ${line} -ne -1 ]
           then
              echo_v_log 'validate_group_object_type: Group object type exists in $2 line $3' "$user_file" "$line"
           fi
        else 
           if [ -f "$sunmc_file" ] && [ -r "$sunmc_file" ]
           then
              line=`findline $sunmc_file 1 $value "[ \t]*"`
              if [ ${line} -ne -1 ]
              then
                 echo_v_log 'validate_group_object_type: Group object type exists in $2 line $3' "$sunmc_file" "$line"
              fi
           fi
        fi
        return
}

# subfunction: validate_segment_data
# inputs : 
# outputs: 
#
validate_segment_data ()
{
        validate_user_name
        if [ "${error_flag}" -eq 1 ]
        then
            return
        fi

        validate_segment_type
        if [ "${error_flag}" -eq 1 ]
        then
            return
        fi

        validate_segment_object_type
        if [ "${error_flag}" -eq 1 ]
        then
            return
        fi

        validate_i18n_key
        if [ "${error_flag}" -eq 1 ]
        then
            return
        fi

        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) ;;
        *)   echo_v_log 'validate_segment_type: Invalid value $2 for Segment_type' "$global_segment_type" ;;
    esac
    return

}

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

    value=`/bin/echo ${global_segment_object_type}`

    # Checkif value exists. If not return error
    if [ "$value" = "" ]
    then
        echo_v_log "validate_segment_object_type: No value specified for Segment object type"
        return
    fi

    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
        *)      echo_v_log 'validate_segment_object_type: Invalid object type $2' "$global_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 1 $value "[ \t]*"`
           if [ ${line} -ne -1 ]
           then
              echo_v_log 'validate_segment_object_type: Segment object type exists in $2 line $3' "$user_file" "$line"
           fi
        else 
           if [ -f "$sunmc_file" ] && [ -r "$sunmc_file" ]
           then
              line=`findline $sunmc_file 1 $value "[ \t]*"`
              if [ ${line} -ne -1 ]
              then
                 echo_v_log 'validate_segment_object_type: Segment object type exists in $2 line $3' "$sunmc_file" "$line"
              fi
           fi
        fi
        return
}

# subfunction: validate_composite_data
# inputs : 
# outputs: 
#
validate_composite_data ()
{
        validate_user_name
        if [ "${error_flag}" -eq 1 ]
        then
            return
        fi

        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: 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: add_device
# inputs : 
# outputs: 
#
add_device ()
{
        case ${global_object_type} in
            node) add_node ;;
            group) add_group ;;
            segment) add_segment ;;
            composite) add_composite ;;
            *) echo_v_log 'add_device: Invalid object type $2' "$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 $global_hardware_module 
                       # 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 $global_hardware_module
                       # 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 $global_hardware_module
                        addline "$DISCOVERY_CFG_FILE" "$discovery_value" 
                        ;;
        *)              echo_v_log 'add_node: Invalid node type $2' "$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
            echo_v_log 'add_node: Cannot copy $2 file, check if file exists and permissions' "$global_properties_file"
            return
        fi
 
        # copy icon files to /var/opt/SUNWsymon/cfg/deviceimages
        if [ -f "$global_large_icon" ] && [ -r "$global_large_icon" ]
        then
            /bin/cp "$global_large_icon" "$USER_IMAGES_DIR/$large_icon_file"
        else
           echo_v_log 'add_node: Cannot copy $2 file, check if file exists and permissions' "$global_large_icon"
           return
        fi

        if [ -f "$global_small_icon" ] && [ -r "$global_small_icon" ]
        then
            /bin/cp "$global_small_icon" "$USER_IMAGES_DIR/$small_icon_file"
        else
           echo_v_log 'add_node: Cannot copy $2 file, check if file exists and permissions' "$global_small_icon"
           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"
                 ;;
        *)       echo_v_log 'add_group: Invalid group type $2' "$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
            echo_v_log 'add_group: Cannot copy $2 file, check if file exists and permissions' "$global_properties_file"
            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_IMAGES_DIR/$large_icon_file"
        else
           echo_v_log 'add_group: Cannot copy $2 file, check if file exists and permissions' "$global_large_icon"
            return
        fi

        if [ -f "$global_small_icon" ] && [ -r "$global_small_icon" ]
        then
            /bin/cp "$global_small_icon" "$USER_IMAGES_DIR/$small_icon_file"
        else
           echo_v_log 'add_group: Cannot copy $2 file, check if file exists and permissions' "$global_small_icon"
            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"
                 ;;
        *)       echo_v_log 'add_segment: Invalid segment type $2' "$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
            echo_v_log 'add_segment: Cannot copy $2 file, check if file exists and permissions' "$global_properties_file"
            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_IMAGES_DIR/$large_icon_file"
        else
           echo_v_log 'add_segment: Cannot copy $2 file, check if file exists and permissions' "$global_large_icon"
            return
        fi

        if [ -f "$global_small_icon" ] && [ -r "$global_small_icon" ]
        then
            /bin/cp "$global_small_icon" "$USER_IMAGES_DIR/$small_icon_file"
        else
           echo_v_log 'add_segment: Cannot copy $2 file, check if fileexists and permissions' "$global_small_icon"
            return
        fi

       return
}

# subfunction: add_composite
# inputs : 
# outputs: 
#
add_composite ()
{
        family_name=`/bin/echo ${global_composite_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`

        families_file="composite-objects-families-j.x"

        addline "$families_file" "$family_name $global_i18n_key"
        create_composite_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
            echo_v_log 'add_composite: Cannot copy $2 file, check if file exists and permissions' "$global_properties_file"
            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_IMAGES_DIR/$large_icon_file"
        else
           echo_v_log 'add_composite: Cannot copy $2 file, check if file exists and permissions' "$global_large_icon"
            return
        fi

        if [ -f "$global_small_icon" ] && [ -r "$global_small_icon" ]
        then
            /bin/cp "$global_small_icon" "$USER_IMAGES_DIR/$small_icon_file"
        else
           echo_v_log 'add_composite: Cannot copy $2 file, check if file exists and permissions' "$global_small_icon"
            return
        fi
    
       global_search_parameter="||"
       discovery_value=`/bin/echo ${global_i18n_key} ${global_search_parameter} ${family_name}`
       addline "$DISCOVERY_CFG_FILE" "$discovery_value"

       return
}

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

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

VAROPTDIR=/var/opt/SUNWsymon
export VAROPTDIR

check_for_l10n
setup_textdomain

# Check if superuser is executing this script
check_root

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

argcount=0

# The parameter that indicates whether we want diagnostic messages or not.
do_verbose=0

# A dummy value is given to global_operator for subsequent checks.
global_operator="foo"

while getopts adv OPT
do
    case $OPT in
        a)
            #
            # Check if for some reason, the global_operator is already
            #   set. In such case, print error and exit. This will protect
            #   when both add and delete are given as arguments at the
            #   same time.
            #
            if [ "$global_operator" != "foo" ] ; then
                print_usage
                exit 1
            fi
            global_operator="add"
            argcount=`expr $argcount + 1`
            ;;
        d)
            #
            # Check if for some reason, the global_operator is already
            #   set. In such case, print error and exit. This will protect
            #   when both add and delete are given as arguments at the
            #   same time.
            #
            if [ "$global_operator" != "foo" ] ; then
                print_usage
                exit 1
            fi
            global_operator="delete"
            argcount=`expr $argcount + 1`
            ;;
	v)
	    do_verbose=1
	    argcount=`expr $argcount + 1`
	    ;;
        \?)
            print_usage
            exit 1
            ;;
    esac
done

# What if there is no add or delete at all? Somebody may simply use
#   "es-device - filename" or "es-device -v filename"
#
if [ "$global_operator" = "foo" ] ; then
        print_usage
        exit 1
fi

# Check if the SUNWessrv package is installed
# If it is get the installed directory - BASEDIR
    /usr/bin/pkginfo -qi SUNWessrv
    if [ $? -ne 0 ] 
    then
        echolog "Package SUNWessrv 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`
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 ""
            echolog `Unable to create logfile $2` "$LOGFILE"
            echo ""
            exit 1
        fi
    fi
export LOGFILE

SUNMC_CFG_DIR="$SUNMC_INSTALL_DIR/SUNWsymon/classes/base/console/cfg"
USER_CFG_DIR="$VAROPTDIR/cfg"
USER_IMAGES_DIR="$USER_CFG_DIR/deviceimages"
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 FOLLOWS"
families_comment2="# DO NOT REMOVE THIS COMMENT"

# Create the directories if they do not exist
mkdir -p $USER_PROPERTIES_DIR
mkdir -p $USER_CFG_DIR
mkdir -p $USER_IMAGES_DIR

# clear all the global variables
unset_global_variables

# Check if the file exists
if [ ! -f $2 ] 
then
    echolog '$2 file does not exist' "$2"
    exit 1

fi

# Check if file has read permissions
if [ ! -r $2 ] 
then
   echolog '$2 file does not have read permissions' "$2"
    exit 1

fi

global_object_type=""
# 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
                 ;;
             }*|-*) if [ "$global_object_type" != "" ]
                    then
                        case ${global_operator} in
                            add)    process_add_data 
                                    unset_global_variables
                                    ;;
                            delete) process_delete_data
                                    unset_global_variables
                                    ;;
                            *)      echolog "Invalid Operation: Operation not supported"
                                    ;;
                         esac
                    fi
                    ;;
             *) if [ "$first_value_word" = "implements" ]
                then
                     # Process whatever you have read this far
                    if [ "$global_object_type" != "" ]
                    then
                        case ${global_operator} in
                             add)    process_add_data 
                                     unset_global_variables
                                     ;;
                             delete) process_delete_data
                                     unset_global_variables
                                     ;;
                             *)      echolog "Invalid Operation: Operation not supported"
                                     ;;
                         esac
                     fi

                     #convert the parameter to uppercase
                     global_object_type=`/bin/echo "$value" | /bin/awk '{ print $2 }'`
                     global_object_type=`/bin/echo ${global_object_type} | tr '[a-z]'  '[A-Z]' `
                     case ${global_object_type} in
                         NODE) global_node_object_type=${parameter} ;;
                         GROUP) global_group_object_type=${parameter} ;;
                         SEGMENT) global_segment_object_type=${parameter} ;;
                         COMPOSITE) global_composite_object_type=${parameter} ;;
                     esac
                 else
                     set_global_variable
                     first_value_word=""
                 fi
                 ;;
        esac
    done
