#!/bin/bash
#
# Launch a GNU Emacs
#
# The environment variable EMACS_TOOLKIT is used to determine
# the prefered GUI. Possible values/types of EMACS_TOOLKIT are
#
#   nox -- for pure console based GNU Emacs 
#   gtk -- for full GTK2/3  based GNU Emacs
#   x11 -- for full LUCID   based GNU Emacs (used Xaw3d)
#
# Should work but remember history
#   bnc#345669 -- Emacs doesn't un-maximize in KDE/KWin
#   bnc#342385 -- Emacs doesn't keep the iconic information in KDE/KWin
#
# if test -z "$EMACS_TOOLKIT" ; then
#     EMACS_TOOLKIT=gtk
#     KDE_FULL_SESSION=$(xprop -root KDE_FULL_SESSION 2>/dev/null)
#     case "$KDE_FULL_SESSION" in
#     *true*) EMACS_TOOLKIT=x11
#     esac
# fi
#
: ${EMACS_TOOLKIT:=gtk}
if test "$EMACS_TOOLKIT" = gtk; then
    # Currently (2013/05/24) the parser of the GNOME libs
    # are broken that is it is not independent from locale
    LC_NUMERIC=POSIX
    export LC_NUMERIC
fi
arg0=$0
argv=("$@")
if   test -e ${arg0}-${EMACS_TOOLKIT}
then
    set --   ${arg0}-${EMACS_TOOLKIT}
elif test -e ${arg0}-x11
then
    set --   ${arg0}-x11
elif test -e ${arg0}-nox
then
    set --   ${arg0}-nox
else
    echo "no emacs binary found"
    exit 1
fi
dbusdaemon=$(type -p dbus-daemon 2>/dev/null)
dbuslaunch=$(type -p dbus-launch 2>/dev/null)
dbusession=$(type -p dbus-run-session 2>/dev/null)
#
# Now check for valid dbus, e.g. after su/sudo/slogin
#
if test -n "$dbusdaemon" ; then
    #
    # Currently (2013/05/24) the option --autolaunch for scanning for an
    # already existing session is an internal option of dbus-launch(1).
    #
    if test -s /var/lib/dbus/machine-id ; then
	read -t1 mid < /var/lib/dbus/machine-id
    elif test -s /etc/machine-id ; then
	read -t1 mid < /etc/machine-id
    else
	mid=
    fi
    if test -n "$DBUS_SESSION_BUS_ADDRESS" ; then
	# Determine dbus identifier
	for guid in ${DBUS_SESSION_BUS_ADDRESS//,/ } ; do
	    case "$guid" in
	    guid=*) break
	    esac
	done
	# Check if dbus-daemon is active
	dpid=
	for suid in "${HOME}/.dbus/session-bus/"${mid}* ; do
	    test -e "$suid" || break
	    grep -q $guid "$suid" || continue
	    dpid=$(grep -E '^DBUS_SESSION_BUS_PID=[[:digit:]]+' "$suid")
	    test /proc/${dpid#*=}/exe -ef $dbusdaemon && continue
	    unset DBUS_SESSION_BUS_ADDRESS
	    break
	done
	test -n "$dpid" || unset DBUS_SESSION_BUS_ADDRESS
    fi
    # Find a valid dbus-daemon if active
    if test -z "$DBUS_SESSION_BUS_ADDRESS" ; then
	for suid in "${HOME}/.dbus/session-bus/"${mid}* ; do
	    test -e "$suid" || break
	    dpid=$(grep -E '^DBUS_SESSION_BUS_PID=[[:digit:]]+' "$suid")
	    test /proc/${dpid#*=}/exe -ef $dbusdaemon || continue
	    dadd=$(grep -E '^DBUS_SESSION_BUS_ADDRESS=' "$suid")
	    DBUS_SESSION_BUS_ADDRESS=${dadd#*=}
	    export DBUS_SESSION_BUS_ADDRESS
	done
    fi
    unset mid guid suid dadd
    # Oops ... no dbus-daemon then launch a new session
    if test -z "$DBUS_SESSION_BUS_ADDRESS" ; then
	if test -z "$dbusession" -a -n "$dbuslaunch" ; then
	    set -- $dbuslaunch --sh-syntax --close-stderr --exit-with-session ${1+"$@"}
	    arg0=$dbuslaunch
	elif test -n "$dbusession" ; then
	    set -- $dbusession -- ${1+"$@"}
	    arg0=$dbusession
	else
	    arg0=emacs
	fi
    fi
    unset dbuslaunch dbusdaemon
fi
exec -a $arg0 ${1+"$@"} "${argv[@]}"
