#!/bin/bash

########################################################################
# -*- sh -*-                                                           #
#                                                                      #
# ~/.Xclients: used by startx (xinit) to start up a window manager and #
# any other clients you always want to start an X session with.        #
#                                                                      #
# This version of Xclients is intended for use with wdm.  This         #
# separate file is used rather than the Red Hat Linux Xclients         #
# file located as /etc/X11/xinit/Xclients because it has been          #
# extensively modified to handle starting many different window        #
# or session managers and do that on systems other than Red Hat Linux. #
#                                                                      #
# Systems other than RedHat Linux will like need editing of this       #
# file to tailor the file for the particular system.                   #
#                                                                      #
########################################################################

# these files are left sitting around by TheNextLevel.
rm -f $HOME/Xrootenv.0
rm -f /tmp/fvwmrc* 2>/dev/null

# get user ime setting
userime=$HOME/.vine/system/ime

if [ -f $userime ]; then
    source $userime
fi

# Select IME
echo -n "Selecting IME... " > $HOME/.xwm.msgs
case "$XIM_PROG" in
    wnn* | Wnn*)
        case "$WNN6_FRONT_END" in
            xwnmo)
                xwnmo -D $WNN6_SERVER &
                echo "Wnn6 with xwnmo, server=$WNN6_SERVER" >>$HOME/.xwm.msgs
                XMODIFIERS="@im=_XWNMO"
                TK_KCPROTO=xim
                ;;
            *)
                if [ "$WNN6_SERVER" = "" ] ; then
                    WNN6_SERVER=localhost
                fi
                if [ -f /usr/local/OMRONWnn6/wnn6linux/ja_JP/wnnenvrc ] ; then
                    WNNENVRC=/usr/local/OMRONWnn6/wnn6linux/ja_JP/wnnenvrc
                else
                    WNNENVRC=/etc/wnn6/ja_JP/wnnenvrc
                fi
                kinput2 -wnn -jserver $WNN6_SERVER -wnnenvrc6 $WNNENVRC &
                echo "Wnn6 with kinput2, server=$WNN6_SERVER" >>$HOME/.xwm.msgs
                XMODIFIERS="@im=kinput2"
                TK_KCPROTO=kinput2
        esac
        ;;
    *)
        if [ "$CANNA_SERVER" = "" ] ; then
            if [ "$CANNAHOST" != "" ] ; then
                export CANNA_SERVER=$CANNAHOST
            else
                export CANNA_SERVER=localhost
            fi
        fi
        export CANNAHOST=$CANNA_SERVER
        kinput2 -canna -cannaserver $CANNA_SERVER &
        echo "Canna with kinput2, server=$CANNA_SERVER" >>$HOME/.xwm.msgs
        XMODIFIERS="@im=kinput2"
        TK_KCPROTO=kinput2
        ;;
esac
export XMODIFIERS TK_KCPROTO

