#!/bin/bash
#set -x

function error
{
  exit $1
}

function convertToPCClient
{
PCLANG=$(echo $NEWLANG | cut -b 1-3)
found=0
case $NEWLANG in
    "ca_ES") found=1 ;;
    "cs_CZ") found=1 ;;
    "de_DE") found=1 ;;
    "en_US") found=1 ;;
    "es_ES") found=1 ;;
    "fr_FR") found=1 ;;
    "hu_HU") found=1 ;;
    "it_IT") found=1 ;;
    "ja_JP") found=1 ;;
    "ko_KR") found=1 ;;
    "nl_NL") found=1 ;;
    "pl_PL") found=1 ;;
    "pt_BR") found=1 ;;
    "ru_RU") found=1 ;;
    "sk_SK") found=1 ;;
    "zh_CN") found=1 ;;
    "zh_TW") found=1 ;;
esac
#echo $found
if [ $found -eq 0 ] ; then
  case $PCLANG in
    "ca_") PCLANG="ca_ES" ;;
    "cs_") PCLANG="cs_CZ" ;;
    "de_") PCLANG="de_DE" ;;
    "en_") PCLANG="en_US" ;;
    "es_") PCLANG="es_ES" ;;
    "fr_") PCLANG="fr_FR" ;;
    "hu_") PCLANG="hu_HU" ;;
    "it_") PCLANG="it_IT" ;;
    "ja_") PCLANG="ja_JP" ;;
    "ko_") PCLANG="ko_KR" ;;
    "nl_") PCLANG="nl_NL" ;;
    "pl_") PCLANG="pl_PL" ;;
    "pt_") PCLANG="pt_BR" ;;
    "ru_") PCLANG="ru_RU" ;;
    "sk_") PCLANG="sk_SK" ;;
        *) PCLANG="en_US" ;;
 esac
  case $NEWLANG in
    "zh_HK") PCLANG="zh_TW" ;;
    "zh_SG") PCLANG="zh_CN" ;;
  esac
else
   PCLANG=$NEWLANG
fi

}

NEWLANG=$1

if [ "$1" = "" ] ; then
  echo "ERROR: Null Locale value"
  error 5
fi


echo $NEWLANG > /opt/ccfw/data/language

#
# customize enviroment for different locales
#
if [ "$NEWLANG" = "ru_RU" ] 
then
#  NEWLANG=ru_RU.koi8r
# Doesn't need koi8r for MCP
  NEWLANG=ru_RU
elif [ "${NEWLANG%%_*}" = "ja" ] 
then
  NEW_XMODIFIERS="@im=kinput2" 
elif [ "$NEWLANG" = "zh_CN" -o "$NEWLANG" = "zh_CN.UTF-8" ]
then
  NEW_XMODIFIERS="@im=xcin-zh_CN" 
elif [ "$NEWLANG" = "zh_SG" -o "$NEWLANG" = "zh_SG.UTF-8" ]
then
  NEW_XMODIFIERS="@im=xcin-zh_SG" 
elif [ "$NEWLANG" = "zh_TW" -o "$NEWLANG" = "zh_TW.UTF-8" ]
then
  NEW_XMODIFIERS="@im=xcin-zh_TW" 
elif [ "$NEWLANG" = "zh_HK" -o "$NEWLANG" = "zh_HK.UTF-8" ]
then
  NEW_XMODIFIERS="@im=xcin-zh_HK" 
elif [ "${NEWLANG%%_*}" = "ko" ]
then
  NEW_XMODIFIERS="@im=Ami" 
else
  NEW_XMODIFIERS="" 
fi

#
# Update LANG and XMODIFIERS in /etc/SuSEconfig/profile
#
sed "s/LANG=.*$/LANG=\"${NEWLANG}\"/" /etc/SuSEconfig/profile > /var/hsc/tmp/i18n
if [ $? != 0 ] ; then
  echo "ERROR: Could not create locale in /var/hsc/tmp"
  error $?
fi
cp /var/hsc/tmp/i18n /etc/SuSEconfig/profile  2>/dev/null
if [ $? != 0 ] ; then
  echo "ERROR: Could not copy locale file to /etc/SuSEconfig/profile"
  error $?
fi

sed "s/XMODIFIERS=.*$/XMODIFIERS=\"${NEW_XMODIFIERS}\"/" /etc/SuSEconfig/profile > /var/hsc/tmp/i18n
if [ $? != 0 ] ; then
  echo "ERROR: Could not create locale in /var/hsc/tmp"
  error $?
fi
cp /var/hsc/tmp/i18n /etc/SuSEconfig/profile  2>/dev/null
if [ $? != 0 ] ; then
  echo "ERROR: Could not copy locale file to /etc/SuSEconfig/profile"
  error $?
fi

rm /var/hsc/tmp/i18n 2>/dev/null

if [ $? != 0 ] ; then
  echo "ERROR: Could not remove /var/hsc/tmp/i18n"
fi

#if [ "$NEWLANG" = "ru_RU.koi8r" ] ; then
#  grep InputDevice /etc/X11/XF86Config | grep KeyboardRussian
#  if [ $? != 0 ] ; then
#    /opt/hsc/bin/changeRussianKeyboard set
#  fi
#else
#  grep InputDevice /etc/X11/XF86Config | grep KeyboardRussian
#  if [ $? == 0 ] ; then
#    /opt/hsc/bin/changeRussianKeyboard unset
#  fi
#fi
convertToPCClient
#echo $PCLANG

if [ "$PCLANG" == "" ] ; then
PCLANG=$NEWLANG
fi

rm -f /opt/hsc/data/keycfgcheck

if [ $? != 0 ] ; then
  echo "ERROR: Unable to remove keyboard config file"
  error 3333
fi

exit 0
