#!/bin/sh

CONFIG="$ADTTMP/rc.lua"
SUCCESS_FILE="$ADTTMP/success"
cat > "$CONFIG" <<DONE
local orig_error = error
local had_error = false
function error(...)
    had_error = true
    -- quit() only sets a flag and doesn't quit immediately :-(
    awesome.quit()
    orig_error(...)
end
-- Quit in 5 seconds, if everything worked
local t = timer({ timeout = 5 })
local connect = t.connect_signal or t.add_signal
connect(t, "timeout", function()
    -- Create the success file, everything worked fine
    if not had_error then
        io.open("$SUCCESS_FILE", "w")
    end
    awesome.quit()
end)
t:start()
dofile("/etc/xdg/awesome/rc.lua")
DONE

export TMPDIR="$ADTTMP/"
xvfb-run awesome -c "$CONFIG" &
PID=$!
sleep 10
kill -9 $PID 2> /dev/null
if [ -e "$SUCCESS_FILE" ]; then exit 0; else exit 1; fi
