#!/bin/sh

# Start a D-Bus session if one is not already available
if [ -z "${DBUS_SESSION_BUS_ADDRESS:-}" ]; then
exec dbus-run-session -- "$0" "$@"
fi

# GTK Theming
export GTK_THEME=Adwaita:dark

# Set up monitors. Confirm these names with: xrandr --query
xrandr \
  --output eDP-1 --mode 1920x1080 --rate 60 --primary \
  --output DP-2-1 --mode 1920x1080 --same-as eDP-1

# Set the keyboard layouts
setxkbmap -layout "us,cz" -variant ",qwerty" -option "grp:alt_shift_toggle"

# Natural scrolling for the touchpad (requires xinput and the libinput driver)
xinput list --name-only | while IFS= read -r device; do
    case "$device" in
        *[Tt]ouch[Pp]ad*)
            xinput set-prop "$device" "libinput Natural Scrolling Enabled" 1 2>/dev/null
            ;;
    esac
done

dwmblocks &

# i really need it :(
export GPG_TTY=$(tty)

exec dbus-run-session -- sh -c '
    export GPG_TTY=$(tty)
    export PATH=$HOME/.local/bin:$PATH

    if ! pgrep -a -u "$(id -u)" -x pipewire >/dev/null 2>&1; then
        pipewire &
    fi
    sleep 1
    pkill -RTMIN+2 dwmblocks

    # Start desktop services
    bluetoothctl power off &
    if ! pgrep -a -u "$(id -u)" -x picom >/dev/null 2>&1; then
        picom --backend glx &
    fi
    if ! pgrep -a -u "$(id -u)" -x dunst >/dev/null 2>&1; then
        dunst &
    fi

    if ! pgrep -a -u "$(id -u)" -x mpv >/dev/null 2>&1; then
        mpv --shuffle --pause --no-video --loop-playlist -ao=alsa $HOME/Music &
    fi
    if ! pgrep -a -u "$(id -u)" -x mpv-shuffle-notify >/dev/null 2>&1; then
        mpv-shuffle-notify &
    fi

    if ! pgrep -a -u "$(id -u)" -x sxhkd >/dev/null 2>&1; then
        sxhkd &
    fi

    xbindkeys &

    # xss-lock -l slock &
    if ! pgrep -a -u "$(id -u)" -x xss-lock >/dev/null 2>&1; then
        xss-lock -- \
            env XSECURELOCK_AUTHPROTO=authproto_pam \
                XSECURELOCK_PAM_SERVICE=login \
                XSECURELOCK_NO_PAM_RHOST=1 \
                xsecurelock &
    fi

    feh --bg-scale "$HOME/Pictures/wallpapers/glados.jpg" &

    exec dwm
'
