#!/sbin/runscript # Copyright 2008-2009 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 # $Id$ : ${EMACS:=/usr/bin/emacs} : ${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 [ "${USER}" = "${RC_SVCNAME}" ]; then eerror "You have to create an init script for each user:" eerror "ln -s emacs /etc/init.d/emacs." 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 ebegin "Starting Emacs daemon for user ${USER}" # This was tested with start-stop-daemon from OpenRC; don't expect # it to work with other implementations. start-stop-daemon --start \ --user "${USER}" --chdir "${home}" --pidfile "${PIDFILE}" \ --env EMACS="${EMACS}" --env EMACS_TIMEOUT="${EMACS_TIMEOUT}" \ --exec "${EMACS_START}" -- ${EMACS_OPTS} eend $? } stop() { # Optionally, call a custom script before stopping. if [ "${EMACS_STOP}" ] && [ -x "${EMACS_STOP}" ]; then "${EMACS_STOP}" || return fi ebegin "Stopping Emacs daemon for user ${USER}" start-stop-daemon --stop --user "${USER}" \ --pidfile "${PIDFILE}" --exec "${EMACS}" eend $? }