#!/usr/bin/env bash
#  ------------------------------------------------------------------------
#  Title        : GetMessage Funktion - Include File fuer Bourne sh Skripte
#  ------------------------------------------------------------------------
#                  This is an UNPUBLISHED work COPYRIGHT by
#		              ArtCom GmbH
#                   Haferwende 2; D-28357 Bremen; Germany
#       It may be used, copied, or distributed only as permitted in a
#                              written license.
#  ------------------------------------------------------------------------
#  Created      :       Thu, 06-Feb-92 / 19:03 / (pf)
#  Version      : $Id: .Messages,v 1.6 2016-05-26 15:05:17 tm Exp $
#  Portability  : XENIX sh
#  State        : under development
#  Purpose      : belongs to the XENIX port of BITS
#I Usage        : insert this header file into each script
#I Remarks      : all lines marked with '#I' are used with 'info'
#I              : Zeilen mit Shell-Variablen muessen im Catalog mit An-
#I              : fuehrungszeichen geklammert sein. Sie koennen mit GetMessage
#I              : expandiert geholt werden, wobei leider Blanks verloren gehen.
#I              : Print() und PrintLn() geben unexpandiert direkt aus, evtl.
#I              : vorhandene Anfuehrungszeichen werden mit ausgegeben.
#I              : Format der Katalogeintraege :
#I              : sectionN@msgNum@Message
#  ------------------------------------------------------------------------
#
# Default Message-Katalog :
#: ${LANG:=deu}
#: ${MSG_CAT:=/bitslib/env/shellmsg.${LANG}}
[ "$MSG_CAT" ] || { 
    echo 'MSG_CAT nicht definiert' ; exit 1 
}

# PROCEDURE Print (sectionN : ARRAY OF CHAR; msgNum : CARDINAL);
# druckt die Meldung _ohne_ Zeilenende und _ohne_ Shell-Var-Expansion
Print() {
    awk 'BEGIN { FS="@"; found = 0 }
	 $1 == sectionN && $2 == msgNum { printf $3; found = 1; exit }
					{ next }
	 END { if (found == 0)
	       printf "Message (" sectionN ", " msgNum ") not found" }' \
	sectionN=$1 msgNum=$2 $MSG_CAT 
}

# PROCEDURE PrintLn (sectionN : ARRAY OF CHAR; msgNum : CARDINAL);
# druckt die Meldung _mit Zeilenende aber _ohne_ Shell-Var-Expansion
PrintLn() {
    awk 'BEGIN { FS="@"; found = 0 }
	 $1 == sectionN && $2 == msgNum { print $3; found = 1; exit }
					{ next }
	 END { if (found == 0)
	       print "Message (" sectionN ", " msgNum ") not found" }' \
	sectionN=$1 msgNum=$2 $MSG_CAT 
}

# PROCEDURE MultiPrint (sectionN : ARRAY OF CHAR; msgNum : ARRAY OF CARD);
# druckt die Meldungen nacheinander mit Zeilenende aus
MultiPrint() {
    SECTIONN=$1
    shift
    awk 'begin == 0 { begin=1; n = split(msgNums, numArray)
		      FS="@"
		      for (i = 1; i <= n; i++)
			  msg[i] = "Message ("sectionN", "numArray[i]") not found" }
	 $1 == sectionN { for (i = 1; i <= n; i++)
			      if (numArray[i] == $2) msg[i] = $3 }
					{ next }
	 END { for (i = 1; i <= n; i++) print msg[i] }' \
	sectionN=$SECTIONN msgNums="$*" $MSG_CAT
}

# PROCEDURE BlockPrint (sectionN : ARRAY OF CHAR; blkName : ARRAY OF CHAR);
# Gibt alle Zeilen zwischen zwei "<sectionN>@<blkName>@" per 'echo' aus.
# Beispiel :
# ----------
# testBlock@0@
# `clear`\c
# Der Wert der Variablen TEST ist '$TEST'
# testBlock@0@
# Aufruf :
#   "TEST=99; export TEST; BlockPrint testBlock 0"
# Loescht den Bildschirm und gibt dann den String
#   "Der Wert der Variablen TEST ist '99'"
# aus (ohne die doppelten Anfuehrungszeichen).
BlockPrint()
{ awk 'mark == "" {found=0; mark=sectionN "@" blkName "@"}
       $0 ~ mark {if (found == 1) exit; else found=1; next;}
       found == 1 {print "echo \"" $0 "\""}
       END        {if (found == 0)
                      printf "echo \"MessageBlock(%s,%s) not found\"", sectionN, blkName;
                  }
      ' sectionN="$1" blkName="$2" $MSG_CAT | sh
}
# PROCEDURE GetMsg (sectionN : ARRAY OF CHAR; msgID : ARRAY OF CHAR);
# Da es so schoen war, noch dies. GetMessage() lieber nicht ersetzt.
# Wer weiss, was fuer Seiteneffekte. (mk)
GetMsg ()
{ awk 'BEGIN { FS="@"; }
       $1 == sectionN && $2 == msgID {print "echo \"" $3 "\""; found=1; exit}
       END {if (!found)
	       printf "echo \"Message(%s,%s) not found\"", sectionN, msgID;
	   }
      ' sectionN=$1 msgID=$2 $MSG_CAT | sh
}

# PROCEDURE GetMessage (sectionN : ARRAY OF CHAR; msgNum : CARDINAL);
# laedt die Meldung in $MESSAGE und zwar _mit_ Shell-Var-Expansion
# leider werden Blanks komprimiert !
GetMessage () {
    MESSAGE="`awk -f $MESSAGES_AWK \
	      sectionN=$1 msgNum=$2 $MSG_CAT`"
    MESSAGE="`eval echo $MESSAGE`"
    if [ -z "$MESSAGE" ] && [ "$LANGUAGE" != eng ]
    then MSG_CAT_ENG=`echo "$MSG_CAT" | LC_ALL=C sed -e "s/\.$LANGUAGE$/\.eng/"`
         MESSAGE="`awk -f $MESSAGES_AWK \
                   sectionN=$1 msgNum=$2 $MSG_CAT_ENG`"
         MESSAGE="`eval echo $MESSAGE`"
    fi
    if [ -z "$MESSAGE" ]
    then MESSAGE="message text not found ($MSG_CAT, $1, $2)"
    fi
}

[ -z "$BITSLIB" ] && BITSLIB=/bitslib
MESSAGES_AWK="$BITSLIB/bin/.Messages.awk"
export MESSAGES_AWK
