firestarter/firestarter

486 lines
11 KiB
Plaintext
Raw Normal View History

2019-04-27 00:01:25 -05:00
#! /bin/bash
#
2019-06-21 16:39:11 -05:00
# firestarter
2019-04-27 00:01:25 -05:00
# A desktop environment startup script
# Copyright (C) 2019 Vintage Salt <rehashedsalt@cock.li>
#
# Distributed under terms of the MIT license.
#
# "Globals"
_name="firestarter"
_configdir="${XDG_CONFIG_HOME:-$HOME/.config}/$_name"
_logdir="${XDG_DATA_HOME:-$HOME/.local/share}/$_name/logs"
_firestarterrc="$HOME/.firestarterrc"
_sessionid="$(< /proc/self/sessionid)"
2019-04-27 00:01:25 -05:00
# Basic functions
print() {
# Write a message to STDOUT without a name attached
[ -z "$1" ] && return 1
printf "%s\\n" \
"$1"
}
log() {
# Write a message to STDOUT
[ -z "$1" ] && return 1
printf "%s log: %s\\n" \
"$_name" \
2019-04-27 00:01:25 -05:00
"$1" >&1
}
err() {
# Write a message to STDERR, also exit if arg 2 is specified
[ -z "$1" ] && return 1
printf "%s err: %s\\n" \
"$_name" \
2019-04-27 00:01:25 -05:00
"$1" >&2
[ -z "$2" ] && return 0
if ! [ "$2" -ge "0" ] > /dev/null 2>&1; then
err "Attempted to exit with malformed exit code \"$2\"" 10
else
exit "$2"
fi
}
gettarget() {
# Parse a defaults file to get the target program
2019-04-27 00:01:25 -05:00
[ -z "$1" ] && return 1
[ -r "$1" ] || return 1
# Every odd line is the check line
# Every even one is the exec line
while read -r checkline; do
read -r execline
if bash -c "$checkline" > /dev/null 2>&1; then
_return="$execline"
return 0
else
continue
fi
done < "$1"
return 2
2019-04-27 00:01:25 -05:00
}
# Steps in execution
step_generate() {
log "Creating default config setup in \"$_configdir\""
2019-04-27 00:01:25 -05:00
log "See firestarter -h for more information"
# Audio daemon
cat << EOF > "$_configdir/audio-daemon"
2019-04-27 00:01:25 -05:00
command -v pulseaudio
pulseaudio
EOF
# Information bars
cat << EOF > "$_configdir/bar"
2019-04-27 00:01:25 -05:00
command -v tint2
tint2
command -v polybar && [ -r "$HOME/.config/polybar/launch.sh" ]
"$HOME/.config/polybar/launch.sh"
2019-04-27 00:01:25 -05:00
command -v lxpanel
lxpanel
command -v lxqt-panel
lxqt-panel
command -v mate-panel
mate-panel
command -v xfce4-panel
xfce4-panel
2019-06-21 16:58:01 -05:00
EOF
# Blue light filter
cat << EOF > "$_configdir/blue-light-filter"
2019-06-21 16:58:01 -05:00
command -v redshift-gtk
redshift-gtk
command -v redshift
redshift
EOF
# Compositor
cat << EOF > "$_configdir/compositor"
2019-06-21 16:58:01 -05:00
[ -z "\$DISPLAY" ]
:
command -v unagi
unagi
command -v compton
compton
command -v xcompmgr
xcompmgr
2019-04-27 00:01:25 -05:00
EOF
# Polkit authentication agents
cat << EOF > "$_configdir/polkit-agent"
2019-04-27 00:01:25 -05:00
command -v lxqt-policykit-agent
lxqt-policykit-agent
command -v lxpolkit
lxpolkit
command -v mate-polkit
mate-polkit
command -v polkit-efl-authentication-agent-1
polkit-efl-authentication-agent-1
2019-06-21 20:17:07 -05:00
[ -x "/usr/lib/ts-polkitagent" ]
/usr/lib/ts-polkitagent
[ -x "/usr/lib/$(uname -m)-linux-gnu/polkit-mate/polkit-mate-authentication-agent-1" ]
"/usr/lib/$(uname -m)-linux-gnu/polkit-mate/polkit-mate-authentication-agent-1"
[ -x "/usr/lib/mate-polkit/polkit-mate-authentication-agent-1" ]
/usr/lib/mate-polkit/polkit-mate-authentication-agent-1
[ -x "/usr/lib/$(uname -m)-linux-gnu/libexec/polkit-kde-authentication-agent-1" ]
"/usr/lib/$(uname -m)-linux-gnu/libexec/polkit-kde-authentication-agent-1"
[ -x "/usr/lib/polkit-kde-authentication-agent-1" ]
/usr/lib/polkit-kde-authentication-agent-1
[ -x "/usr/lib/policykit-1-gnome/polkit-gnome-authentication-agent-1" ]
/usr/lib/policykit-1-gnome/polkit-gnome-authentication-agent-1
[ -x "/usr/lib/polkit-gnome/polkit-gnome-authentication-agent-1" ]
/usr/lib/polkit-gnome/polkit-gnome-authentication-agent-1
2019-06-21 16:58:01 -05:00
EOF
# Hotkey daemon
cat << EOF > "$_configdir/hotkey-daemon"
2019-06-21 16:58:01 -05:00
[ -z "\$DISPLAY" ]
:
command -v sxhkd
sxhkd
EOF
# Network daemon
cat << EOF > "$_configdir/network-daemon"
command -v nm-applet
nm-applet
2019-06-21 16:58:01 -05:00
EOF
# Notification daemon
cat << EOF > "$_configdir/notification-daemon"
2019-06-21 16:58:01 -05:00
[ -z "\$DISPLAY" ]
:
command -v dunst
dunst
command -v lxqt-notificationd
notificationd
2019-04-27 00:01:25 -05:00
EOF
# Power daemons
cat << EOF > "$_configdir/power-daemon"
2019-04-27 00:01:25 -05:00
command -v batterymon
batterymon
command -v cbatticon
cbatticon
command -v lxqt-powermangement
lxqt-powermanagement
command -v xfce4-power-manager
xfce4-power-manager
command -v mate-power-manager
mate-power-manager
command -v gnome-power-manager
gnome-power-manager
2019-06-21 16:58:01 -05:00
EOF
# Runners
# Note that rofi is not a daemon and is not included here
cat << EOF > "$_configdir/runner"
2019-06-21 16:58:01 -05:00
command -v krunner
krunner
2019-04-27 00:01:25 -05:00
EOF
# Settings daemons
cat << EOF > "$_configdir/settings-daemon"
2019-04-27 00:01:25 -05:00
command -v xsettingsd
xsettingsd
command -v xsettings-kde
xsettingskde
command -v xfsettingsd
xfsettingsd
command -v mate-settings-daemon
mate-settings-daemon
command -v gnome-settings-daemon
gnome-settings-daemon
EOF
# System statistics glances
2019-06-21 18:35:25 -05:00
# Note: the dumb sleep hack is because Conky crashes with window_type override if the WM hasn't loaded yet
# This gives the Wm ample time to load up
cat << EOF > "$_configdir/stat-glances"
[ -z "\$DISPLAY" ]
:
command -v conky && [ -r "\${XDG_CONFIG_HOME:-$HOME/.config}/conky/conky.conf" ]
2019-06-21 18:35:25 -05:00
sleep 5 && conky
EOF
# Wallpaper setters
cat << EOF > "$_configdir/wallpaper"
[ -z "\$DISPLAY" ]
:
command -v feh && [ -r "$HOME/.fehbg" ]
~/.fehbg
command -v nitrogen
nitrogen --restore
2019-04-27 00:01:25 -05:00
EOF
2019-06-21 16:58:01 -05:00
# Window managers
cat << EOF > "$_configdir/wm"
2019-04-27 00:01:25 -05:00
[ -z "\$DISPLAY" ]
:
command -v 2bwm
2bwm
command -v aewm
aewm
command -v awesome
awesome
command -v bspwm
bspwm
command -v catwm
catwm
command -v cwm
cwm
command -v dwm
dwm
command -v evilwm
evilwm
command -v exwm
exwm
command -v fluxbox
fluxbox
command -v flwm
flwm
command -v fvwm
fvwm
command -v herbstluftwm
herbstluftwm
command -v i3
i3
command -v icewm
icewm
command -v jbwm
jbwm
command -v jwm
jwm
command -v lwm
lwm
command -v openbox
openbox
command -v pawm
pawm
command -v ratpoison
ratpoison
command -v twm
twm
command -v windowmaker
windowmaker
command -v wmii
wmii
command -v xmonad
xmonad
command -v xfwm4
xfwm4
command -v metacity
metacity
command -v mutter
mutter
command -v kwin
kwin
command -v tinywm
tinywm
EOF
}
step_printhelp() {
cat << EOF
2019-06-21 07:57:54 -05:00
Usage: $(basename -- "$0") [OPTION...]
2019-04-27 00:01:25 -05:00
Start or generate a desktop environment configuration
-h Show this help text
2019-06-21 16:39:11 -05:00
-g Generate a default configuration. This will clobber
2019-04-27 00:01:25 -05:00
-d Perform a dry run. This will print the results of all decisions
without executing them.
Additionally, firestarter responds to the following environment variables:
FS_NOLOG If nonempty, create no log files
Copyright (c) 2019 rehashedsalt@cock.li
Licensed under the MIT License
2019-04-27 00:01:25 -05:00
https://gitlab.com/rehashedsalt/firestarter
EOF
}
step_check() {
if [ -z "$_dryrun" ]; then
for pid in $(pgrep firestarter); do
# Skip invalid PIDs
! [ -d "/proc/$pid" ] && continue
# If it's not our session then who cares
[ "$_sessionid" != "$(< "/proc/$pid/sessionid")" ] && continue
if [ "$pid" != "$BASHPID" ]; then
2019-06-21 19:12:41 -05:00
err "Firestarter is already running: $pid" 40
fi
done
fi
if ! [ -d "$HOME" ] || ! [ -r "$HOME" ]; then
err "Inaccessible home directory: \"$HOME\"" 54
fi
return 0
}
2019-04-27 00:01:25 -05:00
step_preexecute() {
# Special things that can't use simple configuration files
[ "$_dryrun" = "1" ] && return 0
2019-04-27 00:01:25 -05:00
2019-06-21 21:25:44 -05:00
# Exports
export XDG_CURRENT_DESKTOP="${XDG_CURRENT_DESKTOP:-firestarter}"
2019-04-27 00:01:25 -05:00
# dbus
if \
[ -z "$DBUS_SESSION_BUS_ADDRESS" ] && \
[ -n "$XDG_RUNTIME_DIR" ] && \
[ "$XDG_RUNTIME_DIR" = "/run/user/$(id -u)" ] && \
2019-04-27 00:01:25 -05:00
[ -S "$XDG_RUNTIME_DIR/bus" ]; then
# We already have a bus started, use it
export DBUS_SESSION_BUS_ADDRESS="unix:path=$XDG_RUNTIME_DIR/bus"
hasdbus=1
elif \
[ -z "$DBUS_SESSION_BUS_ADDRESS" ] && \
command -v dbus-launch > /dev/null 2>&1; then
# We have dbus but haven't started up a bus yet
eval "$(dbus-launch --exit-with-session --sh-syntax)"
hasdbus=1
else
log "Did not start dbus; some applications may misbehave"
fi
if [ -n "$hasdbus" ]; then
if command -v dbus-update-activation-environment > /dev/null 2>&1; then
dbus-update-activation-environment --verbose --systemd --all > /dev/null 2>&1
fi
fi
unset hasdbus
# kcminit/Qt settings
if command -v kcminit > /dev/null 2>&1; then
log "Initializing KDE Control Module settings"
kcminit > /dev/null 2>&1
export XDG_CURRENT_DESKTOP="KDE"
elif command -v qt5ct > /dev/null 2>&1; then
log "Integrating qt5ct"
if [ -z "$QT_QPA_PLATFORMTHEME" ]; then
export QT_QPA_PLATFORMTHEME="qt5ct"
log "Exporting QT_QPA_PLATFORMTHEME as \"$QT_QPA_PLATFORMTHEME\""
else
log "Using existing theme setting \"$QT_QPA_PLATFORMTHEME\""
fi
if [ -z "$QT_AUTO_SCREEN_SCALE_FACTOR" ]; then
export QT_AUTO_SCREEN_SCALE_FACTOR="0"
log "Exporting QT_AUTO_SCREEN_SCALE_FACTOR as $QT_AUTO_SCREEN_SCALE_FACTOR"
else
log "Using existing scale setting \"$QT_AUTO_SCREEN_SCALE_FACTOR\""
fi
fi
# xhost
if command -v xhost > /dev/null 2>&1; then
if xhost +si:localuser:"$(id -un)" > /dev/null 2>&1; then
log "Session can be accessed in other sessions by this user"
else
log "Failed to open session up via xhost"
fi
fi
# xresources
if [ -n "$DISPLAY" ] && command -v xrdb && [ -r "$HOME/.Xresources" ]; then
if xrdb "$HOME/.Xresources" > /dev/null 2>&1; then
log "Loaded in .Xresources"
else
log "Failed to load .Xresources"
fi
fi
# xset
if command -v xset > /dev/null 2>&1; then
log "Disabling bell"
xset -b
fi
2019-04-27 00:01:25 -05:00
}
step_execute() {
# Ensure we have a config directory
if ! [ -d "$_configdir" ]; then
if ! mkdir -p "$_configdir" > /dev/null 2>&1; then
2019-06-21 19:12:41 -05:00
err "Failed to create configuration directory: \"$_configdir\"" 52
2019-04-27 00:01:25 -05:00
fi
fi
# Ensure we can log if we have to
if [ -n "$FS_NOLOG" ]; then
log "No logs will be created per FS_NOLOG"
elif ! [ -d "$_logdir" ]; then
if ! mkdir -p "$_logdir" > /dev/null 2>&1; then
2019-06-21 19:12:41 -05:00
err "Failed to create log directory: \"$_logdir\"" 53
2019-04-27 00:01:25 -05:00
fi
fi
# Parse configs
for file in "$_configdir"/*; do
if ! [ -e "$file" ]; then
log "No configuration files found; generating defaults"
step_generate
step_execute
break
2019-04-27 00:01:25 -05:00
fi
local filename="$(basename -- "$file")"
local logfile="$_logdir/$filename"
[ -n "$FS_NOLOG" ] && logfile="/dev/null"
if gettarget "$file"; then
target="$_return"
log "Found target for \"$filename\": \"$target\""
if ! [ "$_dryrun" = "1" ]; then
if [ -f "$logfile" ]; then
[ -f "$logfile.old" ] && rm "$logfile.old"
mv "$logfile" "$logfile.old"
2019-04-27 00:01:25 -05:00
fi
bash -c "$target" > "$logfile" 2>&1 &
2019-04-27 00:01:25 -05:00
fi
fi
2019-04-27 00:01:25 -05:00
done
}
step_postexecute() {
# Wait for the WM to initialize, if one was found
# Source in a user script if it exists
if [ -r "$_firestarterrc" ] && [ -z "$_dryrun" ]; then
log "Sourcing .firestarterrc"
"$_firestarterrc"
fi
2019-04-27 00:01:25 -05:00
}
step_wait() {
[ "$_dryrun" = "1" ] && exit 0
2019-04-27 00:01:25 -05:00
log "Waiting for programs to exit"
log "Send any termination signal to firestarter to log out"
trap step_logout EXIT
2019-04-27 00:01:25 -05:00
wait
exit 0
}
step_logout() {
log "Logging out"
if command -v loginctl > /dev/null 2>&1; then
# Use loginctl if possible
if [ -n "$_sessionid" ]; then
loginctl terminate-session "$_sessionid"
fi
else
# Otherwise just brute it
kill $(jobs -p)
fi
return 0
}
2019-04-27 00:01:25 -05:00
# Main
main() {
while getopts ":dgh" opt; do
case $opt in
d)
if ! [ "$_dryrun" = "1" ]; then
2019-04-27 00:01:25 -05:00
log "Performing a dry run"
_dryrun=1
2019-04-27 00:01:25 -05:00
fi
;;
g)
step_generate
exit $?
;;
h)
step_printhelp
exit $?
;;
*)
err "Unrecognized argument: \"$OPTARG\"" 50
;;
:)
err "Invalid option: \"$OPTARG\"" 51
;;
esac
done
step_check
2019-06-21 17:01:21 -05:00
step_preexecute
2019-04-27 00:01:25 -05:00
step_execute
step_postexecute
2019-04-27 00:01:25 -05:00
step_wait
return 0
}
main "$@"