#!/bin/sh
#----------------------------------------------------------------------------
#  Name:     XDmakeMsgCat
#  Project:  Xdiary
#
#  Description:
#    Create a message catalogue file from a template.
#
#   To convert a text to a message catalogue, use the tool XDmakeMsgCat
#   (syntax: XDmakeMsgCat textDomain language), e.g.
#
#     XDmakeMsgCat XDiary En_US
#
#  Filename:         XDmakeMsgCat
#
#  Authors:          Roger Larsson, Ulrika Bornetun
#  Creation date:    1992-04-22
#
#
#  (C) Copyright Ulrika Bornetun, Roger Larsson (1995)
#      All rights reserved
#
#  Permission to use, copy, modify, and distribute this software and its
#  documentation for any purpose and without fee is hereby granted,
#  provided that the above copyright notice appear in all copies. Ulrika
#  Bornetun and Roger Larsson make no representations about the usability
#  of this software for any purpose. It is provided "as is" without express
#  or implied warranty.
#----------------------------------------------------------------------------
#
# SCCSID = @(#) Module: XDmakeMsgCat, Version: 1.1, Date: 95/02/18 14:15:06

language=""
msgFile=""

# Default parameters?
if [ $# -eq 3 ]; then
  msgFile="$1"
  shift
  language="$1"
  shift
  sourceDir="$1"
fi

if [ -z "$msgFile" ]; then
  while [ True ]; do
    echo "Text domain:      "
    read msgFile

    echo "Language:         "
    read language

    echo "Source directory: "
    read sourceDir

    if [ ! -r $sourceDir/${msgFile}Msg${language}.txt ]; then
      echo ""
      echo "Cannot open the message text file"
      echo "  $sourceDir/${msgFile}Msg${language}.txt."
      echo ""
    else
      break
    fi
  done
fi

if [ ! -r $sourceDir/${msgFile}Msg${language}.txt ]; then
  echo ""
  echo "Cannot open the message text file"
  echo "  $sourceDir/${msgFile}Msg${language}.txt."
  echo ""
  exit 1
fi

# Translate the message text file.
/bin/rm -f msgCatCompiled 2>/dev/null

cat $sourceDir/${msgFile}Msg${language}.txt |
  awk '/^[ ]*#/ {
	   next
	 }
       /^[ ]*!/ {
	   next
	 }
       /^[ ]*$/ {
	   next
	 }
       /^[ ]+\/\*[0-9 ]+\*\/[ ]+/ {
	   split( $0, fields, "\"" )
	   printf "i%s:%s\n", $2, fields[ 2 ] >> "msgCatCompiled"
	   msgs++
	   size=size+length( fields[ 2 ] )+1
	   next
	 }
       /^[ ]+\"/ {
	   split( $0, fields, "\"" )
	   printf ">%s\n", fields[ 2 ] >> "msgCatCompiled"
	   size=size+length( fields[ 2 ] )+1
	   next
	 }
       { printf "Unknown line, skipped:\n <%s>\n", $0 }
       END { printf "@(#):1.0:'$language'\n"  > "msgCatStats"
	     printf "s%d:%d\n", msgs, size   >> "msgCatStats"
	   }
  '
if [ $? -ne 0 ]; then
  exit 1
fi

# Assemble the complete file.
cat msgCatStats                        > ${msgFile}Msg${language}.cat
cat msgCatCompiled | sed 's/\\n//g' >> ${msgFile}Msg${language}.cat

/bin/rm -f msgCatStats    2>/dev/null
/bin/rm -f msgCatCompiled 2>/dev/null

exit 0
