#!/bin/ksh
#
#
#ident "@(#)x400_genmaps 1.6 - 96/02/12 SMI"
#
# Copyright 12 Feb 1996 Sun Microsystems, Inc. All Rights Reserved
#
#   	Regenerate mapping tables for MHS gateway.
#
#	This script calls the executable '822orname'. The 
#	executable has to be in PATH or in the same directory
#	where this script is placed.
#
#	If no directory is supplied as -d option: 
#  		If the gateway if running,
#       		send the "RESET" command (prepend the prefix)
#  		Else: remove the cache files under OSIROOT/mhs/con
#			
# 
# --------------------------------------------------------------

THIS1=`basename $0`

# SMTP gateway Prefix-Mailbox-Process
GWY=osismtpx400
# GWY's Cache files: basename 
CACHEF1=d2gcache
CACHEF2=d2xcache
CACHEF3=x2dcache
# Seconds to wait for osioper to answer
WAITOPER=5

#
#  CMD used
#
ORNAME822=822orname
OSIOPER=x400oper

# PATH 

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



# TMP
TOTO=/tmp/toto.$$
SED1=/tmp/${THIS1}.sed1.$$      
TMPF=/tmp/${THIS1}.$$
TMPLOW1=/tmp/${THIS1}.low1.$$
TMPLOW2=/tmp/${THIS1}.low2.$$
TMPS=/tmp/${THIS1}.osioper.$$

#
#   ERROR function
#   $1=message; $2=exit code
err () {
	echo "## ERROR: $1" 1>&2
	if [ $2 -ne 0 ]
	then
		rm -f $TMPF $SED1 $TMPLOW1 $TMPLOW2
		exit $2
	fi
}

#
#  Basename of input files
#
BSNM_MAP1=rfc1148-mapping1
BSNM_MAP2=rfc1148-mapping2
BSNM_GATE=rfc1148-gate

#
#   Usage
#

USAGE=`cat <<%
Usage: $THIS1 -d <dir>
	Generates the mapping files used by MHS gwy for
	X.400 <-> SMTP address conversion.
	If no directory is supplied, by default the
	MHS's conf directory under OSIROOT is taken.

	The input files have to be named as follows:
		<$BSNM_MAP1>
		<$BSNM_MAP2>
		<$BSNM_GATE>
	The produced files are named after the input
	file, adding the suffix '.bin'

%
`
MAINDIR=""
while getopts d:  c
    do
	case $c in
	d) MAINDIR=$OPTARG ;;
	\?)  echo "$USAGE" 1>&2 ; exit 1 ;;
        esac
    done
shift `expr $OPTIND - 1`

UPDT_OSIROOT_INFO=0
if [ "x$MAINDIR" = "x" ]
then
	UPDT_OSIROOT_INFO=1
	if [ "x$OSIROOT" = "x" ]
	then
		#
		# Get OSIROOT
		#
		OSIROOT_FILE=/etc/OSIROOT
		if [ ! -r $OSIROOT_FILE ]
		then
		   err  "directory not specified and OSIROOT not defined "  1
		fi
		OSIROOT=`cat /etc/OSIROOT`
	fi
		
	# DIR
	MAINDIR=$OSIROOT/mhs/conf
fi

echo "Regenerating Mapping Tables in dir: <$MAINDIR> ..."

if [ ! -d $MAINDIR ]
then
	err "No such directory: <$MAINDIR> " 2 
fi


# Text files : INPUT 

T_MAP1=$MAINDIR/$BSNM_MAP1
T_MAP2=$MAINDIR/$BSNM_MAP2
T_GATE=$MAINDIR/$BSNM_GATE

# Data files:  OUTPUT

B_MAP1=${T_MAP1}.bin
B_MAP2=${T_MAP2}.bin
B_GATE=${T_GATE}.bin

# Check Existence of input file 

MISSING=0
for f in $T_MAP1 $T_MAP2 $T_GATE
do
	if [ ! -r $f ]
	then
		MISSING=1
		err "No such file <$f>" 0 
	fi
done

if [ $MISSING -ne 0 ]
then
	err "Can't find input file(s)" 2
fi


# Check existence of needed CMD


if type $ORNAME822 2>&1 | grep "not found" > /dev/null
then
      err "Can't find executable <$ORNAME822> " 3
fi

#
#  SED1 
#
cat > $SED1 <<EOSED
/^#/d
s/\\$/!/
s/\(.*\)!\(.*\)/\2!\1/
EOSED

#
#	to_lower function
# 	
#	$1= file. 
#	transform in lowercase each line till '#' sign. 
#	Print on stdout 
#

to_lower () {
	cut -f 1  -d '#' $1 | tr "[A-Z]" "[a-z]" > $TMPLOW1
	cut -f 2- -d '#' $1 > $TMPLOW2
	paste -d '#' $TMPLOW1 $TMPLOW2 
	rm -f $TMPLOW1 $TMPLOW2 
}

#
#  Create data files
#
#  MAP1 
#
echo "Generating <$B_MAP1> ..."
to_lower  $T_MAP1 | sed -f  $SED1 | sort | \
  sed 's/\(.*\)!\(.*\)/\2$\1/' > $TMPF || err "$B_MAP1 generation failed" 10
$ORNAME822 $TMPF                      || err "$B_MAP1 generation failed" 10
mv ${TMPF}.bin  $B_MAP1               || err "$B_MAP1 generation failed" 10
rm -f $TMPF

#
# MAP2
#
echo "Generating <$B_MAP2> ..."
to_lower $T_MAP2 | sort > $TMPF  || err "$B_MAP2 generation failed" 11
$ORNAME822 $TMPF 		   || err "$B_MAP2 generation failed" 11
mv ${TMPF}.bin $B_MAP2             || err "$B_MAP2 generation failed" 11
rm -f $TMPF

#
# GATE map
#
echo "Generating <$B_GATE> ..."
to_lower $T_GATE | sort > $TMPF  || err "$B_GATE generation failed" 12
$ORNAME822 $TMPF                   || err "$B_GATE generation failed" 12
mv ${TMPF}.bin $B_GATE	           || err "$B_GATE generation failed" 12
rm -f $TMPF

# RM TMP files
rm -f $TMPF $SED1 $TMPLOW1 $TMPLOW2

# 
#  If the gateway if running,
#       send the "RESET" command (preprend the prefix)
#  Else: remove the cache files under OSIROOT/mhs/con
#		d2gcache  d2xcache  x2dcache
# 

if [ $UPDT_OSIROOT_INFO  -ne 0 ]
then
	# I need the operator 
	if type $OSIOPER 2>&1 | grep "not found" > /dev/null
	then
      		err "Can't find executable <$OSIOPER> " 3
	fi

cat >  $TMPS <<%
\$status 
\$wait $WAITOPER
\$Exit
%

	# Check if the Gwy is running 
        FILETMP=/tmp/toto$$
        export FILETMP
        $OSIOPER  $TMPS FOO > $FILETMP 
	STATUS=`cat $FILETMP | grep "Pref=$GWY Prog=$GWY" `
	rm -f $FILETMP
	if [ "x$STATUS" = "x" ]    # GWY isn't runnning
	then   
		rm -f ${MAINDIR}/$CACHEF1
		rm -f ${MAINDIR}/$CACHEF2
		rm -f ${MAINDIR}/$CACHEF2
	else			   # GWY IS running 
	
cat >  $TMPS <<%
\$wait $WAITOPER 
$GWY RESET
\$e
%

$OSIOPER $TMPS FOO > /dev/null

	fi
fi

rm -f $TMPS   

# Happy End
exit 0



