#!/bin/bash


if [[ $# -lt 1 ]] ; then
    echo "usage: $0 [program-name]"
    exit 1
fi

if ! [ -e /etc/qubes-release ] && ! [ -e /var/run/qubes-service/guivm ]; then
    echo "not dom0 or guivm, exiting"
    exit 0
fi

"$@"

exit_code=$?

if [[ ${exit_code} -eq 0 ]] ; then
    echo "exiting with 0"
    exit 0
else
    if xhost >/dev/null ; then
    # it was a genuine crash
        echo "exiting with 1"
        exit ${exit_code}
    else
        # XServer is down
        echo "exiting with 0"
        exit 0
    fi
fi
