summaryrefslogtreecommitdiff
blob: 63d5a1b46d02d9b3d4c38d11d539e1d0ddce705e (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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/sbin/runscript
# $Header: $
# Copyright 2008 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

EMACS="/usr/bin/emacs"
USER="${RC_SVCNAME#*.}"
PIDFILE_DIR="/var/run/emacs-daemon/${USER}"
PIDFILE="${PIDFILE_DIR}/emacs.pid"

checkconfig() {
    if [ "${USER}" = "${RC_SVCNAME}" ]; then
	eerror "You have to create an init script for each user:"
	eerror "ln -s emacs-daemon /etc/init.d/emacs-daemon.<user>"
	return 1
    fi

    if ! id -u "${USER}" >/dev/null; then
	eerror "${USER}: No such user"
	return 1
    fi

    # Executing Emacs here also ensures a warm cache, which later helps
    # to create the pid file in a timely manner (within 0.5 seconds).
    local has_daemon=$(${EMACS} -batch -q --no-site-file \
	--eval "(princ (fboundp 'daemonp))")
    if [ "${has_daemon}" != t ]; then
	eerror "${EMACS} does not support running as a daemon"
	return 1
    fi

    if [ ! -d "${PIDFILE_DIR}" ]; then
	ewarn "${PIDFILE_DIR} does not exist - creating it"
	mkdir -p "${PIDFILE_DIR}"
	chown "${USER}" "${PIDFILE_DIR}"
    fi

    return 0
}

start() {
    local home
    checkconfig || return 1

    ebegin "Starting Emacs daemon for ${USER}"
    eval home="~${USER}"
    start-stop-daemon --start --user "${USER}" --chdir "${home}" \
	--quiet --pidfile "${PIDFILE}" --exec "${EMACS}" -- --daemon
    eend $?
}

stop() {
    ebegin "Stopping Emacs daemon for ${USER}"
    start-stop-daemon --stop --user "${USER}" \
	--pidfile "${PIDFILE}" --exec "${EMACS}"
    eend $?
}