#!/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.        #
#                                                                      #
# feel free to edit this file to suit your needs.                      #
########################################################################

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

# first, find an M4-enabled config file (such as 
# the one from TheNextLevel) for fvwm2 or fvwm95.
RCFILE=""
for tryfile in "$HOME/.fvwm2rc.m4" "/etc/X11/TheNextLevel/.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="-f '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 (TheNextLevel doesn't
# work with fvwm1).
if [ -n "$(type -path fvwm)" ]; then
    # if this works, we stop here
    exec fvwm
fi

# wow, fvwm isn't here either ... 
# use twm as a last resort.
xterm &
exec twm
