#!/bin/sh

# TODO: Replace this script with tini

trap 'kill $pids $(pidof xcape) 1>/dev/null 2>&1' 2 15 EXIT

LOGFILE="${LOGFILE:-${HOME}/.local/log/session-$(date -I).log}"

log() {
	while IFS= read -r line; do
		printf "%b" "[$(date +%X)][$1]\t$line\n" >>"${LOGFILE}"
	done
}

start() {
    logp="${XDG_RUNTIME_DIR:-/tmp}/${1}"
    [ -p "$logp" ] || mkfifo "$logp"
    cat <"$logp" | log "$1" &
    pidof -sx "$1" || "./$1" >"$logp" 2>&1 &
    # pidof -sx "$1" || "./$1" 2>&1 | log "$1" &
}

cd "$HOME/.local/libexec/dwm/autorun.d" || exit 1

for s in *; do
    start "$s"
    pids="$! $pids"
done

wait