aboutsummaryrefslogtreecommitdiff
blob: f418970e4664a111358d1f8f9b6df1d9f0962d6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/bin/bash
# Copyright 2008-2016 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2 or later

# Save output in a temporary file and display in case of error
logfile=$(mktemp ${TMPDIR:-/tmp}/emacs.log.XXXXXX)
trap "rm -f '${logfile}'" EXIT

# Start Emacs with a login shell wrapper to read the user's profile
exec -l "${SHELL}" -c "exec \"${EMACS}\" $*" </dev/null &>"${logfile}" &
pid=$!

# Wait for Emacs daemon to detach
for (( t=${EMACS_TIMEOUT:-30}; t > 0; t-- )); do
    sleep 1
    if ! kill -0 ${pid} 2>/dev/null; then
        wait ${pid}		# get exit status
        status=$?
        [[ ${status} -ne 0 || -n ${EMACS_DEBUG} ]] && cat "${logfile}"
        exit ${status}
    fi
done

cat "${logfile}"
echo "${0##*/}: timeout waiting for ${EMACS} to detach" >&2
kill ${pid} $(pgrep -P ${pid}) 2>/dev/null
exit 1