#!/bin/sh

# Source this file from ~/.config/openbox/autostart using:
# . $CATLOAF_HOME/etc/openbox/autostart

if [ -x /usr/bin/xrdb ] && [ -d "$CATLOAF_HOME"/etc ] && [ -f ~/.Xresources ]; then
    /usr/bin/xrdb -I"$CATLOAF_HOME"/etc -merge ~/.Xresources
fi

if [ -x /usr/bin/xbindkeys ]; then
    /usr/bin/xbindkeys -f "$CATLOAF_HOME"/etc/xbindkeysrc
fi

if [ -x /usr/bin/tint2 ]; then
    /usr/bin/tint2 > /dev/null 2>&1 &
fi

if [ -x /usr/bin/parcellite ]; then
    /usr/bin/parcellite > /dev/null 2>&1 &
fi

if [ -x /usr/bin/dunst ]; then
    /usr/bin/dunst > /dev/null 2>&1 &
fi

if [ -x /usr/bin/flameshot ]; then
    /usr/bin/flameshot > /dev/null 2>&1 &
fi

# Don't use an absolute path for powerkit, at the time of introduction it is part of the Catloaf Contrib dist,
# but in the future, Debian may package it for us.
# This prevents Firefox from putting the display to sleep when watching videos but not touching the mouse.
if [ -x "$(command -v powerkit)" ]; then
    powerkit > /dev/null 2>&1 &
fi

# AppArmor notify relies on auditd to log messages.
if [ -x /usr/bin/aa-notify ] && [ -x /usr/sbin/auditd ] && [ -f /var/log/audit/audit.log ]; then
    /usr/bin/aa-notify --poll -f /var/log/audit/audit.log > /dev/null 2>&1 &
fi

# 2023-05-27 - Don't run ssh-agent from here, it should be handled by Xsession now.

# Only some of my machines use NetworkManager.
if [ -x /usr/bin/nm-applet ]; then
    /usr/bin/nm-applet --sm-disable > /dev/null 2>&1 &
fi

# Debian 11 deprecated compton in favor of picom
if [ -x /usr/bin/picom ]; then
    if [ -f "$HOME"/.config/picom/picom.conf ]; then
        PICOMCONF="$HOME"/.config/picom/picom.conf
    else
        PICOMCONF="$CATLOAF_HOME"/etc/picom.conf
    fi
    /usr/bin/picom --daemon --config "$PICOMCONF" > /dev/null 2>&1
fi

# One trick here is to symlink to the desired wallpaper. This allows for the infrastructure
# to set the wallpaper to be centralized while still allowing machine-specific configuration.
if [ -x /usr/bin/feh ]; then
    if [ -f "$XDG_CONFIG_HOME/catloaf/wallpaper" ]; then
        WALLPAPER="$XDG_CONFIG_HOME/catloaf/wallpaper"
    elif [ -f "$HOME/.config/catloaf/wallpaper" ]; then
        WALLPAPER="$HOME/.config/catloaf/wallpaper"
    else
        WALLPAPER="$HOME/.mywallpaper"
    fi

    /usr/bin/feh --bg-scale "$WALLPAPER" > /dev/null 2>&1
fi

# Give everyone a chance to catch up.
sleep 1
