#!/bin/bash
thisdir=`pwd`/$(dirname "$0")
[ "$(echo $0 | grep '\./')" = "" ] && [ "$(echo $0 | grep '^/.*')" = "" ] && [ `pwd` = '/' ] && thisdir=`pwd`$(dirname "$0")
[ "$(echo $0 | grep '^/.*')" != "" ] && thisdir=$(dirname "$0")
typeset -i counter

locale_set=$LC_ALL
[ "$locale_set" ] || locale_set=$LC_MESSAGES
[ "$locale_set" ] || locale_set=$LANG
[ "$locale_set" ] || locale_set=en_US.UTF-8

# use character encoding from the command 'locale charmap'
# remove all " chars from the output. The " chars are added on the HP UX.
# remove dot char '.' and all chars after it
encoding=$(locale charmap | sed "s/\"//g" | sed "s/\..*//")

# if we don't know the encoding
if [ "$encoding" = "" ]
then
  # separate encoding from the locale_set. Remove:
  # - the begining when it contains '_'
  # - the end after '@' if it exists
  # e.g. fr_FR.IBM-1252@preeuro is converted to IBM-1252
  encoding=$(echo $locale_set | sed "s/^.*_.*\.//" | sed "s/(.*)@.*/$1/")

  # if encoding is POSIX or C used default UTF-8
  if [[ "$encoding" = "POSIX" || "$encoding" = "C" ]]
  then
    encoding="UTF-8"
  fi
fi

case "$locale_set" in
  ar*|AR*|Ar*) locale_set="ar";;
  de*|DE*|De*) locale_set="de";;
  cs*|CS*|Cs*) locale_set="cs";;  
  es*|ES*|Es*) locale_set="es";;
  fr*|FR*|Fr*) locale_set="fr";;
  hu*|HU*|Hu*) locale_set="hu";;
  it*|IT*|It*) locale_set="it";;
  ja*|JA*|Ja*) locale_set="ja";;
  ko*|KO*|Ko*) locale_set="ko";;
  pt*br*|pt*BR*|PT*BR*|Pt*BR*) locale_set="pt_BR";;
  pl*|PL*|Pl*) locale_set="pl";;
  ru*|RU*|Ru*) locale_set="ru";;
  zh*tw*|zh*TW*|ZH*TW*|tw*|TW*|Zh*TW*|Zh*tw*) locale_set="zh_TW";;
  zh*|ZH*|Zh*) locale_set="zh_CN";;
  *) locale_set="en";;
esac

msgdir=$(ls "$thisdir" | grep 'msg')
if [ "$msgdir" = "msg" ] 
then
  properties_dir="$thisdir/msg"
else
  properties_dir="$thisdir/../config/nls/msg"
fi

# check if iconv exists in the system 
iconv_path="$(whence iconv 2>/dev/null)"
if [ -z "$iconv_path" ]
then
  # if iconv does not exist in the system, set the default english version
  locale_set="en";
fi

[ -f "$properties_dir/$locale_set/sh_$locale_set.properties" ] || locale_set="en";

prop_file="$properties_dir/$locale_set/sh_$locale_set.properties"

key="^$1="

if [[ "$encoding" = "UTF-8" || "$encoding" = "UTF8" || "$encoding" = "utf8" || "$encoding" = "utf-8" ]]
then
  # do not convert
  text1=$(grep $key "$prop_file" | sed "s/$1=//" 2>/dev/null)

  # if message not found in localized file, use English
  if [ "$text1" = "" ]
  then
    locale_set="en";
    prop_file="$properties_dir/$locale_set/sh_$locale_set.properties"
    text1=$(grep $key "$prop_file" | sed "s/$1=//" 2>/dev/null)
  fi 
else
  # convert to the proper coding
  text1=$(iconv -f UTF-8 -t $encoding "$prop_file" 2>/dev/null | grep $key | sed "s/$1=//" )

  # If we cannot convert, just print the default english version.
  # It is much better then nothing or illegible chars.
  if [ "$text1" = "" ]
  then
    locale_set="en";
    prop_file="$properties_dir/$locale_set/sh_$locale_set.properties"
    text1=$(grep $key "$prop_file" | sed "s/$1=//" 2>/dev/null)
  fi
fi

shift 
counter=1
while [ $# -ne 0 ] ; do
text1=$(echo $text1 | sed "s%\$$counter%$1%")
shift
((counter = counter + 1))
done

echo $text1


