#!/bin/sh
# Copyright 08/11/05 Sun Microsystems, Inc. All Rights Reserved.
# pragma ident  "@(#)es-config-server	1.6 05/08/11 Sun Microsystems"
#
# This script is used to update configuration of following file
#  - /var/opt/SUNWsymon/cfg/server-conconfig.x
# 
#
#
#
print_script_usage () {
    /usr/bin/echo ""
    eval /usr/bin/echo "\"`/usr/bin/gettext  'Usage: $PROGNAME [ -h | -p <port_id> ] \n Where the options represent:\n' `\""
    /usr/bin/gettext " Default : Displays present configuration.\n"
    /usr/bin/echo ""
    /usr/bin/gettext " -h      : Prints usage.\n"
    /usr/bin/echo ""
    /usr/bin/gettext " -p <port_id> : Configures port for rmi service.\n"
    exit 0
}

display_configuration () {


    RMIRECEPTOR_CONFIG_FILE="$VARDIR/server-config.x"
    if [ -r "$RMIRECEPTOR_CONFIG_FILE" ]; then
          old_port=`/usr/bin/cat "$RMIRECEPTOR_CONFIG_FILE" | /usr/bin/grep "property:rmiPort"  2> /dev/null`
          echolog '$2' "$old_port"
          old_port=`/usr/bin/echo "$old_port" | /usr/bin/cut -f2 -d=  2> /dev/null `
          old_port=`/usr/bin/echo $old_port 2> /dev/null `
          [ -z "$old_port" ] && echolog 'port for rmi service is not configured.'
          # No validation here. Will be done in ask_port_user later
   
    fi
    
    echolog ''
    echolog 'Use $2 -h to print the usage of this script.' "$BASEDIR/lib/sbin/es-config-server"
   
}

# Input $1 = port_id
#
#
configure_grouping_port() {

    ret_stat=1
    
    /usr/bin/expr $1 + 1  1> /dev/null  2>&1 
    ret_stat=$?
    if [ $ret_stat -eq 0 ] ; then
       if [ $1 -lt 1 -o $1 -gt 65535 ] ; then
         ret_stat=1
       fi         
    fi
    [ $ret_stat -ne 0 ] && echolog 'Port $2 is not a valid port number.' "$1" && return $ret_stat
    
    RMIRECEPTOR_CONFIG_FILE="$VARDIR/server-config.x"
    if [ -w "$RMIRECEPTOR_CONFIG_FILE" ]; then
         old_port=`/usr/bin/cat "$RMIRECEPTOR_CONFIG_FILE" | /usr/bin/grep "property:rmiPort"  2> /dev/null`
         old_port=`/usr/bin/echo "$old_port" | /usr/bin/cut -f2 -d=  2> /dev/null `
         old_port=`/usr/bin/echo $old_port 2> /dev/null `
         /usr/bin/mv -f "$RMIRECEPTOR_CONFIG_FILE" "${RMIRECEPTOR_CONFIG_FILE}.old"
         /usr/bin/sed  "s/property:rmiPort = $old_port/property:rmiPort = $1/" "${RMIRECEPTOR_CONFIG_FILE}.old" > "$RMIRECEPTOR_CONFIG_FILE"
         ret_stat=$?
    fi
 
    if [ $ret_stat -eq 0 ]; then
       SMREG=/usr/sbin/smreg
       registerSMLegacyLoginModule "$1"
       echolog 'property:rmiPort updated to $2' "$1"
    fi

    return $ret_stat
}

####################################################################################
# Main
####################################################################################
PROGNAME=$0
PREV_DIR=`/usr/bin/pwd`
TOOLS_DIR=`/usr/bin/dirname $PROGNAME`
SCRIPT_DIR=${TOOLS_DIR}/../../sbin
PATH="$PATH:${SCRIPT_DIR}:${TOOLS_DIR}"

[ ! -x "${SCRIPT_DIR}/es-common.sh" ] && exit 1

. ${SCRIPT_DIR}/es-common.sh

set_basedir

configure_port_flag=0
num_comp=0

while getopts hp: OPT
do
    case $OPT in
       h) print_script_usage
          ;;          
	  
       p) num_comp=`/usr/bin/expr $num_comp + 1`
          configure_port_flag=1
          port_id="$OPTARG"
          ;;
       
       *) num_comp=`/usr/bin/expr $num_comp + 1`
          print_script_usage
          exit 1
          ;;
     esac
done

if [ $num_comp -eq 0 ] ; then
   display_configuration
else
   if [ $num_comp -gt 1 ] ; then
      echolog ''
      echolog 'Invalid arguments.'
      echolog 'Use $2 -h to print the usage of this script.' "$BASEDIR/lib/sbin/es-config-server"
   else
      if [ $configure_port_flag -eq 1 ] ; then
         configure_grouping_port "$port_id"
         exit $?       
      fi
   fi
fi
  
exit 0

