#!/bin/sh

userresources=$HOME/.Xresources
usermodmap=$HOME/.Xmodmap
sysresources=/etc/X11/xinit/.Xresources
sysmodmap=/etc/X11/xinit/.Xmodmap

# merge in defaults and keymaps

if [ -f $sysresources ]; then
    xrdb -merge $sysresources
fi

if [ -f $sysmodmap ]; then
    xmodmap $sysmodmap
fi

if [ -f "$userresources" ]; then
    xrdb -merge "$userresources"
fi

if [ -f "$usermodmap" ]; then
    xmodmap "$usermodmap"
fi

# start some nice programs

if [ -d /etc/X11/xinit/xinitrc.d ] ; then
 for f in /etc/X11/xinit/xinitrc.d/?*.sh ; do
  [ -x "$f" ] && . "$f"
 done
 unset f
fi

#NixOS does not setup the user's D-Bus Daemon, so doing it explicitly here
if [ -z "$DBUS_SESSION_BUS_ADDRESS" ]; then
    eval $(dbus-launch --exit-with-session --sh-syntax)
fi
syustemctl --user import-environment DISPLAY XAUTHORITY
if command -v dbus-update-activation-environment >/dev/null 2>&1; then
    dbus-update-activation-environment DISPLAY XAUTHORITY
fi

# Setting any of these parameters causes issues when trying to use xrandr
# while on offload mode.  Specifically, the prover named NVIDIA-G0 sets up
# the system to expect reverse-sync mode.  Using --auto also does the same
# for some reason.  Basically any of these commands are not needed.
# xrandr --setprovideroutputsource modesetting NVIDIA-0
# xrandr --setprovideroutputsource 0 1
# xrandr --auto
# autorandr --change

# Speed when holding down key
xset r rate 300 50
xserverauthfile=$XAUTHORITY # Stop the accumulation of ~/.serverauth.### files

# Need to source environment before starting fcitx5 so that it can
# read the *_IM_MODULE and XMODIFIER variables.
source ~/.config/zsh/.zshenv
autorandr -c -d "default"
export TERMINAL=/usr/local/bin/st

# Start important daemons
kill $(pidof fcitx5) 2>/dev/null
fcitx5 -d
picom -b
sxhkd &
killall emacs 2>/dev/null
emacs -daemon

~/.config/autorandr/postswitch
dwmblocks &
exec dwm

