#! /bin/sh

#################################################################
# This is a "simple" script which is intended to be run by rpm
# as a post install function to look at the system where wdm is
# being installed and create Xclients and wdm-config files which
# reflect the window managers available on that system.

#    This software is Copyright (C) 1998 by Gene Czarcinski.
# This software falls under the GNU Public License. Please read
#              the COPYING file for more information
#
# Too many modification by
# MATSUBAYASHI 'Shaolin' Kohji <shaolin@rhythmaning.org>
#################################################################


save_ifs="$IFS"
IFS=:
path=$PATH:/usr/local/bin:/usr/bin:/usr/X11R6/bin:/usr/kde/bin:/opt/kde/bin


#
# Setting up Input Method Selection
#

IMLIST=""

# entry for Canna
    if [ \( -d /usr/lib/canna \) -a \
         \( -x /usr/sbin/cannaserver \) ] ; then
      IMLIST=Canna
    fi

# entry for FreeWnn
    if [ \( -f /etc/FreeWnn/ja/wnnenvrc \) -a \
         \( -x /usr/bin/jserver \) ] ; then
      if [ "${IMLIST}" != "" ] ; then
          IMLIST=${IMLIST}:FreeWnn
      else
          IMLIST=FreeWnn
      fi
    fi

# entry for Wnn7
    if [ \( \( -f /etc/wnn7/ja_JP/jserverrc \) -o \
            \( -f /usr/lib/wnn7/ja_JP/jserverrc \) \) -a \
         \( \( -x /usr/sbin/jserver \) -o \
            \( -x /usr/local/bin/jserver \) -o \
            \( -x /usr/local/bin/Wnn7/jserver \) \) ] ; then
      if [ "${IMLIST}" != "" ] ; then
          IMLIST=${IMLIST}:Wnn7:Wnn7xwnmo
      else
          IMLIST=Wnn7:Wnn7xwnmo
      fi
    fi

# entry for Wnn6
    if [ \( \( -f /etc/wnn6/ja_JP/wnnenvrc \) -o \
            \( -f /usr/local/lib/wnn6/ja_JP/wnnenvrc \) \) -a \
         \( \( -x /usr/sbin/jserver \) -o \
            \( -x /usr/local/bin/jserver \) -o \
            \( -x /usr/local/bin/Wnn6/jserver \) \) ] ; then
      if [ "${IMLIST}" != "" ] ; then
          IMLIST=${IMLIST}:Wnn6:Wnn6xwnmo
      else
          IMLIST=Wnn6:Wnn6xwnmo
      fi
    fi

# entry for VJE-Delta
    if [ \( \( -d /usr/lib/vje \) -o \
            \( -d /usr/local/vje30 \) \) -a \
         \( \( -x /usr/sbin/vjed \) -o \
	    \( -x /usr/X11R6/bin/vjed \) \) -a \
	 \( \( -f /var/lock/subsys/vjed \) -o \
	    \( ! -z "`ps aux | grep vjed 2>/dev/null`" \) \) ] ; then
      if [ "${IMLIST}" != "" ] ; then
          IMLIST=${IMLIST}:VJE-Delta
      else
          IMLIST=VJE-Delta
      fi
    fi

# entry for ATOK X
    if [ -x /usr/lib/im/locale/ja/atokserver/atokx_client ] ; then
      if [ "${IMLIST}" != "" ] ; then
          IMLIST=${IMLIST}:ATOKX
      else
          IMLIST=ATOKX
      fi
    fi 

#
# Setting up Window Manager Selection
#

