#!/bin/sh -e

runit_live() {
  if [ ! -L /run/runit/service ]; then
    echo >&2 "  Skipped: Current root is not booted."
    exit 0
  fi
}

svc_help(){
    echo "	==> Start/stop/restart a service:"
    echo "	sv <start/stop/restart> <service>"
}

svc_add_help(){
    echo "	==> Add a service:"
    echo "	ln -s ../../sv/<service> /run/runit/service/"
    svc_help
}

svc_del_help(){
    echo "	==> Remove a service:"
    echo "	unlink /run/runit/service/<service>"
    svc_help
}

each_conf() {
  while read -r f; do
    "$@" "/$f"
  done
}

reload_dbus() {
    dbus-send --print-reply --system --type=method_call \
            --dest=org.freedesktop.DBus \
            / org.freedesktop.DBus.ReloadConfig > /dev/null
}

op="$1"; shift

case $op in
  sysctl)   runit_live; each_conf /usr/bin/sysctl -q -p ;;
  dbus_reload) runit_live; reload_dbus ;;
    # For use by other packages
  restart)   runit_live; /usr/bin/sv restart "$@" ;;
  add)      svc_add_help ;;
  del)      svc_del_help ;;
  *) echo >&2 "  Invalid operation '$op'"; exit 1 ;;
esac

exit 0
