#!/usr/bin/bash

# The pure GTK build of emacs is not supported on X11, so try to avoid
# using it there if there is another alternative.

preferred="$(readlink -f /usr/bin/emacs)"

if [[ $XDG_SESSION_TYPE == 'x11' ]]; then
    case "$preferred" in
    *-pgtk)
        for variant in gtk+x11 lucid; do
            if type "emacs-$variant" >/dev/null; then
                exec -a emacs "emacs-$variant" "$@"
            fi
        done
        ;;
    esac
fi

exec emacs "$@"
