#!/bin/bash
# Copyright (C) 1999 - 2004 Red Hat, Inc. All rights reserved. This
# copyrighted material is made available to anyone wishing to use, modify,
# copy, or redistribute it subject to the terms and conditions of the
# GNU General Public License version 2.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

#
# RunWM - this is a script used to launch window managers
#

WM_CHOICE=
KNOW_WM="no"

if [ -n "$1" ] ; then
    DESK=`echo $1 |tr A-Z a-z`
    case $DESK in
    --windowmaker | --wmaker | \
    --mwm )
	KNOW_WM="yes"
	WM_CHOICE=$1
	shift
	;;
    --help)
	echo "Usage: $0 [window_manager] where window_manager can be"
	echo "  --WindowMaker   start WindowMaker"
	echo
	exit 0
	;;
    *)
	KNOW_WM="no"
	WM_CHOICE=
	;;
    esac
fi

# Now, some braindead window managers don't know how to pass arguments to another
# wm when doing a Restart... (fvwm2 is one of them)

if [ -z "$WM_CHOICE" ] ; then
    #try to see if we are called funny names...
    NAME=`echo $0 |tr A-Z a-z`
    case "$NAME" in 
	*runwm.mwm)
	    WM_CHOICE="--FvwmMWM"
	    KNOW_WM="yes"
	    ;;
	*runwm.windowmaker)
	    WM_CHOICE="--WindowMaker"
	    KNOW_WM="yes"
	    ;;
    esac
fi

if [ "$KNOW_WM" = "no" ] ; then
    if [ -f $HOME/.wm_style ] ; then
	WM_CHOICE="--$(cat $HOME/.wm_style)"
    else
	WM_CHOICE="--Fvwm95"
    fi
fi

############### FUNCTIONS FOR WINDOW MANAGERS ############################
function Start_WindowMaker () {
  if [ -x /usr/bin/wmaker -a \
       -d /usr/share/WindowMaker -a \
       -x /usr/bin/wmaker.inst ] ; then
    local WMGLIBALDIR
    local GSDIR
    WMGLOBALDIR="/usr/share/WindowMaker"
    GSDIR=$HOME/GNUstep
    need_installation=""
    [ -d $GSDIR/Defaults ] || need_installation="yes"
    [ -d $GSDIR/Library/WindowMaker ] || need_installation="yes"
    if [ -n "$need_installation" ] ; then
	/usr/bin/wmaker.inst --batch
    fi
    env > "$HOME"/Xrootenv.0
    eval "exec /usr/bin/wmaker" > "$HOME"/.WindowMaker-errors 2>&1
  else
    cat <<EOF
 WARNING: You selected WindowMaker as your window manager, but your
 installation of WindowMaker does not appear to be functional. Either
 /usr/bin/wmaker was not found or /usr/share/WindowMaker directory
 is missing.
EOF
  fi
    echo "RESUMING with Fvwm2 (95 look)" >&2
    Start_Fvwm95
}

#
###
#

# there is not really any difference between this and the previous one
# but we stay prepared... :-)
function Start_MWM () {
    Start_Fvwm95 $*
}

##############################################################################

################### THE REAL WORK IS DNE HERE ################################
case "$WM_CHOICE" in
    --windowmaker | --wmaker | --WindowMaker)
	echo "WindowMaker" > $HOME/.wm_style
	Start_WindowMaker
	exit 0;
	;;
    --MWM | --Mwm | --mwm | \
    *)
	echo "This window manager ($WM_CHOICE) is unknown to me."
	$0 --help
	exit -1
	;;
esac

if [ "$KNOWN_WM" = "yes" ] ; then
cat <<EOF

    The window manager you have requested ($WM_CHOICE) could not be started.
EOF
else
cat <<EOF

    The default window manager ($WM_CHOICE) could not be started. You have
    selected this window manager in your $HOME/.wm_style file or it was
    selected as the default window manager by the system administrator.
EOF
fi
cat <<EOF
    Check your settings and try again.
EOF

exit -1;
################# This is it #########################
