#!/bin/bash

# Resources
resources=( '/etc/X11/xinit/.Xresources' "$HOME/.Xresources" )
modmaps=( '/etc/X11/xinit/.Xmodmap' "$HOME/.Xmodmap" )

# Set DPI
xrandr --dpi 95

for r in "${resources[@]}"; do
	if [[ -f "$r" ]]; then
		xrdb -merge "$r"
	fi
done

for m in modmaps; do
	if [[ -f "$m" ]]; then
		xmodmap "$m"
	fi
done

# Common services
common_services+=( pulseaudio lockd )
for i in "${common_services[@]}"; do
	service "$i" start
done

# Fonts
xset +fp /usr/share/fonts/local

# Screensaver settings
xset -dpms
xset s 300
xset s noblank
xset s noexpose

# Solid black root window
# chameleon is available at https://github.com/fbt/misc
chameleon -C '#151515'

# Set a wallpaper if it exists
if [[ -f "$HOME/wallpaper.png" ]]; then
	chameleon "$HOME/wallpaper.png"
fi

# Input settings
setxkbmap -layout "us,ru" \
	       -option "lv3:menu_switch,grp:caps_toggle,grp_led:caps,compose:ralt,terminate:ctrl_alt_bksp"
xmodmap -e 'remove lock = Caps_Lock' \
        -e 'keycode 135 = KP_Insert' \
        -e 'keycode 49 = Escape asciitilde' \
        -e "pointer = 3 2 1"
xinput --set-prop 22 'libinput Accel Speed' -0.92
#xinput set-button-map 17 3 2 1 4 5 6 7 8 9

# Start a dbus session
if ! [[ "$DBUS_SESSION_BUS_ADDRESS" ]]; then
	export $(dbus-launch)
fi

# Some nvidia tweaks
export VDPAU_NVIDIA_NO_OVERLAY=1
#export __GL_THREADED_OPTIMIZATIONS=1
nvidia-settings -l
nvidia-settings -a InitialPixmapPlacement=2

# Warmer colours
xsct 4500

# Custom functions
services() {
	for i in "${session_services[@]}"; do
		service "$i" "$1"
	done
}

return 0