#!/bin/sh  
#
#
#ident "@(#)x400_genconf 1.12 - 95/09/29 SMI"
#
# Copyright 1994 Sun Microsystems, Inc. All Rights Reserved
#
#	Rebuild the MHS configuration from a text file definition
#
# 	See Usage
# --------------------------------------------------------------

THIS1=`basename $0`

#
#  PATH: CMD used
#
MYDIR=`dirname $0`
EXECSPATH="`(cd $MYDIR ; pwd)`"
PATH="$PATH:/sbin:/bin:/usr/bin:/etc/opt/SUNWconn/bin:$EXECSPATH" 

OSIMCF=osimcf		# basename 
#
#  Constants referred to 
#
OSIROOT_FILE=/etc/SUNWconn/OSIROOT
OSIMTA=osimta		# process's name for ps
LOG=/tmp/${THIS1}.log.$$

	# regexp to parse source file 
XOM_PREFIX='^; xom.ini -'
XMH_PREFIX='^; xmh.ini -' 
MTAINIT_PREFIX='^; osimta.init -'
GWYINIT_PREFIX='^; osismtpx400.init -'
MQAINIT_PREFIX='^; osix400mqa.init -'

OSIMTA_INIT=osimta.init		# basename : file in OSIROOT/conf
GWY_INIT=osismtpx400.init 	# basename : file in OSIROOT/conf
MQA_INIT=osix400mqa.init 	# basename : file in OSIROOT/conf
XOM_INI=xom.ini			# basename : file in OSIROOT/conf
XMH_INI=xmh.ini			# basename : file in OSIROOT/conf

			# Mode, Owner and group of the created files
TIGHTMODE=0600		# for "reserved info"
DEFMOD=0644		# for "everyone can look at" stuff 
DEFOWN=root
DEFGRP=sys
	
#
#   ERROR function
#   $1=message; $2=exit code
INVFILE="File is not an MHS config file\n\t"

err () {
	echo "$THIS1 - ERROR: $1" 1>&2
	if [ $2 -ne 0 ]
	then
		rm -f $LOG 
		exit $2
	fi
}

# Error codes. 
e_ok=0
e_usage=1
e_noinfile=2		# Can't find input file
e_invfile=3		# Source file is not a valid one 
e_rm=4			# Can't remove/overwrite file 
e_osimcf=5		# Can't find osimcf 
e_doing=6		# Error while generating the config
e_mtarunning=7		# The MTA is running. Can't regenerate current conf
e_signal=8		# Interrupted by a signal
e_canfindOSIROOTFILE=9	# Can't find file that defines OSIROOT 
e_noOSIROOTdir=10	# Can't find OSIROOT directory
e_cantrestore=11	# Can't restore a file of current config 
e_x400toolrunning=12	# The MTA is running. Can't regenerate current conf

#
#  TRAP
#
trap 'rm -f $LOG  $XOMINITMP  ;echo "$THIS1 interrupted" 1>&2; exit $e_signal' INT  QUIT
#
#	Yes/No answer
#
#
yesno_answer()
{
	STRING=$1
	answer=
	while [ "$answer" != "y" -a "$answer" != "Y" -a "$answer" != "n" -a "$answer" != "N" ]
	do
             	echo "$STRING [y,n] " | tr -d '\012' 
		read answer 
	done
}

#
#   Usage
#

