#!/bin/ksh

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

#pragma ident   "@(#)wdrconfig.ksh	1.13 1.13        03/11/11 SMI"

#
# This shell script initializes the Serengeti WDR configuration file,
# and compiles the WBEM DR MOF files.
#

PROG=$(basename $0)
PATH=/usr/bin:/usr/sbin:/usr/sadm/bin
MOFDIR=/usr/sadm/mof
# Set default textdomain for I18N.
TEXTDOMAIN=SUNW_WDR
export TEXTDOMAIN


# location of the shared configuration file
WDRCONFIGFILE=serengeti-wdr-cfg.dat
BASEDIR=/var/opt/SUNWWDR/cfg

#############################################################################
#
# yes_or_no
#
# Query the user for yes or no input.
#
# $1 - prompt, should not include final newline or "(y/n)"
# Returns true (0) for yes, false (1) for no.
#
#############################################################################
yes_or_no()
{
	typeset response=""
	# TRANSLATION_NOTE - this is the format for yes or no prompts
	typeset yn=$(gettext "[y,n]")
	typeset fmt="$1"
	shift

	while true
	do
		printf "$fmt $yn " "$@"
		read response
		printf "%s\n" "$response" | /usr/xpg4/bin/grep -Eq "$(locale yesexpr)" && return 0
		printf "%s\n" "$response" | /usr/xpg4/bin/grep -Eq "$(locale noexpr)" && return 1
	done
}

#########################################################
#
# issunfire15kSC() {
#
# Returns True (0) iff current machine is a SunFire 15k SC
# Otherwise returns False (1)
#
#########################################################
issunfire15kSC()
{
    PRTCONF_OUT=/tmp/prtconf$$.out
    SC=NO
    prtconf -p > $PRTCONF_OUT
    if grep \'gchip\' $PRTCONF_OUT > /dev/null 2>&1 && \
        grep \'echip\' $PRTCONF_OUT > /dev/null 2>&1 && \
         grep \'consbus\' $PRTCONF_OUT > /dev/null 2>&1
    then
        SC=YES
    fi
    rm -f $PRTCONF_OUT
    [ $SC = YES ]
}

#############################################################################
#
# require_root
#
# Require root privileges.
#
#############################################################################
require_root()
{
	if id | grep root >/dev/null
	then
		:
	else
		printf "$(gettext "You must be root to run %s.")\n" $PROG >&2
		exit 1
	fi
}

#############################################################################
#
# getrootpass
#
# Obtain root password from user.
#
# $1 - name of variable to be set to value of root password
#
#############################################################################
getrootpass()
{
	rtnarg=$1
	if tty -s; then
		trap "stty echo; return 1" HUP INT QUIT TERM
		stty -echo
		while [ -z "$ROOTPASS" ]; do
			printf "$(gettext "Enter root password: ")" > /dev/tty
			read rootpass < /dev/tty && ROOTPASS=$rootpass
			echo > /dev/tty
		done
		stty echo
	fi
	eval $rtnarg=$ROOTPASS
}

initWdrConfigFile()
{

	yes_or_no "$(gettext "Do you want to configure the config file now?")" || return 0
	echo
	gettext "Initiate WDR Configuration File\n"
	echo "-------------------------------"

	# Get default port from existing config file.
	# If it is not there, use the default SNMP Trap port 162.
	if [ -f $BASEDIR/$WDRCONFIGFILE ] ; then
		defport=$(awk -F: '
			BEGIN { port=162 }
			/^[ 	]*#/{ next } 
			/^SCTrapPort/ { port=$2; exit }
			END { print port }
		' $BASEDIR/$WDRCONFIGFILE 2>/dev/null)
	else
		defport=162
	fi

	# Prompt user for port.
	port=
	while [ -z "$port" ]; do
		def=
		[ -n "$defport" ] && def="[$defport]"
		echo "SCTrapPort$def: \c"
		read answer || return 0
		if [ -z "$answer" ]; then
			port=$defport
		else
			port=$answer
		fi
	done

	# Create dir if it is not already there.
	if [ ! -d "${BASEDIR}" ]; then
		mkdir -m 0755 -p ${BASEDIR} || return 0
	fi

	# Remove existing config file (if one is already there).
	rm -f $BASEDIR/$WDRCONFIGFILE

	# Create new config file.
	cat > $BASEDIR/$WDRCONFIGFILE <<_END_
# This file must not be modified manually.
# Please use /opt/SUNWWDR/bin/wdrconfig to modify this file.
SCTrapPort:$port
_END_
	printf "$(gettext "%s value set to %s in %s")""\n" \
		SCTrapPort $port $BASEDIR/$WDRCONFIGFILE

}

compileMOF()
{
	echo
	yes_or_no "$(gettext "Do you want to compile the MOF files now?")" || return 0
	(
	if cd $MOFDIR; then
		if getrootpass rootpass; then
			gettext "\nCompiling Core MOF File\n"
			echo "-------------------------------"
			if mofcomp -u root WDR_Core1.0.mof <<_END_PASS_ \
				| sed 's/Enter password://'
$rootpass
_END_PASS_
			then
				if issunfire15kSC ; then
					gettext "\nCompiling High-End Server MOF File\n"
					echo "-------------------------------"
					mofcomp -u root WDR_XC1.0.mof <<_END_PASS_ \
					| sed 's/Enter password://'
$rootpass
_END_PASS_
				else
					gettext "\nCompiling Midrange Server MOF File\n"
					echo "-------------------------------"
					mofcomp -u root WDR_SG1.0.mof <<_END_PASS_ \
					| sed 's/Enter password://'
$rootpass
_END_PASS_
				fi
			fi
		fi
	else
		gettext "WARNING: Could not compile MOF files because $MOFDIR directory not found.\n" >&2
	fi
	)
}


#
# Execution begins here
#

if [ $# -gt 0 ]; then
    printf "$(gettext "Usage: %s (no arguments)")\n" "$0" >&2
    exit 1
fi

require_root

umask go-w

# Only init the Config File on a Serengeti (Sun Fire x800),
# not a Sun Fire 15K SC)
issunfire15kSC || initWdrConfigFile

compileMOF

