#!/bin/sh

#
# Nano-X PC emulation environment setup script
#

check_dir ()
{
   if [ ! -d $1 ]; then
      echo
      echo "Could not find directory $1"
      echo
      exit 0
   fi
   echo "Found $1"
}


#
# locate EmLinux installation directory
#
if [ x$NANOGTK_HOME = x ]; then
   INSTDIR=`dirname \`pwd\``
   NANOGTK_HOME=$INSTDIR
else
   INSTDIR=$NANOGTK_HOME
fi


#
# set testing directory
#
TESTDIR=$INSTDIR/Testbench

check_dir $TESTDIR


#
# get machine dependent information
#
CONFIG=$TESTDIR/local/etc/platform.conf
if [ -f $TESTDIR/local/etc/platform.conf ]; then
   . $CONFIG
else
   echo
   echo "Could not find $CONFIG"
   echo
   exit 0
fi


#
# set up environment variables
#
#SYSCONFIG=$TESTDIR/bin/sysconf
GTKRCFILEDIR=$TESTDIR/local/etc/gtk

LIBGLIB=$INSTDIR/glib-1.2.7/lib-$MARCH
LIBGTK=$INSTDIR/gtk+-1.2.7/lib-$MARCH
LIBMICROWIN=$INSTDIR/microwin/lib-$MARCH
LIBAPPLICATION=$INSTDIR/Application/lib-$MARCH:$INSTDIR/EmGdkPixbuf/lib-$MARCH

export LD_LIBRARY_PATH=$LIBGLIB:$LIBGTK:$LIBMICROWIN:$LIBAPPLICATION
export FONT_PATH=$TESTDIR/fonts
export PATH=$INSTDIR/gtk+-1.2.7/bin-$MARCH:$INSTDIR/glib-1.2.7/bin-$MARCH
export PATH=$PATH:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:.
export LOCALE_DIR=$TESTDIR/local/share/locale
export XIN_DICT_DIR=$TESTDIR/local/share/dicts
export HWR_DATA_FILE=$TESTDIR/local/share/hwr/gb_hsrecog.dat


#
# set locale from command argument
#
if [ "x-gbx"   = "x$1x" ]; then       # Simplified Chinese
        language=gb
        command=$2
elif [ "x-big5x" = "x$1x" ]; then     # Traditional Chinese
        language=big5
        command=$2
elif [ "x-jisx" = "x$1x" ]; then      # Japanese
        language=jis
        command=$2
elif [ "x-ksx" = "x$1x" ]; then       # Korean
        language=ks
        command=$2
else
#        if [ -x ${SYSCONFIG} ]; then
#                language=`${SYSCONFIG} --get language encoding`
#        fi
        command=$1
fi

if [ "xgbx" = "x${language}x" ]; then
        export LC_ALL=zh_CN.GB2312
        export GTK_RC_FILES=$GTKRCFILEDIR/gtkrc.zh_CN
elif [ "xbig5x" = "x${language}x" ]; then
        export LC_ALL=zh_TW.Big5
        export GTK_RC_FILES=$GTKRCFILEDIR/gtkrc.zh_TW
elif [ "xksx" = "x${language}x" ]; then
        export LC_ALL=ko
        export GTK_RC_FILES=$GTKRCFILEDIR/gtkrc.ko
else
        export LC_ALL=en_US
        export GTK_RC_FILES=$GTKRCFILEDIR/gtkrc.en_US
fi

echo
echo "LC_ALL=$LC_ALL"
echo "GTK_RC_FILES=$GTK_RC_FILES"
echo


#
# Application specific setup
#
pkgdir=$TESTDIR/local/package
if [ -d $pkgdir ]; then
   apps=`ls $pkgdir`

   for a in $apps
   do
      if [ -d $pkgdir/$a ]; then
         fn=`ls $pkgdir/$a/*.ini`

         # consider 1 file only
         if [ -f $fn ]; then
            . $fn
         fi
      fi
   done
fi

unset LIBAPPLICATION
unset LIBMICROWIN
unset LIBGTK
unset LIBGLIB
unset TESTDIR
unset INSTDIR

