#!/bin/sh

prefix="/usr"
exec_prefix="${prefix}"
xrdb="xrdb"
xinitdir="/etc/X11/xinit"
xmodmap="xmodmap"

userresources="$HOME/.Xresources"
usermodmap="$HOME/.Xmodmap"
sysresources="$xinitdir/.Xresources"
sysmodmap="$xinitdir/.Xmodmap"

# merge in defaults and keymaps

[ -x /usr/bin/cpp ] && xinit_nocpp_opt="" || xinit_nocpp_opt="-nocpp"
[ -f "$sysresources" ] && "$xrdb" "$xinit_nocpp_opt" -merge "$sysresources"
[ -f "$sysmodmap" ] && "$xmodmap" "$sysmodmap"
[ -f "$userresources" ] && "$xrdb" -nocpp -merge "$userresources"
[ -f "$usermodmap" ] && "$xmodmap" "$usermodmap"

# source xinitdrc.d script files
[ -d "$xinitdir"/xinitrc.d ] && {
	for f in "$xinitdir/xinitrc.d"/?*.sh ; do
		[ -x "$f" ] && . "$f"
	done
	unset f
}

# xrandr mode for laptop display (framework 13in), target 1920x1280 resolution
dispmanage_modeline=$(cvt 1920 1280 60 | sed -n '2p' | sed -E "s/^Modeline //g")
dispmanage_mode_id=$(printf "%s" "$dispmanage_modeline" | sed -E "s/^([^ ]+)\s.*$/\1/g")
printf "%s" "$dispmanage_modeline" | xargs xrandr --newmode
printf "%s" "$dispmanage_mode_id" | xargs xrandr --addmode eDP
xrandr --output eDP --mode "1920x1280_60.00"

# start window manager
exec dwm
