#!/bin/ksh

# Copyright (c) 2001 Sun Microsystems Inc.,
# All rights reserved.

#pragma ident   "@(#)mcfgconfig.ksh	1.14 1.14        04/06/09 SMI"

#
# This shell script prompts for the data and place the returned values
# in the shared configuration file, which is located in /var/opt/SUNWWDRr/cfg
# directory. Defaults will be offered equal to the present value if there is
# a valid value already, if no valid value out there, put "-1" as the defaults.
#
# It will use the executable program 'setconfigfile' to read from the write
# to the shared configuration file.
#
# Syntax for read:	setconfigfile -r config_data_index
# Syntax for write:	setconfigfile -w config_data_index value
#

# Set default textdomain for I18N.
TEXTDOMAIN=SUNW_WDR
export TEXTDOMAIN

# location of the shared configuration file
SHAREDCONFIGFILE=cfg
BASEDIR=/var/opt/SUNWmcfg/cfg

# location of the executable program used by this script
SETCONFIGFILE=/opt/SUNWmcfg/bin/setconfigfile

#
# function used to initiate shared configuration file
#
# NOTE: we don't have to initiate other config data because the
#       libmcfg.so will take care of them while initating the first one.
#
initConfigFile() {
    gettext "SC IP Address or Hostname: "
    read inputValue 
    if [ "aa${inputValue}" = "aa" ]; then
	inputValue="-1"
    fi

    result=`${SETCONFIGFILE} -w 0 ${inputValue}`
}

#
# function used to update shared configuration file
#
updateConfigFile() {
    if [ "$1" = "SCIPADDRESS" ]; then
	index=0	
    elif [ "$1" = "PLATFORMRCOMM" ]; then
	index=1	
    elif [ "$1" = "PLATFORMWCOMM" ]; then
	index=2	
    elif [ "$1" = "DOMAINAIP" ]; then
	index=3	
    elif [ "$1" = "DOMAINBIP" ]; then
	index=4	
    elif [ "$1" = "DOMAINCIP" ]; then
	index=5	
    elif [ "$1" = "DOMAINDIP" ]; then
	index=6	
    else
	gettext "No match"
	exit 1	
    fi


    #get the value that already exists in the shared config file.
    existValue=`${SETCONFIGFILE} -r ${index}`
    if [ $? != 0 ]; then
	printf "%s" "${existValue}"
	return 1
    fi
    if [ ${existValue} = "-1" ]; then
	echo ": \c"
    else
	printf "[%s]:" "${existValue}"
    fi
    read configValue
    if [ "aa${configValue}" = "aa" ]; then
	if [ "aa${existValue}" = "aa" ]; then
	    # This should not happen unless the user change the file manually
	    configValue="-1"
	else
	    # User wants to keep the existed value
	    return 0
	fi
    fi

    result=`${SETCONFIGFILE} -w ${index} ${configValue}`
    if [ $? != 0 ]; then
	printf "%s" "${result}"
	return 1
    fi
    return 0
}

#
# Execution begins here
#

umask go-wx

gettext "\n*****************************************************************************\n"
gettext "\nWARNING: The WDR feature is obsolete. Please discontinue use of this feature.\n" 
gettext "\n*****************************************************************************\n"

if [ ! -d "${BASEDIR}" ]; then
    mkdir -m 0755 -p ${BASEDIR}
fi

if [ $# -gt 0 ]; then
    format="$(gettext "Usage: %s [No argument needed]")"
    printf "$format" "$0"
    echo
    exit 1
fi

#
# ask user if he wants to continue configuring the file
#
echo
while [ 1 -eq 1 ]
do
    gettext "Do you want to configure the shared config file now? [yes] "
    read answer
    echo
    answer2=`printf "%s" "${answer}" | tr "[a-z]" "[A-Z]"`
    if [ "aa${answer2}" = "aa" -o "${answer2}" = "YES" -o "${answer2}" = "Y" ]
    then
	gettext "Initiate Shared Configuration File"
	echo
	gettext "----------------------------------"
	echo
	break	
    elif [ "${answer2}" = "N" -o "${answer2}" = "NO" ]
    then
	exit 0
    else
	continue
    fi
done

#
# initiate the shared configuration file if it doesn't exist,
# or in case it exists but contains no data, which should not
# happen because this file must not be initiate and modified
# manually.
#
if [ ! -f "${BASEDIR}/${SHAREDCONFIGFILE}" -o \
		! -s "${BASEDIR}/${SHAREDCONFIGFILE}" ]; then
    initConfigFile
else
    gettext "SC IP Address or Hostname"
    updateConfigFile "SCIPADDRESS"
    if [ $? -ne 0 ]; then
	gettext "\nPlease check the file and try again\n"
	exit 1
    fi

fi

#
# update other data in the shared configuration file
#
#
# update platform public community string
gettext "platformRComm"
updateConfigFile "PLATFORMRCOMM"
if [ $? -ne 0 ]; then
    gettext "\nPlease check the file and try again\n"
    exit 1
fi

# update platform private community string
gettext "platformWComm"
updateConfigFile "PLATFORMWCOMM"
if [ $? -ne 0 ]; then
    gettext "\nPlease check the file and try again\n"
    exit 1
fi

# update domain A's IP address
gettext "domainA IP Address or Hostname"
updateConfigFile "DOMAINAIP"
if [ $? -ne 0 ]; then
    gettext "\nPlease check the file and try again\n"
    exit 1
fi

# update domain B's IP address
gettext "domainB IP Address or Hostname"
updateConfigFile "DOMAINBIP"
if [ $? -ne 0 ]; then
    gettext "\nPlease check the file and try again\n"
    exit 1
fi

# update domain C's IP address
gettext "domainC IP Address or Hostname"
updateConfigFile "DOMAINCIP"
if [ $? -ne 0 ]; then
    gettext "\nPlease check the file and try again\n"
    exit 1
fi

# update domain D's IP address
gettext "domainD IP Address or Hostname"
updateConfigFile "DOMAINDIP"
if [ $? -ne 0 ]; then
    gettext "\nPlease check the file and try again\n"
    exit 1
fi
