#!/bin/sh

SVRNAMMSG="Enter the name of the node on which the InfoBroker will run"
SVRNAMREQ="This information is required to enable directory services"
SVRNAMBLANK="(Return=NONE)"
LDAPHELP1="InfoBroker is used to handle all requests for directory service information."
LDAPHELP2="The node in your network where the InfoBroker is running must be specified"
LDAPHELP3="in order to enable any directory service queries."
NOINFOBROKER="A machine where InfoBroker can be found has not been defined"
TOADD1="To add this value in the future, edit"
TOADD2="Add the following line:"
TOADD3="                   DAXServer=INFOBROKER_NODE"
HASBEENUPDATED="has been updated"
TOCHANGE1="To change this value in the future, edit"
TOCHANGE2="Modify the following line:"
TOCHANGE3="                DAXserver="
NORCFILE="No MAILworksrc file could be found, cannot continue"

MAILWORKSRCPATH="/opt/DMW /usr/spool/MAILworks /usr/lib/MAILworks /usr/kits/MAILworks /usr/lpp/MAILworks /usr/MAILworks /MAILworks /usr/local/MAILworks /usr/local/lib/MAILworks"

KEYSFILETEXT="Replace with the matching Key of the MSR on the node where the MSR is running"



if [ `whoami` != "root" ]
then
	echo
	echo "Please log on as root to configure the MAILworks client"
	exit 1
fi

for PRCD in $MAILWORKSRCPATH
do
    if [ -f $PRCD/MAILworksrc ]
    then
	MAILWORKSRC="$PRCD/MAILworksrc"
	break
    fi
done


if [ "$MAILWORKSRC" = "" ]
then
	echo "$NORCFILE"
	exit 1
fi

. $MAILWORKSRC

if [ "`echo -n`" = "-n" ]
then
	n=''
	c='\c'
else
	n='-n'
	c=''  
fi

#
# Set up the Keys file for the MSR
#

keysFile=$sharedDir/$keysFile

GETHOST=0
if [ -f $keysFile ]
then
	if  [ "`wc -l $keysFile | tr -s ' ' ' ' | cut -d' ' -f2`" != "1" ]
	then
		echo
		echo "Warning: The keys file, $keysFile, contains more than one entry"
		echo "         This file should contain a Key which matches the key of the"
		echo "         Message Store Registry (MSR) on the node where it is in use"
		echo "         Edit $keysFile to define this properly."
	else
		read MSRHOST < $keysFile
		if [ "$MSRHOST" = "$KEYSFILETEXT" ]	
		then
			GETHOST=1
		else
			echo
                        echo "In order to access the Message Store Registry (MSR) on the proper machine in"
	                echo "a network, a key to that MSR must be specified. This key must match the key on"
	                echo "the machine where the MSR is running. The default value for this key"
	                echo "is the host name of the machine where the MSR runs"
			echo
			echo "Your keys file, $keysFile, has the following entry:"
			echo
			cat $keysFile
			echo
			echo "This can be modified by editting $keysFile if this entry is incorrect"
		fi
	fi
else
	GETHOST=1
fi

if [ $GETHOST -eq 1 ]
then
	echo "In order to access the Message Store Registry (MSR) on the proper machine in"
	echo "a network, a key to that MSR must be specified. This key must match the key on"
	echo "the machine where the MSR is running. The default value for this key"
	echo "is the host name of the machine where the MSR runs. If you do not know this"
	echo "at this time it can be left blank and set later by editting $keysFile"
	echo
	echo $n "What is the host name of the machine where the MSR will be run ( ex. mail4u.xxx.dec.com) ? $c" 
	read MSRHOST
	[ "$MSRHOST" = "" ] && MSRHOST=$KEYSFILETEXT
	echo $MSRHOST > $keysFile
fi

#
# Get the name of the node which will run the LDAP server.
#

COUNT=1

while :
do
	echo
	if [ $COUNT -eq 1 ]
	then
		echo $n "$SVRNAMMSG $c: "
	else
		echo $n "$SVRNAMMSG $SVRNAMBLANK $c: "
	fi

	read HOST
	case $HOST in

	"")
		if [ $COUNT -eq 1 ]
		then
			echo $SVRNAMREQ
			echo $SVRNAMESPECIFY
			COUNT=2
		else
			break
		fi
		;;

	"?")
		echo
		echo $LDAPHELP1
		echo $LDAPHELP2
		echo $LDAPHELP3
		;;
	*)
		break
		;;
	esac
done

line=`grep -n "DAXserver=" $MAILWORKSRC`
if [ $? -eq 0 ]
then
        line=`echo $line | cut -d: -f1`
        sed "${line}d" $MAILWORKSRC > ${MAILWORKSRC}.tmp
        mv ${MAILWORKSRC}.tmp $MAILWORKSRC
fi

if [ "$HOST" != "" ]
then
	echo "DAXserver=$HOST" >> $MAILWORKSRC

	echo
	echo "$MAILWORKSRC $HASBEENUPDATED"
	echo

	echo "$TOCHANGE1 ${MAILWORKSRC}."
	echo "$TOCHANGE2"
	echo "$TOCHANGE3$HOST"
else
	echo
	echo "$NOINFOBROKER"
	echo
	echo "$TOADD1 ${MAILWORKSRC}."
	echo "$TOADD2"
	echo "$TOADD3"
fi

