#!/bin/sh
#
# s - init system adapter
#
# usage: s caddy restart
#
# ---
# see: https://wiki.cyberia.club/hypha/infrastructure_and_operations/systemd_openrc_cheatsheet

init="${S_INIT:-}"
if [ -z "${init}" ]; then
  # shellcheck disable=SC2249
  case "$(ps 1)" in
    *systemd*)
      init="systemd"
      ;;
    */sbin/init*)
      if stat /sbin/init | grep -q busybox >/dev/null; then
        init="busybox"
      elif stat /sbin/init | grep -q systemd >/dev/null; then
        init="systemd"
      fi
      ;;
  esac
fi
if [ -z "${init}" ]; then
  echo "your init system is unknown to me"
  exit 1
fi

service="$2"
action="$1"
if [ -z "${service}" ] || [ -z "${action}" ]; then
  echo "the first two arguments can't be empty"
  exit 1
fi

# in case the init system specified is docker compose
COMPOSE_ROOT="${COMPOSE_ROOT:-/opt/compose}"

case "${action}" in
  echo)
    echo "${init} ${action} ${service}"
    ;;
  edit)
    if [ "${init}" = "busybox" ]; then
      ${EDITOR:-vi} "/etc/init.d/${service}"
    elif [ "${init}" = "compose" ]; then
      ${EDITOR:-vi} "${COMPOSE_ROOT}/${service}/docker-compose.yml"
    elif [ "${init}" = "systemd" ]; then
      # shellcheck disable=SC2312
      ${EDITOR:-vi} "$(systemctl show --property=FragmentPath "${service}" | sed -ne 's/.*=//p')"
    fi
    ;;
  logs)
    if [ "${init}" = "busybox" ]; then
      # shellcheck disable=SC2312
      less "$(grep 'log=' "/etc/init.d/${service}" | sed -ne 's/.*=//p')"
    elif [ "${init}" = "compose" ]; then
      cd "${COMPOSE_ROOT}/${service}" && docker compose logs -f
    elif [ "${init}" = "systemd" ]; then
      sudo journalctl -feu "${service}"
    fi
    ;;
  off)
    if [ "${init}" = "busybox" ]; then
      sudo rc-update del "${service}"
    elif [ "${init}" = "compose" ]; then
      echo "unsupported action 'off' with init type 'compose'"
    elif [ "${init}" = "systemd" ]; then
      sudo systemctl disable "${service}"
    fi
    ;;
  on)
    if [ "${init}" = "busybox" ]; then
      sudo rc-update add "${service}"
    elif [ "${init}" = "compose" ]; then
      echo "unsupported action 'on' with init type 'compose'"
    elif [ "${init}" = "systemd" ]; then
      sudo systemctl enable "${service}"
    fi
    ;;
  restart)
    if [ "${init}" = "busybox" ]; then
      sudo service "${service}" restart
    elif [ "${init}" = "compose" ]; then
      cd "${COMPOSE_ROOT}/${service}" && docker compose restart
    elif [ "${init}" = "systemd" ]; then
      sudo systemctl restart "${service}"
    fi
    ;;
  start)
    if [ "${init}" = "busybox" ]; then
      sudo service "${service}" start
    elif [ "${init}" = "compose" ]; then
      cd "${COMPOSE_ROOT}/${service}" && docker compose up -d
    elif [ "${init}" = "systemd" ]; then
      sudo systemctl start "${service}"
    fi
    ;;
  status)
    if [ "${init}" = "busybox" ]; then
      sudo service "${service}" status
    elif [ "${init}" = "compose" ]; then
      cd "${COMPOSE_ROOT}/${service}" && docker compose ps
    elif [ "${init}" = "systemd" ]; then
      sudo systemctl status "${service}"
    fi
    ;;
  stop)
    if [ "${init}" = "busybox" ]; then
      sudo service "${service}" stop
    elif [ "${init}" = "compose" ]; then
      cd "${COMPOSE_ROOT}/${service}" && docker compose stop
    elif [ "${init}" = "systemd" ]; then
      sudo systemctl stop "${service}"
    fi
    ;;
  *)
    echo "unknown action '${action}'"
    exit 1
    ;;
esac
