#!/usr/bin/env sh

# Checks if the given command exists
function has_command() {
    command -v $1 >/dev/null
}

# Runs the program in background if it exists
function background_run() {
    has_command $1 && $* &
}

# Runs the program if it exists
function run() {
    has_command $1 && $*
}

function debug() {
    echo $* >> /tmp/xinitrc_debug
}

if [[ -f ~/.nix-profile/etc/profile.d/hm-session-vars.sh ]]; then
  # Make sure to load session vars like XDG_DATA_DIRS, needed to have icons working properly for some Nix-based gui apps.
  #
  # NOTE: the file is guarded against reloads, unset __HM_SESS_VARS_SOURCED to be able to re-source it.
  source ~/.nix-profile/etc/profile.d/hm-session-vars.sh
fi

# -----------------------------------------------

# Setup mouse options (see `man 4 libinput)
#> Enable tap to click
xinput set-prop 'SynPS/2 Synaptics TouchPad' 'libinput Tapping Enabled' 1
#> Enable 2 finger tap for middle click
xinput set-prop 'SynPS/2 Synaptics TouchPad' 'libinput Tapping Button Mapping Enabled' 0 1

# Setup keyboard mapping
setxkbmap -config ~/.dot/gui/xkbmap.config

# Setup X resources
~/.bin-gui/load_xresources

# no beep
xset -b

# Load all ssh keys
eval `ssh-agent`
ssh-add `find ~/.ssh -name "id_*" -a \! -name "*.pub"`

# Warmer screen temperature at night
# Locations: (<lat>:<long>)
# - San Marcos: 29.9:-97.9
# - Paris: 48.8:2.3
background_run ~/.nix-profile/bin/redshift-gtk -l 48.8:2.3

background_run nm-applet # network selector
background_run dunst # notification daemon
background_run flameshot # screenshot tool
background_run copyq # clipboard manager

background_run ~/.bin-gui/desktop--restart-panels

run libinput-gestures-setup start
# NOTE: to reload config, use: libinput-gestures-setup restart

# Run the compositor
[[ -f ~/.config/picom.config ]] && run picom --config ~/.config/picom.config -b
