#!/usr/bin/env bash base_dir="$HOME/.config/systemd/user/" _filename () { echo "autostart-$1.service" } _systemctl () { systemctl --user "${@:2}" "$(_filename "$1")" } _echo_table () { printf "Unit\tFilename\tEnabled\n" for unit in "$@" do printf "%s\t%s\t%s\n" \ "$unit" \ "$(_filename "$unit")" \ "$(_systemctl "$unit" is-enabled)" done } _autostart_manage_add () { cat <"$base_dir$(_filename "$1")" [Unit] Description=$1 [Service] ExecStart=$2 EOF } _autostart_manage_list () { systemctl --user list-unit-files \ | grep "autostart-" \ | awk '{print $1}' \ | sed -e 's/^autostart-//' -e 's/.service$//' } _autostart_manage_info () { mapfile -t autostart_units < <( _autostart_manage_list ) _echo_table "${autostart_units[@]}" | column -t -s$'\t' } _autostart_manage_enable () { _systemctl "$1" add-wants autostart.target } _autostart_manage_disable () { _systemctl "$1" disable } _autostart_manage_rm () { rm "$base_dir$(_filename "$1")" } case $1 in list) _autostart_manage_list ;; info) _autostart_manage_info ;; add) _autostart_manage_add "$2" "$3" ;; enable) _autostart_manage_enable "$2" ;; disable) _autostart_manage_disable "$2" ;; rm) _autostart_manage_rm "$2" ;; *) echo "'$1' is not valid" ;; esac