#!/usr/bin/env bash

command -v sxhkd >/dev/null 2>&1
return_val=$?
if [ $return_val -ne 0 ]; then
    echo >&2 "Can't seem to find the sxhkd executable. Exiting!"
    bspc quit && exit 1
else
    sxhkd &
    return_val=$?
    if [ $return_val -ne 0 ]; then
        echo >&2 "Couldn't fire up sxhkd: $return_val. Exiting!"
        bspc quit && exit 1
    fi
fi

command -v alacritty >/dev/null 2>&1
return_val=$?
if [ $return_val -ne 0 ]; then
    echo >&2 "Can't seem to find the alacritty executable. Exiting!"
    bspc quit && exit 1
fi

for monitor in $(bspc query -M); do
    bspc monitor $monitor -d \~
done

bspc config border_width 2
bspc config window_gap 0
bspc config top_padding 20
bspc config bottom_padding 0
bspc config left_padding 0
bspc config right_padding 0
bspc config single_monocle false
bspc config split_ratio 0.50
bspc config borderless_monocle true
bspc config gapless_monocle true
bspc config focus_follows_pointer true
bspc config remove_disabled_monitors true
bspc config merge_overlapping_monitors true
bspc config pointer_modifier mod1
bspc config pointer_action1 move
bspc config pointer_action2 resize_side
bspc config pointer_action3 resize_corner

bspc config normal_border_color "#44475a"
bspc config active_border_color "#bd93f9"
bspc config focused_border_color "#ff79c6"
bspc config presel_feedback_color "#6272a4"

bspc rule -a Screenkey manage=off
bspc rule -a Zathura state=tiled manage=on

command -v nitrogen >/dev/null 2>&1
return_val=$?
if [ $return_val -ne 0 ]; then
    echo >&2 "Can't seem to find the nitrogen executable"
else
    nitrogen --restore &
    return_val=$?
    if [ $return_val -ne 0 ]; then
        echo >&2 "Couldn't fire up nitrogen: $return_val"
    fi
fi

command -v /usr/lib/xfce-polkit/xfce-polkit >/dev/null 2>&1
return_val=$?
if [ $return_val -ne 0 ]; then
    echo >&2 "Can't seem to find the /usr/lib/xfce-polkit/xfce-polkit executable"
else
    /usr/lib/xfce-polkit/xfce-polkit &
    return_val=$?
    if [ $return_val -ne 0 ]; then
        echo >&2 "Couldn't fire up xfce-polkit: $return_val"
    fi
fi

command -v polybar >/dev/null 2>&1
if [ $? -ne 0 ]; then
    echo >&2 "Can't seem to find the polybar executable. Starting barless!"
    bspc config top_padding 0
else
    $HOME/.config/polybar/launch
    if [ $? -ne 0 ]; then
        echo >&2 "Couldn't fire up polybar: $?"
    fi
fi
