diff options
author | P. J. McDermott <pj@pehjota.net> | 2018-06-01 15:01:07 (EDT) |
---|---|---|
committer | P. J. McDermott <pj@pehjota.net> | 2018-06-03 23:40:26 (EDT) |
commit | 573a0a5ff1489d0ae3c582b844a80b9f693b4c71 (patch) | |
tree | d391fbd1a02ef0c82c5c16300d939de02d519f72 | |
parent | 03b4f6ebd3c9e1f1bb6edb09bce67071417ed526 (diff) | |
download | homerc-573a0a5ff1489d0ae3c582b844a80b9f693b4c71.zip homerc-573a0a5ff1489d0ae3c582b844a80b9f693b4c71.tar.gz homerc-573a0a5ff1489d0ae3c582b844a80b9f693b4c71.tar.bz2 |
bin/svc: New script
-rw-r--r-- | .gitignore.d/homerc | 1 | ||||
-rwxr-xr-x | bin/svc | 134 |
2 files changed, 135 insertions, 0 deletions
diff --git a/.gitignore.d/homerc b/.gitignore.d/homerc index 66206ac..b674cd2 100644 --- a/.gitignore.d/homerc +++ b/.gitignore.d/homerc @@ -6,6 +6,7 @@ !/.gitignore.d/homerc !/bin/ !/bin/rc +!/bin/svc # Exclude swap and backup files *.s[a-w]? @@ -0,0 +1,134 @@ +#!/bin/sh + +set -eu + +XDG_DATA_HOME= +XDG_CONFIG_HOME= +XDG_DATA_DIRS= +XDG_CONFIG_DIRS= +XDG_CACHE_HOME= +PIDDIR= +SERVICES= + +# TODO: "[....] service" output if attached to a tty + +load_service() +{ + local service="${1}" + shift 1 + + XDG_DATA_HOME="${XDG_DATA_HOME:-${HOME}/.local/share}" + XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-${HOME}/.config}" + XDG_DATA_DIRS="${XDG_DATA_DIRS:-/usr/local/share/:/usr/share/}" + XDG_CONFIG_DIRS="${XDG_CONFIG_DIRS:-/etc/xdg}" + XDG_CACHE_HOME="${XDG_CACHE_HOME:-${HOME}/.cache}" + PIDDIR="${XDG_CACHE_HOME}/homerc" + + # Default functions + stop() { :; } + restart() { stop; start; } + status() { :; } + + # TODO: Errors + . "${XDG_CONFIG_HOME}/homerc/${service}" +} + +act() +{ + local action="${1}" + shift 1 + + "${action}" + return ${?} +} + +match_session() +{ + local needle="${1}" + local haystack="${2}" + shift 2 + local hay= + + IFS=',' + for hay in ${haystack}; do + unset IFS + case "${hay}" in + '') continue;; + "${needle}" | "${needle}:"*) return 0;; + esac + done + unset IFS + + return 1 +} + +chg_sessions() +{ + local old_sessions="${1}" + local new_sessions="${2}" + shift 2 + local is_running= + local should_run= + + is_running=false + should_run=false + for session in ${SESSIONS}; do + if match_session "${session}" "${old_sessions}"; then + is_running=true + fi + if match_session "${session}" "${new_sessions}"; then + should_run=true + fi + done + + if ${is_running} && ! ${should_run}; then + act 'stop' + return ${?} + fi + if ! ${is_running} && ${should_run}; then + act 'start' + return ${?} + fi + + # No change. + return 0 +} + +usage() +{ + printf 'Usage: %s <service> {start|stop|restart|status}\n' "${progname}" + + return 0 +} + +main() +{ + local service= + local action= + + if [ ${#} -ne 2 ]; then + usage 1>&2 + return 1 + fi + service="${1}" + action="${2}" + + case "${action}" in + 'start' | 'stop' | 'restart' | 'status') + load_service "${service}" || return 1 + act "${action}" + ;; + *';'*) + load_service "${service}" || return 1 + chg_sessions "${action%%;*}" "${action#*;}" + ;; + *) + usage 1>&2 + return 1 + ;; + esac + + return 0 +} + +main "${@}" |