#!/usr/bin/env bash

# The script created by RedBearAK(https://github.com/RedBearAK) was used as a basis.

send_log() {
    echo "$1" | tee >(logger -p user.info)
}

send_log "Processing the contents of the XDG Autostart folders..."

autostart_dirs=(~/.config/autostart /etc/xdg/autostart)

for dir in "${autostart_dirs[@]}"; do
    for entry in "$dir"/*.desktop; do
        if [[ -f "$entry" ]]; then
            cmd=$(grep '^Exec=' "$entry" | head -n 1 | sed 's/^Exec=//' | sed 's/%.//g')
            send_log "Launching the desktop file: '$entry'"
            # shellcheck disable=SC2086
            $cmd > /dev/null 2>&1 &
        elif [[ -x "$entry" ]]; then
            send_log "Running an executable file: '$entry'"
            "$entry" > /dev/null 2>&1 &
        fi
    done
done

send_log "The processing of the contents of the XDG Autostart folders has been completed."
