#!/bin/sh

# This is the list of supported window manager binaries
WMS=("metacity" "kwin" "xfwm4" "openbox" "marco")

# Get the application binary to start and remove it from
# the argument list
BINARY=$1
shift

for WM in ${WMS[@]}; do
    FILE=$(which $WM 2>/dev/null)
    FOUND=$?

    if [ $FOUND -eq 0 -a -x "$FILE" ]; then
        # start window manager
        echo "Running $FOUND window manager ($FILE)" | logger
        "$FILE" "$@" &
        pid=$!

        # start the application
        echo "Running $BINARY" | logger
        $BINARY
        res=$?

        # stop window manager
        ps -p $pid >/dev/null && kill $pid

        # return result
        exit $res
    fi
done

# No known window manager found
echo "No supported window manager found!" | logger
exit 1
