#!/bin/bash
#
# Sample .xinitrc for SuSE Linux
# This script is called from 'startx' when you start an X session
#
# save PID in file first so logoff hook code can use to send SIGUSR2
echo "$$" > /var/run/hmcxinitrc.pid
#
# In case everything goes wrong, we at least fall back to a plain xterm
#
failsafe="/opt/hsc/bin/launchrshterm"
trap "exec $failsafe" SIGINT SIGPIPE SIGTERM SIGIO
trap "exit 0" SIGUSR2

rm /opt/hsc/data/pendingLocaleChange


#
# Some bash (1 and 2) settings to avoid trouble on a
# failed program call.
#
test -n "$BASH" && set +o posix
no_exit_on_failed_exec=1
type shopt > /dev/null 2>&1 && shopt -s execfail
set +e > /dev/null 2>&1

#
# This should be the default
#
export TERM=xterm
#
# choose a window manager
#
if test -n "$WINDOWMANAGER" ; then
    WINDOWMANAGER=`type -p $WINDOWMANAGER`
fi
if test -z "$WINDOWMANAGER" ; then
    if test -x /usr/X11R6/bin/kde ; then
        WINDOWMANAGER=/usr/X11R6/bin/kde
    elif test -x /usr/X11R6/bin/startkde ; then
        WINDOWMANAGER=/usr/X11R6/bin/startkde
    elif test -x /usr/X11R6/bin/fvwm2 ; then
        WINDOWMANAGER=/usr/X11R6/bin/fvwm2
    elif test -x /usr/X11R6/bin/wmlist ; then
        for i in `/usr/X11R6/bin/wmlist` ; do
            WINDOWMANAGER=`type -p $i`
            test -n "$WINDOWMANAGER" && break
        done
    elif test -x /usr/X11R6/bin/twm ; then
        WINDOWMANAGER=/usr/X11R6/bin/twm
    fi
fi

if test -z "$WINDOWMANAGER" ; then
    echo "Error: Unable to find a window manager. Please make sure you installed one!"
    echo "Exiting..."
    xmessage -timeout 10 -default okay -center -file - <<-EOF
	Error: Unable to find a window manager. Please make sure you installed one!
	Exiting...
	EOF
    exit 1
fi

#
# Load system and users resources if not already done
# (XSESSION_IS_UP set by xdm in $XLIBDIR/xdm/Xsession)
#
if test "$XSESSION_IS_UP" != "yes" ; then
    XLIBDIR=/usr/X11R6/lib/X11
    test -r $XLIBDIR/Xmodmap    && xmodmap $XLIBDIR/Xmodmap
    test -r $HOME/.Xmodmap      && xmodmap $HOME/.Xmodmap
    test -r $XLIBDIR/Xresources && xrdb -load -retain $XLIBDIR/Xresources
    test -r $HOME/.Xdefaults    && xrdb -I$HOME -merge $HOME/.Xdefaults
    test -r $HOME/.Xresources   && xrdb -I$HOME -merge $HOME/.Xresources
fi

# Start the XIM server
test -r $HOME/.xim && source $HOME/.xim

# Enable Numlock if set
test -r /var/run/numlock-on -a -x /usr/X11R6/bin/numlock && /usr/X11R6/bin/numlock

#
# Add your own lines here...
#

/opt/hsc/bin/licenseManager
if [ -f /opt/esshmc/bin/consolestartup ]
then
	. /opt/esshmc/bin/consolestartup
fi

DISPLAY=127.0.0.1:0 xhost +127.0.0.1
export DISPLAY

LANG=`cat /opt/ccfw/data/language`
export LANG
# Start DBCS xim server
LANGPREFIX=${LANG%%_*}
if [ "$LANGPREFIX" = "ja" ]
then
	/usr/X11R6/bin/kinput2 -xim -kinput -canna &
elif [ "$LANGPREFIX" = "zh" ]
then	
	RUN_LOCALE=${LANG%%.*}
	LANG=$RUN_LOCALE /usr/X11R6/bin/xcin -d 127.0.0.1:0 -r /opt/hsc/data/xcinrc &
elif [ "$LANGPREFIX" = "ko" ]
then	
	LANG=ko_KR /usr/bin/ami -nocp &
else
	:
fi
/usr/bin/fluxbox -rc /opt/hsc/data/fluxbox/en_US/init


# call failsafe
exit 0
