#ident	"@(#)lp:lib/oam/gen-defs	1.5.3.1"
#ident  "$Header: gen-defs 1.4 91/06/28 $"

##########
#
# Generate ../../include/oam_def.h
#
# Generate lp.messages the file with the English text of the messages
# to be used for translation into any language.
#
# This script performs the following functions:
# 
#    The first sed statement copies the ident string and the copyright
#    notice from msg.source to lp.messages.  abs s21
#
#    The first group of grep statements removes all blank lines and
#    comment lines from the msg.source file and puts the result
#    into a temporary tmp.msg.source file.
#
#    The second sed statement selects all text lines from the 
#    tmp.msg.source file and puts them into the lp.messages file.
#    A new line character is placed at the end of each text line.
#    The double quotes are removed from the begining and the end
#    of the text lines. This file is used a source for translation
#    into a foreign language.
#
#    The first nl group numbers all E_*_* symbols starting from
#    1 and incrementing by 2.  This numbers are the sequence numbers
#    used to access the message from the uxlp catalog that contains
#    translated messages. The sequence numbers are placed after the
#    E_*_* symbol and a comma is placed after each number to separate
#    it from the array number to be created later. All new line
#    characters are replaced with three % characters. This is done
#    for the echo statement belov not to brake the line when it
#    finds the new line character. All back slash characters
#    are replaced with three back slash characters. The read statement
#    belov will remove one of them and convert two of them to one.
#    The three % characters are later replaced by a new line character.
#    The results are put into tmp1.msg.source file and the tmp.msg.source
#    file is removed.
#
#    The next loop groops all the messages in appropriate groops based
#    on the second part of the E_*_* symbols. An #endif is placed
#    at the end of each group. The underscores left after the loop
#    are removed and the three % are replaced with a new line character.
#    A tab character is placed next to the new line character to
#    create a better view of the message on the screen. The result
#    is placed in the tmp.msg file.
#
#    The csplit statement splits the tmp.msg file into separate xxnn
#    files by the E_*__MSGS symbols used to indicate message groupping.
#    The xx00 file is copied into the output to create the oam_def.h
#    file. The tmp1.msg.source, tmp.msg and xx00 files are removed.
#
#    The next two loops read the xxnn files and generates the define
#    statements for each E_*_* symbol and places them into the
#    output for the oam_def.h file and create the e_*__msgs.c
#    structure arrays to be compiled into the code.
#    
##########

sed '/ID+Copyright-Notice-Trailer/,$ d' msg.source > lp.messages # abs s21

grep -v '^[ 	]*\(\/\*\)\(.*\)$' msg.source |
grep -v '^[ 	]*\(\*\*\)\(.*\)$' |
grep -v '^[ 	]*\(\*\)\(.*\)$'   | 
grep -v '^[ 	]*\(\*\/\)\(.*\)$' |
grep -v '^[ 	]*$' > tmp.msg.source

sed -e 's/\\n/\\n\\t/gp' < tmp.msg.source \
| sed -n -e 's/^[ 	]*\("\)\(.*\)\("\)$/\2\\n/p' >> lp.messages # abs s21
 
nl -bp"^E_[A-Z]*_[A-Z,0-9]" -v1 -i2 -s: tmp.msg.source \
| sed \
      -e 's/^[  ]*\([0-9]*\):\(.*\)$/\2 \1,/'  \
| sed -e 's/^[ 	]*\([E#]\)\(.*\)/\1\2/' \
| sed -e 's/\\n/%%%/gp' \
| sed -e 's/\\t/###/gp' \
| sed -e 's/\\/\\\\\\/gp' > tmp1.msg.source

rm tmp.msg.source

COPY=1
HLINE=1
for F in 'E_LP_'  'E_FL_'  'E_CAN_'  'E_CMB_' \
         'E_MOV_' 'E_LPU_' 'E_LPP_'  'E_SHT_' \
         'E_ADM_' 'E_FOR_' 'E_STAT_' 'E_SYS_' \
         'E_DAT_' 'E_SCH_'
do
   FF=${F}_
   FA=${FF}MSGS
   IFS="_"
   while read BASE  BASEA BASEB
   do
      IFS=""
      BASEC=${BASE}_${BASEA}__${BASEB}
      BASEE=${BASE}_${BASEA}_${BASEB}
      if [ ${FA} = ${BASEC} ]
        then
           COPY=1
           if [ ${HLINE} = 1 ]
             then
                /bin/echo ${BASEC}
                HLINE=0
           fi
      else
         if [ ${F} = ${BASE}_${BASEA}_ ]
           then
              COPY=1
              /bin/echo ''${BASEE}''
         else
            BASED=${BASE}_
            if [ ${BASED} = "E_" ]
              then
                 COPY=0
            else
               if [ ${COPY} = 1 ]
                 then
                    /bin/echo ''${BASEE}''
               fi
            fi
         fi
      fi
      IFS="_"
   done < tmp1.msg.source
   IFS=""
   COPY=0
   HLINE=1
   if [ ${F} != "E_LP_" ]
     then
        /bin/echo "#endif"
   fi
done | sed -e 's/%%%/\\n\\t/gp' \
| sed -e 's/###/\\t/gp' \
| sed -e 's/^\(.*\)__$/\1/p' \
| sed -e 's/^\(.*\)_$/\1/p' > tmp.msg

/bin/echo "/*This file is automatically generated from msg.source.*/"

csplit -k -s tmp.msg /E_.*__MSGS/ {99} 2>/dev/null

sed -n -e '/^[ 	]*#/p' xx00
 
rm xx00 tmp1.msg.source tmp.msg

/bin/echo 'extern char		**_oam_msg_base_;'
for x in xx??
do
   BASE=`line <${x}`
   base=`echo ${BASE} | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz'`
   /bin/echo 'extern char		*'${base}'[];'
done

for x in xx??
do

    (
	BASE=`line`
	base=`echo ${BASE} | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz'`

	/bin/echo "${BASE}"

	sed -e '/^[ 	]*$/d' \
	| sed -n \
      -e 's/^[ 	]*\(E_.*\)$/\1:(_oam_msg_base_='${base}',(long)(:-'${BASE}'))/p' \
      -e '/^[ 	]*#/p'
    ) <${x}

done  \
| nl -bp"^E_" -v0 -i2 -s: \
| sed \
      -e 's/^[ 	]*\([0-9]*\):\(E_.*\):\(.*\):\(.*\)$/#define \2 \3\1\4/' \
      -e 's/^[ 	]*\([0-9]*\):\(E_.*\)$/#define \2 (\1+2)/' \
| sed -e 's/^[ 	]*//' \
