#!/bin/bash
#
# sc - small wrapper for systemctl which provides some shortcuts.

# Copyright (C) 2015-2019 Simon Ruderich
# shellcheck shell=bash

set -eu

args=( )
found=
for arg; do
    # This heuristic replaces the 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, maybe a bug in bash
set +u
exec systemctl "${args[@]}" 2>&1
