.dotfiles/.bin/autostart-manage

90 lines
1.8 KiB
Plaintext
Raw Normal View History

2022-10-13 14:08:14 +00:00
#!/usr/bin/env bash
base_dir="$HOME/.config/systemd/user/"
_filename () {
echo "autostart-$1.service"
}
_systemctl () {
systemctl --user "${@:2}" "$(_filename "$1")"
}
2022-10-13 21:50:13 +00:00
_list () {
systemctl --user list-unit-files \
| grep "autostart-" \
| awk '{print $1}' \
| sed -e 's/^autostart-//' -e 's/.service$//'
}
2022-10-13 14:08:14 +00:00
_echo_table () {
2022-12-16 20:46:19 +00:00
printf "Unit\tFilename\tEnabled\tActive\n"
2022-10-13 14:08:14 +00:00
for unit in "$@"
do
2022-12-16 20:46:19 +00:00
printf "%s\t%s\t%s\t%s\n" \
2022-10-13 14:08:14 +00:00
"$unit" \
"$(_filename "$unit")" \
2022-12-16 20:46:19 +00:00
"$(_systemctl "$unit" is-enabled)" \
"$(_systemctl "$unit" is-active)"
2022-10-13 14:08:14 +00:00
done
}
_autostart_manage_add () {
cat <<EOF >"$base_dir$(_filename "$1")"
[Unit]
Description=$1
[Service]
2022-12-14 14:25:49 +00:00
ExecStart=/bin/sh -c '. \$HOME/.profile && $2'
2022-10-13 14:08:14 +00:00
EOF
}
_autostart_manage_list () {
2022-10-13 21:50:13 +00:00
_list
2022-10-13 14:08:14 +00:00
}
_autostart_manage_info () {
2022-10-17 16:49:58 +00:00
autostart_units=()
while IFS='' read -r line
do
autostart_units+=("$line")
done < <(_list)
2022-10-13 14:08:14 +00:00
_echo_table "${autostart_units[@]}" | column -t -s$'\t'
}
2022-10-13 21:50:13 +00:00
_autostart_manage_enable_all () {
2022-10-17 16:49:58 +00:00
autostart_units=()
while IFS='' read -r line
do
autostart_units+=("$line")
done < <(_list)
2022-10-13 21:50:13 +00:00
for unit in "${autostart_units[@]}"
do
_systemctl "$unit" add-wants autostart.target
done
2022-10-13 14:08:14 +00:00
}
_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" ;;
rm) _autostart_manage_rm "$2" ;;
2022-10-13 21:50:13 +00:00
enable-all) _autostart_manage_enable_all "$2" ;;
show) _systemctl "$2" cat ;;
edit) _systemctl "$2" edit --full ;;
enable) _systemctl "$2" add-wants autostart.target ;;
2022-10-27 13:55:18 +00:00
disable) _systemctl "$2" disable ;;
2022-12-16 20:46:19 +00:00
restart) _systemctl "${2:-*}" restart;;
start) _systemctl "${2:-*}" start;;
2022-10-24 14:57:31 +00:00
status) _systemctl "${2:-*}" status ;;
2022-12-16 20:46:19 +00:00
stop) _systemctl "${2:-*}" stop ;;
run) systemctl --user start autostart.target ;;
2022-10-13 14:08:14 +00:00
*) echo "'$1' is not valid" ;;
esac