aboutsummaryrefslogtreecommitdiff
blob: 702de56cfe66a0c6069ff263eac9e565083fd6db (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/sbin/runscript
# Copyright 2008-2014 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2 or later

: ${EMACS:=/usr/bin/emacs}
: ${EMACSCLIENT:=/usr/bin/emacsclient}
: ${EMACS_OPTS:=--daemon}
: ${EMACS_SHELL=/bin/bash}
: ${EMACS_START:=/usr/libexec/emacs/emacs-wrapper.sh}
: ${EMACS_TIMEOUT:=30}
USER=${RC_SVCNAME##*.}
PIDFILE_DIR=/var/run/emacs/${USER}
PIDFILE=${PIDFILE_DIR}/emacs.pid

description="Start an Emacs server running in the background"

depend() {
    need localmount
    after bootmisc dbus
}

checkconfig() {
    if [ "${RC_VERSION:-0}" = "0" ]; then
        eerror "This script cannot be used for baselayout-1."
        return 1
    fi

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

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

    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

    checkpath -d --owner 0 --mode 0755 "${PIDFILE_DIR%/*}"
    checkpath -d --owner "${USER}" --mode 0755 "${PIDFILE_DIR}"
}

start() {
    local home
    checkconfig || return 1

    eval home="~${USER}"

    SHELL=${EMACS_SHELL:-$(awk -F: "\$1 == \"${USER}\" { print \$7 }" \
	/etc/passwd)}
    export SHELL EMACS EMACS_TIMEOUT EMACS_DEBUG

    ebegin "Starting Emacs daemon for user ${USER}"
    start-stop-daemon --start \
	--user "${USER}" --pidfile "${PIDFILE}" --chdir "${home}" \
	--exec "${EMACS_START}" -- ${EMACS_OPTS}
    eend $?
}

stop() {
    # Optionally, call a custom script before stopping.
    if [ "${EMACS_STOP}" ] && [ -x "${EMACS_STOP}" ]; then
	export USER PIDFILE EMACS EMACS_TIMEOUT EMACS_DEBUG
	export EMACSCLIENT EMACSCLIENT_OPTS
	ebegin "Calling Emacs stop script for ${USER}"
	"${EMACS_STOP}"
	eend $? || return
    fi

    ebegin "Stopping Emacs daemon for user ${USER}"
    start-stop-daemon --stop --retry "TERM/${EMACS_TIMEOUT}/KILL/5" \
	--user "${USER}" --pidfile "${PIDFILE}" --exec "${EMACS}"
    eend $?
}