#!/bin/bash

###############################################
## x session for instantOS, use with lightdm ##
###############################################

# used for shutdown
touch ~/.instantosrunning

# loop so crashing instantwm doesn't end the x session
while test -e ~/.instantosrunning; do

    # allow changing wm on the fly
    if [ -e /tmp/wmoverride ]; then
        if command -v "$(cat /tmp/wmoverride)" &>/dev/null; then
            "$(cat /tmp/wmoverride)" 2>~/.instantos.log &
            WMPID="$!"
            echo "$WMPID" >/tmp/wmpid
            while kill -0 "$WMPID"; do
                sleep 1
            done
        fi
        rm /tmp/wmoverride
    else
        # Log stderror or stdout to a file
        if ! [ -e ~/.instantdebug ]; then
            instantwm 2>~/.instantos.log
        else
            instantwm &>~/.instantos.log
        fi

    fi
done
