firestarter/firestarter

372 lines
9.3 KiB
Bash
Executable File

#! /bin/bash
#
# firestarter
# A desktop environment startup script
# Copyright (C) 2019 Vintage Salt <rehashedsalt@cock.li>
#
# Distributed under terms of the MIT license.
#
set -e
# Read-only set-once variables
declare -r _name="$(basename -- "$0")"
declare -r _sessionid="$(< /proc/self/sessionid)"
# Options
declare _optconfigdir="${XDG_CONFIG_HOME:-$HOME/.config}/${_name}"
declare _optdryrun
declare _optlogdir="$_optconfigdir/logs"
declare -i _opthelp
declare -i _optverbose
# Working variables
declare -a _args
declare _return
# Helper functions
log() {
# Print a line to the terminal if _optverbose is greater than $2
# $2 defaults to 0
# loglevel 0: Daily-use messages
# loglevel 1: Detailed but not quite debugging
# loglevel 2: Definitely debugging
[ -z "$1" ] && return 1
if (( _optverbose >= ${2:-0} )); then
printf "%s - %s\\n" "$(date -Iseconds)" "$1"
fi
}
warn() {
# Print a yellow line to the terminal, respecting _optverbose
[ -z "$1" ] && return 1
if (( _optverbose >= ${2:-0} )); then
if [ -t 1 ]; then
printf "\\e[33m%s\\e[0m\\n" "$1"
else
printf "WARN: %s\\n" "$1"
fi
fi
}
error() {
# Print a red line to the terminal, exit if $2 is specified
[ -z "$1" ] && return 1
if [ -t 2 ]; then
printf "\\e[31m%s\\e[0m\\n" "$1" 1>&2
else
printf "ERROR: %s\\n" "$1" 1>&2
fi
[ -z "$2" ] && return
exit "${2:-1}"
}
has() {
# Parse out all arguments and try to find them in path
# If an argument cannot be found, set _return and fail
for prog in "$@"; do
if ! command -v "$prog" > /dev/null 2>&1; then
_return="$prog"
return 1
fi
done
return 0
}
# Core program functions
gettarget() {
# Parse a defaults file to get a target program
[ -z "$1" ] && return 1
[ -d "$1" ] && return 1
[ -r "$1" ] || return 1
# Every odd line is a condition
# Every even one is a target
local firstline
while read -r checkline; do
if [ -z "$firstline" ]; then
if [ "$checkline" = "#.fsdefaults" ]; then
firstline=1
continue
else
return 50
fi
fi
[ "${checkline#"#"}" != "$checkline" ] && continue
read -r execline
if bash -c "$checkline" > /dev/null 2>&1; then
_return="$execline"
return 0
else
continue
fi
done < "$1"
return 2
}
step_preexecute() {
# Special things that can't use simple configuration files
[ -n "$_optdryrun" ] && return 0
[ -r "$HOME/.xsessionrc" ] && . "$HOME/.xsessionrc"
export XDG_CURRENT_DESKTOP="${XDG_CURRENT_DESKTOP:-firestarter}"
# dbus
if \
[ -z "$DBUS_SESSION_BUS_ADDRESS" ] && \
[ -n "$XDG_RUNTIME_DIR" ] && \
[ "$XDG_RUNTIME_DIR" = "/run/user/$(id -u)" ] && \
[ -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" ] && \
has dbus-launch; then
# We have dbus but haven't started it yet
eval "$(dbus-laucnh --exit-with-session --sh-syntax)"
hasdbus=1
else
warn "Did not start dbus; some applications may misbehave"
fi
if [ -n "$hasdbus" ]; then
has dbus-update-activation-environment && \
dbus-update-activation-environment --verbose --systemd --all >/dev/null 2>&1
fi
unset hasdbus
# kcminit/Qt settings
if has qt5ct; then
log "Initializing qt5ct"
if [ -z "$QT_QPA_PLATFORMTHEME" ]; then
export QT_QPA_PLATFORMTHEME="qt5ct"
log "Exporting QT_QPA_PLATFORMTHEME as \"$QT_QPA_PLATFORMTHEME\"" 2
else
log "Using existing theme setting \"$QT_QPA_PLATFORMTHEME\"" 2
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\"" 2
else
log "Using existing scale factor \"$QT_AUTO_SCREEN_SCALE_FACTOR\"" 2
fi
fi
if has kcminit; then
log "Initializing KDE Control Module settings"
kcminit >/dev/null 2>&1
export XDG_CURRENT_DESKTOP="KDE"
fi
# xhost
if has xhost; then
if xhost +si:localuser:"$(id -un)" >/dev/null 2>&1; then
log "Session open to other sessions by this user"
else
warn "Failed to open session via xhost"
fi
fi
# xresources
if [ -n "$DISPLAY" ] && has xrdb && [ -r "$HOME/.Xresources" ]; then
if xrdb "$HOME/.Xresources" >/dev/null 2>&1; then
log "Loaded in .Xresources"
else
warn "Failed to load .Xresources"
fi
fi
# xsetroot
if has xsetroot; then
log "Setting root window properties"
xsetroot -cursor_name left_ptr
fi
# xset
if has xset; then
log "Disabling bell"
xset -b
log "Disabling DPMS"
xset s off -dpms
fi
}
step_execute() {
# Parse out our defaults lists and execute their targets
if ! [ -d "$_optlogdir" ]; then
if ! mkdir -p "$_optlogdir" >/dev/null 2>&1; then
error "Failed to create log directory: \"$_optlogdir\"" 53
fi
fi
for file in "$_optconfigdir"/*; do
if ! [ -e "$file" ]; then
error "No configuration files found" 70
fi
# Skip our logs directory
[ "$_optlogdir" == "$file" ] && continue
local filename="$(basename -- "$file")"
local logfile="$_optlogdir/$filename.log"
if gettarget "$file"; then
# It's a defaults file with a selected target
target="$_return"
log "Found target for $filename: \"$target\""
[ -n "$_optdryrun" ] && continue
if [ -f "$logfile" ]; then
[ -f "$logfile.old" ] && rm "$logfile.old"
mv "$logfile" "$logfile.old"
fi
bash -c "$target" > "$logfile" 2>&1 &
elif [ $? = 50 ] && [ -x "$file" ]; then
# It's a shell script or executable symlink
log "Executing file: \"$filename\""
[ -n "$_optdryrun" ] && continue
"$file" > "$logfile" 2>&1 &
else
warn "Could not execute file: \"$filename\""
fi
done
}
step_postexecute() {
# Wait for the WM to initialize, if one was found and we have the tools
if [ -z "$FS_NOWAITWM" ] && gettarget "$_optconfigdir/wm" && has xprop grep; then
log "Waiting for WM to initialize: \"$_return\""
for (( i=0; i<10; i++ )); do
line="$(xprop -root | grep ^_NET_WM_NAME | grep -o '"\S*"$')"
if [ -n "$line" ]; then
log "WM has initialized, _NET_WM_NAME atom reads: $line"
break
fi
read -t 1 -u 1023
done
fi
# Dumb polybar workaround
killall polybar -SIGUSR1
# Execute a user rc if it exists
local firestarterrc="$HOME/.firestarterrc"
if [ -r "$firestarterrc" ] && [ -z "$dryrun" ]; then
log "Executing rc script: $firestarterrc"
"$firestarterrc"
fi
# Start XDG autostarters, if they exist
if [ -z "$_optdryrun" ]; then
if has dex; then
dex -a >/dev/null 2>&1
elif has fbautostart; then
fbautostart > /dev/null 2>&1
elif has xdg-autostart; then
xdg-autostart ${XDG_CURRENT_DESKTOP:-firestarter}
else
warn "Could not find an XDG autostarter"
fi
fi
}
step_wait() {
[ -n "$_optdryrun" ] && exit 0
trap step_logout EXIT
if [ -n "$FS_DIEONWM" ] && gettarget "$_optconfigdir/wm" && has readlink "$_return"; then
target="$(command -v "$_return")"
for job in $(jobs -p); do
if [ "$target" = "$(readlink /proc/$job/exe)" ]; then
log "Waiting for WM to exit: \"$_return\""
wait "$job"
exit 0
fi
done
warn "Could not find WM: \"$target\""
fi
log "Waiting for programs to exit"
wait
exit 0
}
step_logout() {
log "Logging out"
if has loginctl; then
# Use loginctl if possible
if [ -n "$_sessionid" ]; then
loginctl terminate-session "$_sessionid"
fi
else
# Otherwise just brute it out
kill $(jobs -p)
fi
return 0
}
printhelp() {
cat << EOF
Usage: $_name [OPTION]...
-d Perform a dry run; print what programs would have been
executed instead of doing so
-h Print this help text
-v Print more status messages. Stacks
Environment Variables:
FS_DIEONWM If nonempty, end the session when the WM dies.
FS_NOWAITWM If nonempty, skip waiting for the WM to initialize
Copyright (c) 2019 rehashedsalt@cock.li
Licensed under the MIT license
EOF
}
# Main
main() {
# Parse out arguments
while [ -n "$1" ]; do
# Parse out flags
while getopts ":dhv" opt; do
case $opt in
d)
_optdryrun=1
;;
h)
_opthelp=1
;;
v)
_optverbose+=1
;;
:)
error "Option requires argument: -$OPTARG" 2
;;
*)
error "Invalid option: -$OPTARG" 2
;;
esac
done
# Store arguments
shift $((OPTIND - 1))
if [ -n "$1" ]; then
_args+=("$1")
shift
fi
unset OPTIND
done
# Early hook for help
[ -n "$_opthelp" ] && printhelp && exit 0
# Ensure our running environment is sane and that we're not about to nest
if [ -z "$_optdryrun" ]; 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 it's us then who cares
[ "$pid" == "$BASHPID" ] && continue
# We care
error "Firestarter is already running: $pid" 40
done
fi
if ! [ -d "$HOME" ] || ! [ -r "$HOME" ]; then
error "Home directory not found or inaccessable: \"$HOME\"" 54
fi
if ! [ -d "$_optconfigdir" ]; then
if [ -f "$_optconfigdir" ]; then
error "Config directory is a file, should be directory: \"$_optconfigdir\"" 52
fi
if ! mkdir -p "$_optconfigdir" > /dev/null 2>&1; then
error "Failed to find or create config directory: \"$_optconfigdir\"" 52
fi
fi
# Validate core program dependencies
log "Validating dependencies" 2
if ! has basename; then
error "Failed to find program: $_return" 1
fi
# Do the do
[ -n "$_optdryrun" ] && log "Performing a dry run"
step_preexecute
step_execute
step_wait
step_logout
exit 0
}
main "$@"