.dotfiles/.bin/autostart-manage

139 lines
3 KiB
Text
Raw Normal View History

#!/usr/bin/env bash
2022-10-13 14:08:14 +00:00
2023-07-01 14:34:53 +00:00
hostname=$(cat /proc/sys/kernel/hostname)
2023-02-02 18:55:18 +00:00
_systemctl () {
systemctl --user "${@:2}" "autostart@$1.service"
2022-10-13 14:08:14 +00:00
}
2023-07-15 11:54:52 +00:00
_query_autostart_toml() {
tomlq -r --arg host "$hostname" \
'.hosts[$host].groups as $groups | .apps | to_entries[] | select(
(.value.hosts | contains([$host])) or
([.value.group] | inside($groups))
) | '"$1" \
2023-07-15 13:21:33 +00:00
"$XDG_CONFIG_HOME/autostart.toml"
2022-10-13 14:08:14 +00:00
}
2022-10-13 21:50:13 +00:00
_list () {
2023-07-27 14:05:15 +00:00
_query_autostart_toml '.value.alias // .key'
2022-10-13 21:50:13 +00:00
}
2022-10-13 14:08:14 +00:00
_echo_table () {
for unit in "$@"
do
2023-07-15 13:21:33 +00:00
printf "%s\t%s\t%s\t%s\n" \
2022-10-13 14:08:14 +00:00
"$unit" \
2022-12-16 20:46:19 +00:00
"$(_systemctl "$unit" is-enabled)" \
2023-07-15 13:21:33 +00:00
"$(_systemctl "$unit" is-active)" \
"$(_get_autostart_cmd "$unit")"
2022-10-13 14:08:14 +00:00
done
}
_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)
2023-07-15 13:21:33 +00:00
_echo_table "${autostart_units[@]}" | column -t -s$'\t' --table-columns 'Unit,Enabled?,Active?,Command'
2022-10-13 14:08:14 +00:00
}
2023-06-30 18:47:01 +00:00
_autostart_manage_log () {
journalctl --user -fu "autostart@$1.service"
}
2023-07-01 14:34:53 +00:00
_autostart_manage_sync () {
rm "$HOME/.config/systemd/user/autostart.target.wants/"*
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
}
2023-10-18 21:51:45 +00:00
2023-02-02 18:55:18 +00:00
_get_autostart_cmd () {
2023-07-27 14:05:15 +00:00
_query_autostart_toml 'select((.key == "'"$1"'") or (.value.alias == "'"$1"'")) | .value.command'
2023-02-02 18:55:18 +00:00
}
2023-10-18 21:51:45 +00:00
_get_autostart_delay () {
_query_autostart_toml 'select((.key == "'"$1"'") or (.value.alias == "'"$1"'")) | .value.delay // 0'
}
2023-02-02 18:55:18 +00:00
_autostart_manage_exec () {
cmd=$(_get_autostart_cmd "$1")
2023-10-18 21:51:45 +00:00
sleep "$(_get_autostart_delay "$1")"
2023-02-02 18:55:18 +00:00
bash -c "$cmd"
2022-10-13 14:08:14 +00:00
}
2023-06-30 18:47:01 +00:00
_autostart_run_graphical () {
pass x # Try to unlock yubikey asap
2022-12-29 13:46:34 +00:00
start-audio pipewire
wait-for-service "network-online.target"
2023-07-06 21:38:19 +00:00
# Execute only if not already running
# Don't match keepassxc-proxy
if ! (pgrep -l keepassxc | grep -v prox) >/dev/null
2022-12-29 13:46:34 +00:00
then
2023-07-01 11:05:41 +00:00
if pass x
then
(pass keepass | head -n 1 | keepassxc --pw-stdin ~/sync/passwords.kdbx) &
fi
2022-12-29 13:46:34 +00:00
fi
autoinstall graphical
autostart-manage run
2023-06-30 18:47:01 +00:00
}
_autostart_run_wayland () {
_autostart_run_graphical
killall -SIGUSR2 waybar
}
_autostart_run_xorg () {
feh --bg-fill "$XDG_PICTURES_DIR/wallpaper/active_wallpaper"
"$HOME/.config/polybar/launch.sh" &
"$HOME/.config/bspwm/focus-voip.py" &
numlockx on
xsetroot -cursor_name left_ptr
xrdb "$HOME/.Xresources"
_autostart_run_graphical
2022-12-29 13:46:34 +00:00
}
2022-10-13 14:08:14 +00:00
case $1 in
2023-06-30 18:47:01 +00:00
list) _autostart_manage_list ;;
info) _autostart_manage_info ;;
2023-07-01 14:34:53 +00:00
sync) _autostart_manage_sync ;;
2023-06-30 18:47:01 +00:00
enable) _systemctl "$2" add-wants autostart.target ;;
disable) _systemctl "$2" disable ;;
restart) _systemctl "${2:-*}" restart;;
log) _autostart_manage_log "$2";;
start) _systemctl "${2:-*}" start;;
status) _systemctl "${2:-*}" status ;;
stop) _systemctl "${2:-*}" stop ;;
run) systemctl --user start autostart.target ;;
run-xorg) _autostart_run_xorg ;;
run-wayland) _autostart_run_wayland ;;
exec) _autostart_manage_exec "$2" ;;
*) echo "'$1' is not valid" ;;
2022-10-13 14:08:14 +00:00
esac