USAGE=`cat <<%
Usage: $THIS1 <fileName>
	fileName is the path of a MHS configuration log file.
	$THIS1 regenerates the whole MHS configuration from the log file.
%
`
if [ $# -ne 1 ]
then
	echo "$USAGE" 2>&1 
	exit  $e_usage
fi
SRCFILE=$1

#
#	Check whether there is such a file 
#
if [ ! -f $SRCFILE ]
then
	err "Can't find file: $SRCFILE " $e_noinfile

fi
#
#	get OSIROOT, if not already defined in the environment
#
if [ -z "$OSIROOT" ]
then
	if [ -r $OSIROOT_FILE ]
	then
		OSIROOT=`cat $OSIROOT_FILE` 2>&1 > /dev/null 
	else
		err "Can't find file: $OSIROOT_FILE" $e_canfindOSIROOTFILE
	fi
fi
#	Check: OSIROOT must be a directroy
if [ ! -d "$OSIROOT" ]
then
	err "No such directory: <$OSIROOT> " $e_noOSIROOTdir
fi
#
#	Check whether I can access $OSIMCF 
#
if type $OSIMCF 2>&1 | grep "^$OSIMCF not found" > /dev/null
then
   	err "Can't find : <$OSIMCF>" $e_osimcf
fi

#	--------------------------------------------------
#
#	Regenerating a configuration means:
#	- generating the xom.ini file associated with the conf
#	- generating the xmh.ini file associated with the conf
#	- generating the Local MTA init file (osimta.init) file 
#	- generating the SMTP gateway init file (osismtpx400init) file 
#	and then
#	- generating the data files used by the MTA process. (MHS data base)
#
#	--------------------------------------------------
#
#	Get from text file the list of files to be created
#
echo	"Analysing source file: $SRCFILE"
echo 	"Please wait ...\c"
M_INITDB='^[ 	]*MTS INI'
M_ORNAME='^[ 	]*MTS CREATF[^ ]* OR[^ ]'
M_X121='^[ 	]*MTS CREATF[^ ]* X121'
M_ALT='^[ 	]*MTS CREATF[^ ]* ALTER'
M_J1='^[ 	]*MTS RECORDF[^ ]* J1'
M_J2='^[ 	]*MTS RECORDF[^ ]* J2'
M_B1='^[ 	]*MTS RECORDF[^ ]* BILLING1'
M_B2='^[ 	]*MTS RECORDF[^ ]* BILLING2'
M_BTMP='^[ 	]*MTS B[^*]'
M_MHSCF='^[ 	]*MTS E[^ ]* F[^ ]*'

echo ".\c"
F_MTA=`grep -i "$M_INITDB"    $SRCFILE  | awk '{ print $3 }'`
echo ".\c"
F_LUAG=`grep -i "$M_INITDB"   $SRCFILE  | awk '{ print $4 }'`
echo ".\c"
F_MSG=`grep -i "$M_INITDB"    $SRCFILE  | awk '{ print $5 }'`
echo ".\c"
F_ORNAME=`grep -i "$M_ORNAME" $SRCFILE  | awk '{ print $4 }'`
echo ".\c"
F_X121=`grep -i "$M_X121"     $SRCFILE  | awk '{ print $4 }'`
echo ".\c"
F_ALT=`grep -i "$M_ALT"       $SRCFILE  | awk '{ print $4 }'`
echo ".\c"
F_J1=`grep -i  "$M_J1"        $SRCFILE  | awk '{ print $4 }'`
echo ".\c"
F_J2=`grep -i  "$M_J2"        $SRCFILE  | awk '{ print $4 }'`
echo ".\c"
F_B1=`grep -i  "$M_B1"        $SRCFILE  | awk '{ print $4 }'`
echo ".\c"
F_B2=`grep -i  "$M_B2"        $SRCFILE  | awk '{ print $4 }'`
echo ".\c"
F_BTMP=`grep -i "$M_BTMP"     $SRCFILE  | awk '{ print $3 }'`
echo ".\c"
F_MHS=`grep -i  "$M_MHSCF"    $SRCFILE  | awk '{ print $4 }'`
echo

# Other file which are part of the configuration
F_MHSTEXT=${F_MHS}.text 
F_XOM=${F_MHS}.xom
F_XMH=${F_MHS}.xmh
F_MTAINIT=${F_MHS}.init
F_GWYINIT=${F_MHS}.gwyinit
F_MQAINIT=${F_MHS}.mqa

ALLFILES="$F_MHS $F_MTA $F_LUAG $F_MSG $F_ORNAME $F_X121 $F_ALT $F_J1 $F_J2 $F_B1 $F_B2 $F_BTMP $F_XOM $F_XMH $F_MTAINIT $F_GWYINIT $F_MQAINIT "

#
#	Check data
#
if [ -z "$F_MHS" ]
then
	err "${INVFILE}Missing Configuration file specification " $e_invfile
fi
if [ -z "$F_MTA" ]
then
	err "${INVFILE}Missing MTA file specification" $e_invfile
fi
if [ -z "$F_LUAG"  ]
then
	err "${INVFILE}Missing LUAG file specification " $e_invfile
fi
if [ -z "$F_MSG"  ]
then
	err "${INVFILE}Missing MESSAGE file specification " $e_invfile
fi
if [ -z "$F_ORNAME" ]
then
	err "${INVFILE}Missing ORNAME file specification " $e_invfile
fi
if [ -z "$F_X121" ]
then
	err "${INVFILE}Missing X121 file specification " $e_invfile
fi
if [ -z "$F_ALT" ]
then
	err "${INVFILE}Missing ALTERNATE file specification " $e_invfile
fi
if [ -z "$F_J1" ]
then
	err "${INVFILE}Missing JOURNAL file specification" $e_invfile
fi
if [ -z "$F_J2" ]
then
	err "${INVFILE}Missing JOURNAL file specification" $e_invfile
fi
if [ -z "$F_B1" ]
then
	err "${INVFILE}Missing BILLING file specification " $e_invfile
fi
if [ -z "$F_B2" ]
then
	err "${INVFILE}Missing BILLING file specification " $e_invfile
fi
if [ -z "$F_BTMP" ]
then
	err "${INVFILE}Missing BILLING file specification " $e_invfile
fi

#	-------------------------------------------------------
#	If I've regenerating the Current configuration,
#	I'd better make sure all the files are fine
#
#	That's means taking care of the following files:
#	  OSIROOT/conf/xom.ini
#	  OSIROOT/conf/xmh.ini
#	  OSIROOT/conf/osimta.init
#	  OSIROOT/conf/osismtpx400.init
#	------------------------------------------------------------
	
	# FP= Full Path 

FP_XOMINI=${OSIROOT}/conf/$XOM_INI
FP_XMHINI=${OSIROOT}/conf/$XMH_INI 
FP_MTAINIT=${OSIROOT}/conf/$OSIMTA_INIT
FP_GWYINIT=${OSIROOT}/conf/$GWY_INIT
FP_MQAINIT=${OSIROOT}/conf/$MQA_INIT

	# if you don't have one of the following files 
	# you system is in really bad shape.
	# You could/should use the configuration as the
	# current one 


NEEDEDFILES="$FP_XOMINI $FP_XMHINI $FP_MTAINIT $FP_GWYINIT $FP_MQAINIT"

MISSINGFILES=0
for f in $NEEDEDFILES
do
	if [ ! -f $f ]
	then
		MISSINGFILES=1
	fi
done

	#  If the configuration generated right now
	#  is the current one, make sure all the files 
	#  are fine. 
	#  The x400tool won't allow you to restore the 
	#  "current" configuration
ISCURRENT=0
if [ $MISSINGFILES -eq 0 ]
then
	# replace '/' with '\/' : sed doesn't like paths 
	FILE=`echo $FP_MTAINIT | sed 's/\//\\\\\//g'`

	LINK=`file -h $FP_MTAINIT | sed "s/^$FILE:[ 	]*symbolic link to //g" `

	if [ "$LINK" = "$F_MTAINIT" ]
	then
		ISCURRENT=1
	fi
fi

	#
	# Ask the user whether s/he want to make the generated 
	# configuration the current one used by the MTA
	#
	
if [ $ISCURRENT -ne  1 -a  $MISSINGFILES -ne 1 ]
then
	yesno_answer "Do you want to make the generated configuration the current one "
	if [ "$answer" = "y" -o "$answer" = "Y" ]
	then
		ISCURRENT=1 
	fi
else
	echo "You are regenerating the current configuration"
fi


#
#	If the MTA is running, You can't touch the current config
#
if [  $MISSINGFILES -eq 1 -o $ISCURRENT -eq 1 ] 
then
	ERR=0
	if [ `ps -fu root | grep $OSIMTA | grep -v grep | wc -l ` -gt 0 ]
	then
		err "The MHS MTA process is running ($OSIMTA)\nYou need to stop it before regenerating the configuration\nTo stop it, use : osistop $OSIMTA\n" 0

	ERR=$e_mtarunning
	fi

	if [ `ps -fu root | grep x400tool | grep -v grep | wc -l ` -gt 0 ]
	then
		err "The x400tool is running \nYou need to stop it before regenerating the configuration\n" 0
		ERR=$e_x400toolrunning
	fi

	if [ $ERR -ne 0 ] ; then
		exit $ERR
	fi

fi

#
#	Want to overwrite
#
EXISTING=0
for f in $ALLFILES
do
	if [ -f $f ]
	then
		EXISTING=1
	fi
done
if [ $EXISTING -eq 1 ]
then
	echo "The generation of the configuration will overwrite an existing configuration:"
	echo "$F_MHS"
	yesno_answer "Do you want to proceed"
	if [ "$answer" = "n" -o "$answer" = "N" ]
	then
		exit $e_ok 
	fi
fi
#
#	Remove all existing file 
#	
echo	"Generating the configuration ...\c"
for f in $ALLFILES
do
	rm -f $f 
	echo ".\c"
	if [ -f $f ]
	then
		echo ""
		err "Can't overwrite file: $f "	$e_rm
	fi
done
echo ""
#
#	Generating F_XOM 
#
echo ".\c"
grep "$XOM_PREFIX" $SRCFILE  | sed "s/$XOM_PREFIX//g" > $F_XOM 
#
#	Generating F_XMH 
#
echo ".\c"
grep "$XMH_PREFIX" $SRCFILE  | sed "s/$XMH_PREFIX//g" > $F_XMH

#
#	Generating F_MTAINIT
#
echo ".\c"
grep "$MTAINIT_PREFIX" $SRCFILE  | sed "s/$MTAINIT_PREFIX//g" > $F_MTAINIT

#
#	Generating F_GWYINIT
#
echo ".\c"
grep "$GWYINIT_PREFIX" $SRCFILE  | sed "s/$GWYINIT_PREFIX//g" > $F_GWYINIT
#
#	Generating F_MQAINIT
#
echo ".\c"
grep "$MQAINIT_PREFIX" $SRCFILE  | sed "s/$MQAINIT_PREFIX//g" > $F_MQAINIT
if [ ! -s $F_MQAINIT ]
then
  #
	# file doesn't exist in the text configuration, generate a default one
  cat >$F_MQAINIT <<%
#ident "@(#)osix400mqa.init  1.1 25 Jul 1994 SMI"
#
#
# Copyright 1994 Sun Microsystems, Inc. All Rights Reserved
#
#
# osix400mqa.init
#
# Init file for the message queue interface
#
#
# in the rest of the file Input directory is from the application to the MTA
# and output directory is from the MTA to the application
#
# verbose mode, if you want to have some evens traces on standard output 
# uncomment the following line if you want to have the event traces
# TRACEON
#
# polling time for looking into the input directory
# uncomment the following line if you want to change the default value
# WAIT 30
#
# set xom debug
# uncomment the following line when needed
# DEBUG XOM 2
#
# set asn1 low level debug
# uncomment the following line when needed
# DEBUG ASN1 2
#
# set lifetime values for Normal, low priority, Urgent messages,
# report and probe (in seconds)
# uncomment the following line when needed
# LIFETIME 86400 86400 3600 3600 86400
#
#
# Declare users to bind on
# those users must be declared as P1 users in the MTA
#
# USER P1 <username> <input_dir> <outputdir> <GDI_Country> <GDI_ADMD> <GDI_PRMD>
#
# Could also be P3 users
#
# USER P3 <username> <input_dir> <outputdir>
#
# Sample:
#
# USER P1 GATE2 OUT IN FR ATLAS SUNTST
#
# you can specify "#" as PRMD if you don't have a PRMD name
#
# Sample
#
# USER P1 GATE2 OUT IN FR ATLAS #
Start
%
fi
#
#	Generate the data files: MHS data base 
#
echo ".....\c"
$OSIMCF < $SRCFILE  >$LOG 2>&1 
if grep -i ERROR $LOG 2>&1 >/dev/null 
then
	echo 	"" 
	echo	"Error while generating the configuration" 1>&2
	cat	$LOG | sed  "s/^[X400AM] ===>/	/g"  
	rm -f $LOG
	exit 	$e_doing
fi
echo "" 
#
#	Put the text file in place, if needed.
#	You can't copy a file on itself: if there
#	is no difference, just don't copy
#	
#
if [ `diff "$F_MHSTEXT" "$SRCFILE"| wc -l` -ne 0 ]
then
	cp  $SRCFILE $F_MHSTEXT
fi


	#
	#	Make sure everything is fine with 
	#	the current configuration
	#
if [  $MISSINGFILES -eq 1 -o $ISCURRENT -eq 1 ] 
then
	echo  "The generated configuration is the current one "
	echo  "Checking all the configuration files ... "
	
	#     
	#	.... the xom.ini file 
	#	
	echo "\t$FP_XOMINI"

	# This is exactly what we do at installation time 
	# (postintall) 

	XOMINI=$FP_XOMINI
	MYXOMINI=$F_XOM

	XOMINIBAK=${XOMINI}.bak		# If everything goes fine, It's removed
	XOMINITMP=/tmp/xomini.$$ 

	FULL_STARTMARK='# ----------------- MHS BEGIN ---Do NOT EDIT THIS SECTION BY HAND ---'
	FULL_ENDMARK='# ----------------- MHS END -------------------'


	STARTMARK='MHS *BEGIN'
	ENDMARK='MHS *END'
	MYSERVICE=27 		# MHS SERVICE: Id=27 - field number 6 


	COMMENTOUT_WORKDIR='s/^ *\([Ww][Oo][Rr][Kk][Dd][Ii][Rr]\)/###MHS_COMMENTED_OUT###WORKDIR /g'

	if [ -f $XOMINI ]; then

		# Backup the file. Just for security
		cp $XOMINI $XOMINIBAK
	
		# Comment out existing WORKDIR commands
		sed "$COMMENTOUT_WORKDIR" $XOMINIBAK >   $XOMINI

		lines=`grep -v "^#" $XOMINI | sed 's/  */ /g' | cut -f6 -d' ' -s - | grep -c $MYSERVICE`
		if [ $lines -ne 0 ]; then
	
			oldmode=`grep -c "$STARTMARK" $XOMINI`
			if [ $oldmode -ne 0 ]; then
				# get anything but what's in between the marks 
				beginline=`grep -n "$STARTMARK"  $XOMINI | sed '1s/:#.*//'`
				beginline=`expr $beginline - 1`
				endline=`grep -n "$ENDMARK" $XOMINI | sed '1s/:#.*//'`
				endline=`expr $endline + 1`
				head -$beginline $XOMINI > $XOMINITMP
				tail +$endline $XOMINI >> $XOMINITMP
			else
				# Get everything but MHS stuff 
				ENTRY="^\ *[^ ][^ ]*\ *[^ ][^ ]*\ *[^ ][^ ]*\ *[^ ][^ ]*\ *[^ ][^ ]*\ *$MYSERVICE"
				grep -v "$ENTRY" $XOMINI  > $XOMINITMP
			fi
			# Add MHS definition between marks 
			echo "$FULL_STARTMARK" 	>> $XOMINITMP
			cat $MYXOMINI		>> $XOMINITMP 
			echo "$FULL_ENDMARK" 	>> $XOMINITMP	
			mv $XOMINITMP $XOMINI
		else
			# append MHS part to existing xom.ini file 
			echo "$FULL_STARTMARK" 	>> $XOMINI
			cat $MYXOMINI		>> $XOMINI 
			echo "$FULL_ENDMARK" 	>> $XOMINI
		fi
	
	else
		# Just copy MYOMINI into XOMINI: wrap the lines in between Marks
		echo "$FULL_STARTMARK" 	>  $XOMINI
		cat $MYXOMINI		>> $XOMINI 
		echo "$FULL_ENDMARK" 	>> $XOMINI
	fi
	rm -f $XOMINIBAK
	rm -f $XOMINITMP
	
	#     
	#	.... the xmh.ini file 
	#	
	echo "\t$FP_XMHINI"

	rm -f $FP_XMHINI
	echo "$FULL_STARTMARK" 	>  $FP_XMHINI
	cat $F_XMH		>> $FP_XMHINI
	echo "$FULL_ENDMARK" 	>> $FP_XMHINI

	#     
	#	.... the osimta.ini file 
	#	
	echo "\t$FP_MTAINIT"
	rm -f $FP_MTAINIT 
	if grep "define suffix 71" $F_MTAINIT 2>&1 >/dev/null
	then
	:
	else
		sed "/END OF AUTOMATICALLY GENERATED PART -VARY ENTITIES/i\\
define suffix 71 H02/P/3\$SUF50\$SUF40\$SUF30
/END OF AUTOMATICALLY GENERATED PART -VARY ENTITIES/i\\
define suffix 171 H02/P/3\$SUF150\$SUF140\$SUF130
/END OF AUTOMATICALLY GENERATED PART -VARY ENTITIES/i\\
define suffix 271 H02/P/3\$SUF250\$SUF200\$SUF280
/END OF AUTOMATICALLY GENERATED PART -VARY ENTITIES/i\\
macro define version V8.0.2
/END OF AUTOMATICALLY GENERATED PART -VARY ENTITIES/i\\
xrose Working_Dir /var/SUNWconn/OSIROOT/spool" <$F_MTAINIT >/tmp/x400_genconf.$$
	mv /tmp/x400_genconf.$$ $F_MTAINIT
	fi
	ln -s $F_MTAINIT  $FP_MTAINIT   \
		|| err "Can't restore file: $F_MTAINIT" $e_cantrestore 

	#     
	#	.... the gwy's ini file 
	#	
	echo "\t$FP_GWYINIT"
	rm -f $FP_GWYINIT 
	ln -s $F_GWYINIT  $FP_GWYINIT  \
		 || err "Can't restore file: $F_GWYINIT" $e_cantrestore 



	#     
	#	.... the mqa's ini file 
	#	
	echo "\t$FP_MQAINIT"
	rm -f $FP_MQAINIT 
	ln -s $F_MQAINIT  $FP_MQAINIT  \
		 || err "Can't restore file: $F_MQAINIT" $e_cantrestore 


	#
	#	Remove pending files
	#
        # JCM cd in the spool directory to avoid argv overflow
        echo "..... Removing temporary files .....\c"

        ( cd $OSIROOT/spool
	rm -f T*
	rm -f p*
	rm -f f*
	rm -f q*
	rm -f XRO*
        )
#	rm -f $OSIROOT/spool/T*  $OSIROOT/spool/p*  $OSIROOT/spool/f* $OSIROOT/spool/q* $OSIROOT/spool/XRO*

	#
	# See if sendmail.cf needs to be updated
	#
	SNDMAIL_PSEUDO=`/bin/egrep "^CX" /etc/mail/sendmail.cf | sed s/CX//`
	GWY_PSEUDO=`grep pseudo $FP_GWYINIT | awk '{print $3}'`
	if [ -z "$GWY_PSEUDO" ]
	then
		GWY_PSEUDO=x400-gate
	fi
	if [ -n "$SNDMAIL_PSEUDO" ]
	then
		if [ $SNDMAIL_PSEUDO != $GWY_PSEUDO ]
		then
			#
			# the two pseudo host don't match we must update sendmail.cf
			# and restart sendmail
			#
			echo "Updating sendmail.cf"
			if /bin/sed "s/^CX.*/CX$GWY_PSEUDO/" </etc/mail/sendmail.cf >/etc/mail/sendmail.cf.new
			then
				/bin/mv /etc/mail/sendmail.cf.new /etc/mail/sendmail.cf
				/bin/chown bin /etc/mail/sendmail.cf
				/bin/chmod ugo=r /etc/mail/sendmail.cf
				echo "Restarting /usr/lib/sendmail .\c"
				/etc/rc2.d/S88sendmail stop
				echo ".\c"
				/etc/rc2.d/S88sendmail start
				echo " Done"
			else
				echo "Failed to Update sendmail.cf"
				/bin/rm /etc/mail/sendmail.cf.new
			fi
		fi
	fi
	# ---- END dealing with current configuration 
fi 


#
#	Set properly Mode, Owner and group 
#	- set as default 
#	- change the mode of "sensitive" ones .

for f in $ALLFILES  $F_MHSTEXT
do
	chmod $DEFMOD  $f
	chown $DEFOWN  $f 
	chgrp $DEFGRP  $f
done

chmod $TIGHTMODE  $F_XMH 
chmod $TIGHTMODE  $F_MHSTEXT


	echo 	""
	echo 	"MHS configuration successfully generated"
	echo	"Data Base file is: $F_MHS"

# Happy end 
rm -f $LOG  $XOMINITMP 
exit $e_ok
