#!/usr/bin/env bash
#
# sc - systemctl wrapper
#
# ---
# source: https://ruderich.org/simon/gitweb/

# shellcheck shell=bash

set -eu

args=()
found=
for arg; do
  # this heuristic replaces first occurrence of a shortcut with its expansion.
  # this is not perfect (e.g. `systemctl status s` would result in `systemctl
  # status status`) but works even when options are used and doesn't require us
  # to duplicate systemctl's parsing of command line arguments.
  if [ -z "${found}" ]; then
    found=1
    case "${arg}" in
      c) arg="cat" ;;
      d) arg="disable" ;;
      e) arg="enable" ;;
      k) arg="kill" ;;
      m) arg="mask" ;;
      r) arg="restart" ;;
      rl) arg="reload" ;;
      s) arg="status" ;;
      sa) arg="start" ;;
      so) arg="stop" ;;
      u) arg="unmask" ;;
      # no match, try it again for the next parameter.
      *) found= ;;
    esac
  fi
  args+=("${arg}")
done

# empty $args causes an error in bash
set +u
exec systemctl "${args[@]}" 2>&1
