From 28878bd911695f45a7d2a20022f16ac8cb21212b Mon Sep 17 00:00:00 2001
From: Salt <rehashedsalt@cock.li>
Date: Fri, 21 Jun 2019 18:49:03 -0500
Subject: [PATCH] Add .firestarterrc support, fix rotating logs in a dry run

---
 README.md   |  2 ++
 firestarter | 29 +++++++++++++++++++----------
 2 files changed, 21 insertions(+), 10 deletions(-)

diff --git a/README.md b/README.md
index 4afff5f..627217e 100644
--- a/README.md
+++ b/README.md
@@ -28,6 +28,8 @@ By setting a dummy execution line, one can effectively prevent an entire configu
     command -v lemonbar
     ~/.bin/lemonbar.sh | lemonbar
 
+After all these programs have been started, the last thing firestarter does is execute `~/.firestarterrc`, if it exists.
+
 # Logging
 
 All STDOUT and STDERR messages from these commands are saved to a logfile in `~/.local/share/firestarter/logs` under the same name as the configuration file. By default, these logfiles are rotated every time you log in.
diff --git a/firestarter b/firestarter
index 43d4e04..02911b3 100755
--- a/firestarter
+++ b/firestarter
@@ -11,6 +11,7 @@
 _name="firestarter"
 _configdir="${XDG_CONFIG_HOME:-$HOME/.config}/$_name"
 _logdir="${XDG_DATA_HOME:-$HOME/.local/share}/$_name/logs"
+_firestarterrc="$HOME/.firestarterrc"
 
 # Basic functions
 print() {
@@ -276,7 +277,7 @@ https://gitlab.com/rehashedsalt/firestarter
 EOF
 }
 step_check() {
-	if [ -z "$dryrun" ]; then
+	if [ -z "$_dryrun" ]; then
 		for pid in $(pgrep firestarter); do
 			if [ "$pid" != "$BASHPID" ]; then
 				err "Firestarter is already running" 61
@@ -287,7 +288,7 @@ step_check() {
 }
 step_preexecute() {
 	# Special things that can't use simple configuration files
-	[ "$dryrun" = "1" ] && return 0
+	[ "$_dryrun" = "1" ] && return 0
 
 	# dbus
 	if \
@@ -359,11 +360,13 @@ step_preexecute() {
 	fi
 }
 step_execute() {
+	# Ensure we have a config directory
 	if ! [ -d "$_configdir" ]; then
 		if ! mkdir -p "$_configdir" > /dev/null 2>&1; then
 			err "Failed to create configuration directory \"$_configdir\"" 52
 		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
@@ -371,6 +374,7 @@ step_execute() {
 			err "Failed to create log directory \"$_logdir\"" 53
 		fi
 	fi
+	# Parse configs
 	for file in "$_configdir"/*; do
 		if ! [ -f "$file" ]; then
 			log "No configuration files found; generating defaults"
@@ -387,11 +391,11 @@ step_execute() {
 			read -r execline
 			if bash -c "$checkline" > /dev/null 2>&1; then
 				log "Found target for \"$filename\": \"$execline\""
-				if [ -f "$logfile" ]; then
-					[ -f "$logfile.old" ] && rm "$logfile.old" 
-					mv "$logfile" "$logfile.old"
-				fi
-				if ! [ "$dryrun" = "1" ]; then
+				if ! [ "$_dryrun" = "1" ]; then
+					if [ -f "$logfile" ]; then
+						[ -f "$logfile.old" ] && rm "$logfile.old" 
+						mv "$logfile" "$logfile.old"
+					fi
 					bash -c "$execline" > "$logfile" 2>&1 &
 				fi
 				break
@@ -400,9 +404,14 @@ step_execute() {
 			fi
 		done < "$file"
 	done
+	# And then source in a user script if it exists
+	if [ -r "$_firestarterrc" ] && [ -z "$_dryrun" ]; then
+		log "Sourcing .firestarterrc"
+		"$_firestarterrc"
+	fi
 }
 step_wait() {
-	[ "$dryrun" = "1" ] && exit 0
+	[ "$_dryrun" = "1" ] && exit 0
 	log "Waiting for programs to exit"
 	log "Send any termination signal to firestarter to log out"
 	trap step_logout EXIT
@@ -429,9 +438,9 @@ main() {
 	while getopts ":dgh" opt; do
 		case $opt in
 			d)
-				if ! [ "$dryrun" = "1" ]; then
+				if ! [ "$_dryrun" = "1" ]; then
 					log "Performing a dry run"
-					dryrun=1
+					_dryrun=1
 				fi
 				;;
 			g)