aboutsummaryrefslogtreecommitdiff
path: root/bin/s
blob: 960cb353466ff565a6e44b8a20814ca27053aefd (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
#!/usr/bin/mksh

fatal() {
  msg "$@" >&2
  exit 1
}

msg() {
  printf "$@"
}

sv_enable() {
  [ ! -e "${SVDIR}${1}" ] && fatal "service '${1}' not found.\n"
  [ -e "${SERVICEDIR}${1}" ] && fatal "service '${1}' already enabled.\n"
  ln -sfv "${SVDIR}${1}" "${SERVICEDIR}${1}"
}

sv_disable() {
  [ ! -e "${SVDIR}${1}" ] && fatal "service '${1}' not found."
  [ ! -e "${SERVICEDIR}${1}" ] && fatal "service '${1}' already disabled.\n"
  rm -rfv "${SERVICEDIR}${1}"
}

sv_list() {
  sv s "${SVDIR}"* 2>/dev/null
}

sv_deps() {
  for service in "$@"; do
    # TODO: check if service exists
    case $(cat "${SVDIR}${service}/supervise/stat") in
      run*)
        /home/duncan/bin/s check "${service}" >/dev/null || wait+=" $service"
        ;;
      down*)
        /home/duncan/bin/s u "${service}" >/dev/null && wait+=" $service"
        ;;
    esac
  done
  [ -z "$wait" ] || fatal "wait for dependencies:$wait.\n"
  exit 0
}

while getopts "u:w:v" opt; do
  case "$opt" in
    u) SVUSER="$OPTARG" ;;
    w) export SVWAIT="$OPTARG" ;;
  esac
done
shift $(($OPTIND - 1))

: ${SVUSER:=$USER}

case "$SVUSER" in
  root)
    SVDIR="/etc/sv/"
    SERVICEDIR="/var/service/"
    ;;
  *)
    HOMEDIR=$(getent passwd "$SVUSER" | cut -d':' -f6)
    SVDIR="${HOMEDIR}/sv/"
    SERVICEDIR="${HOMEDIR}/service/"
    ;;
esac

case "$1" in
  ls|list) sv_list "$2" ;;
  enable) sv_enable "$2" ;;
  disable) sv_disable "$2" ;;
  deps) shift 1; sv_deps "$@" ;;
  tree) shift 1; sv_tree "$SVDIR" ;;
  *) SVDIR="$SVDIR" sv "$@" ;;
esac