# define font size
HEIGHT=`xrdb -symbol|grep HEIGHT`
if [ ${HEIGHT#-D} -lt 768 ]; then
        XFONTSIZE=small
else
        XFONTSIZE=normal
fi
export XFONTSIZE

# First thing - check the user preferences
if [ -f $HOME/.wm_style ] ; then
    WMSTYLE=`cat $HOME/.wm_style`
    case "$WMSTYLE" in 
#
# first, a general purpose entry:
# 	*)
# 	    # startup a window manager
# 	    if [ -x  ] ; then
# 	        echo Starting  >>$HOME/.xwm.msgs
# 		exec  >>$HOME/.xwm.msgs 2>&1
# 	    fi
# 	    ;;
#
	wmaker*|WindowMaker*)
	    # startup WindowMaker
	    if [ -x /usr/X11R6/bin/wmaker ] ; then
	        echo Starting WindowMaker >>$HOME/.xwm.msgs
		env > "$HOME"/Xrootenv.0
		exec /usr/X11R6/bin/wmaker --WindowMaker >>$HOME/.xwm.msgs 2>&1
	    fi
	    ;;
# 	afterstep*|Afterstep*|AfterStep*)
# 	    # we have to start up afterstep
# 	    if [ -x  ] ; then
# 	        echo Starting AfterStep >>$HOME/.xwm.msgs
# 		env > "$HOME"/Xrootenv.0
# 		# if this works, we stop here
# 		exec  --AfterStep >> $HOME/.xwm.msgs 2>&1
# 	    fi
# 	    ;;
# 	blackbox*|BlackBox*|BLACKBOX*)
# 	    # startup blackbox
# 	    if [ -x  ] ; then
# 	        echo Starting BlackBox >>$HOME/.xwm.msgs
# 	        env > "$HOME"/Xrootenv.0
# 		exec  >>$HOME/.xwm.msgs 2>&1
# 	    fi
# 	    ;;
# 	icewm*|IceWm*|ICEWM*)
# 	    # startup icewm
# 	    if [ -x  ] ; then
# 	        echo Starting icewm >>$HOME/.xwm.msgs
# 	        env > "$HOME"/Xrootenv.0
# 		exec  >>$HOME/.xwm.msgs 2>&1
# 	    fi
# 	    ;;
#gnomecm#	gnome*|Gnome*|GNOME*)
#gnomecm#	    # startup gnome
#gnomecm#	    if [ -x #GNOME_PATH# ] ; then
#gnomecm#	        echo Starting Gnome >>$HOME/.xwm.msgs
#gnomecm#	        env > "$HOME"/Xrootenv.0
#gnomecm#		exec #GNOME_PATH# >>$HOME/.xwm.msgs 2>&1
#gnomecm#	    fi
#gnomecm#	    ;;
# 	startkde*|kde*|KDE*)
# 	    # startup kde
# 	    if [ -x  ] ; then
# 	        echo Starting kde >>$HOME/.xwm.msgs
# 	        env > "$HOME"/Xrootenv.0
# 		exec  >>$HOME/.xwm.msgs 2>&1
# 	    fi
# 	    ;;
#ecm#          Enlightenment*|ENLIGHT*)
#ecm#              # startup enlightenment
#ecm#              if [ -x #E_PATH# ] ; then
#ecm#                  echo Starting enlightenment >>$HOME/.xwm.msgs
#ecm#	           env > "$HOME"/Xrootenv.0
#ecm#                  exec #E_PATH# >>$HOME/.xwm.msgs 2>&1
#ecm#              fi
#ecm#              ;;
#qvwmcm#        qvwm*|QVWM*)
#qvwmcm#            # startup qvwm
#qvwmcm#            if [ -x #QVWM_PATH# ] ; then
#qvwmcm#                echo Starting qvwm >>$HOME/.xwm.msgs
#qvwmcm#                env > "$HOME"/Xrootenv.0
#qvwmcm#                exec #QVWM_PATH# >>$HOME/.xwm.msgs 2>&1
#qvwmcm#           fi
#qvwmcm#           ;;
#fvwm2cm#       fvwm2*|FVWM2*)
#fvwm2cm#           # startup fvwm2
#fvwm2cm#           if [ -x #FVWM2_PATH# ] ; then
#fvwm2cm#               echo Starting fvwm2 >>$HOME/.xwm.msgs
#fvwm2cm#               env > "$HOME"/Xrootenv.0
#fvwm2cm#               exec #FVWM2_PATH# >>$HOME/.xwm.msgs 2>&1
#fvwm2cm#           fi
#fvwm2cm#           ;;
#twmcm#         twm*|TWM*)
#twmcm#             # startup twm
#twmcm#             if [ -x #TWM_PATH# ] ; then
#twmcm#                 echo Starting twm >>$HOME/.xwm.msgs
#twmcm#                 env > "$HOME"/Xrootenv.0
#twmcm#                 #TWM_PATH# >>$HOME/.xwm.msgs 2>&1 &
#twmcm#                 exec kterm -C -title Login -n Login -sb -geometry 80x25-0-0 -ls
#twmcm#             fi
#twmcm#             ;;

    esac
fi

# first, find an M4-enabled config file (such as 
# the one from AnotherLevel) for fvwm2 or fvwm95.
RCFILE=""
for tryfile in "$HOME/.fvwm2rc.m4" "/etc/X11/AnotherLevel/fvwm2rc.m4"; do
    if [ -f "$tryfile" ]; then
        RCFILE="$tryfile"
        break
    fi
done

# if it really exists, use it; if not, fvwm2 or fvwm95 will
# automagically look for a config file in the regular places.
if [ -n "$RCFILE" ]; then
    FVWMOPTIONS="-cmd 'FvwmM4 -debug $RCFILE'"
else
    FVWMOPTIONS=""
fi

# TheNextLevel is supposed to work
# with both fvwm95 and fvwm2
# (try fvwm95 first, then fvwm2).
for FVWMVER in 95 95-2 2; do
    if [ -n "$(type -path fvwm${FVWMVER})" ]; then
        env > "$HOME"/Xrootenv.0
        # if this works, we stop here
        eval "exec fvwm${FVWMVER} ${FVWMOPTIONS}" > "$HOME"/.FVWM${FVWMVER}-errors 2>&1
    fi
done

# gosh, neither fvwm95 nor fvwm2 is available;
# let's try regular fvwm (AnotherLevel doesn't work with fvwm1).
if [ -n "$(type -path fvwm)" ]; then
    # if this works, we stop here
    exec fvwm
fi

# No other window or session manager found.
# Use twm as a last resort.
xterm &
exec twm