WMPATH=/etc/X11/wdm/wm.d
[ -d ${WMPATH} ] || mkdir -p ${WMPATH}
rm -rf ${WMPATH}/*


setupWM(){
    WM=$1
    shift
    CMD="`which $1 2>/dev/null`"
    shift
    if [ -n "$CMD" ]; then
        cat <<EOF > ${WMPATH}/${WM}
#!/bin/bash

echo Starting ${WM} ... >> \${HOME}/.xwm.msgs
env > \${HOME}/Xrootenv.0
exec ${CMD} $@ >> \${HOME}/.xwm.msgs 2>&1
EOF
        chmod 755 ${WMPATH}/${WM}
        return 0
    fi
    return -1
}


WMLIST=""

# entry for WindowMaker
rc_word=RunWM
WMAKER_PATH=""
    for rc_dir in $path; do
      if test -x $rc_dir/wmaker; then
        WMAKER_PATH=$rc_dir/$rc_word
	WMLIST=WindowMaker
        setupWM WindowMaker ${WMAKER_PATH} --WindowMaker
	break
      fi
    done

# entry for GNOME
rc_word=gnome-session
GNOME_PATH=""
    for rc_dir in $path; do
      if test -x $rc_dir/$rc_word; then
        GNOME_PATH=$rc_dir/$rc_word
	WMLIST="$WMLIST:GNOME"
        setupWM GNOME ${GNOME_PATH}
	break
      fi
    done

# entry for KDE
rc_word=startkde
KDE_PATH=""
    for rc_dir in $path; do
      if test -x $rc_dir/$rc_word; then
        KDE_PATH=$rc_dir/$rc_word
	WMLIST="$WMLIST:KDE"
        setupWM KDE ${KDE_PATH}
	break
      fi
    done

# entry for AfterStep
rc_word=RunWM
AFTERSTEP_PATH=""
    for rc_dir in $path; do
      if test -x $rc_dir/afterstep; then
        AFTERSTEP_PATH=$rc_dir/$rc_word
	WMLIST="$WMLIST:AfterStep"
        setupWM AfterStep ${AFTERSTEP_PATH} --AfterStep
	break
      fi
    done

# entry for Enlightenment
rc_word=enlightenment
E_PATH=""
    for rc_dir in $path; do
      if test -x $rc_dir/$rc_word; then
        E_PATH=$rc_dir/$rc_word
	WMLIST="$WMLIST:Enlightenment"
        cat <<EOF > ${WMPATH}/Enlightenment
#!/bin/bash

echo Starting Enlightenment ... >> \${HOME}/.xwm.msgs
env > \${HOME}/Xrootenv.0
${E_PATH} >> \${HOME}/.xwm.msgs 2>&1 &
exec kterm -C -title Login -n Login -sb -geometry 80x25-0-0 -ls
EOF
        chmod 755 ${WMPATH}/Enlightenment
	break
      fi
    done

# entry for BlackBox
rc_word=blackbox
BLACKBOX_PATH=""
    for rc_dir in $path; do
      if test -x $rc_dir/$rc_word; then
        BLACKBOX_PATH=$rc_dir/$rc_word
	WMLIST="$WMLIST:BlackBox"
        setupWM BlackBox ${BLACKBOX_PATH}
	break
      fi
    done

# entry for sawfish
rc_word=sawfish
SAWMILL_PATH=""
    for rc_dir in $path; do
      if test -x $rc_dir/$rc_word; then
        SAWMILL_PATH=$rc_dir/$rc_word
	WMLIST="$WMLIST:sawfish"
        cat <<EOF > ${WMPATH}/sawfish
#!/bin/bash

echo Starting sawfish ... >> \${HOME}/.xwm.msgs
env > \${HOME}/Xrootenv.0
${SAWMILL_PATH} >> \${HOME}/.xwm.msgs 2>&1 &
exec kterm -C -title Login -n Login -sb -geometry 80x25-0-0 -ls
EOF
        chmod 755 ${WMPATH}/sawfish
	break
      fi
    done

# entry for icewm
rc_word=icewm
ICEWM_PATH=""
    for rc_dir in $path; do
      if test -x $rc_dir/$rc_word; then
        ICEWM_PATH=$rc_dir/$rc_word
	WMLIST="$WMLIST:icewm"
        setupWM icewm ${ICEWM_PATH}
	break
      fi
    done

# entry for qvwm
rc_word=qvwm
QVWM_PATH=""
    for rc_dir in $path; do
      if test -x $rc_dir/$rc_word; then
        QVWM_PATH=$rc_dir/$rc_word
        WMLIST="$WMLIST:qvwm"
        setupWM qvwm ${QVWM_PATH}
        break
      fi
    done

# entry for fvwm2
rc_word=fvwm2
FVWM2_PATH=""
    for rc_dir in $path; do
      if test -x $rc_dir/$rc_word; then
        FVWM2_PATH=$rc_dir/$rc_word
        WMLIST="$WMLIST:fvwm2"
        setupWM fvwm2 ${FVWM2_PATH}
        break
      fi
    done

# entry for twm
rc_word=twm
TWM_PATH=""
    for rc_dir in $path; do
      if test -x $rc_dir/$rc_word; then
        TWM_PATH=$rc_dir/$rc_word
        WMLIST="$WMLIST:twm"
        cat <<EOF > ${WMPATH}/twm
#!/bin/bash

echo Starting twm ... >> \${HOME}/.xwm.msgs
env > \${HOME}/Xrootenv.0
${TWM_PATH} >> \${HOME}/.xwm.msgs 2>&1 &
exec kterm -C -title Login -n Login -sb -geometry 80x25-0-0 -ls
EOF
        chmod 755 ${WMPATH}/twm
        break
      fi
    done


# update wdm-config
	rm -f /etc/X11/wdm/wdm-config
	sed \
		-e "s:#configdir#:/etc/X11/wdm:" \
		-e "s:#bindir#:/usr/X11R6/bin:" \
		-e "s:#SHUTDOWN#:/usr/bin/shutdown:" \
		-e "s:#logdir#:/var/log:" \
		-e "s:#rundir#:/var/run:" \
		-e "s:#pixmapfile#:vinelogowdm.xpm:" \
		-e "s:#pixmapcomment#::" \
		-e "s/#wmlist#/${WMLIST}/" \
		-e "s/#imlist#/${IMLIST}/" \
		/etc/X11/wdm/wdm-config.in \
		> /etc/X11/wdm/wdm-config
	chmod 600 /etc/X11/wdm/wdm-config

