#!/bin/sh

# query output information with EDID using xrandr, format it into single line, match and apply rules.

usage() {
	printf "usage: autoxrandr [--dump]\n"
	exit 2
}

match_line() {
	case "$1" in

		# Tygr, větší monitor Samsung
		("HDMI1 connected 00ffffffffffff004c2dcb09353436301a19010380351e782a4ba1a359559b260e5054bfef80714f81c0810081809500a9c0b3000101023a801871382d40582c4500132b2100001e000000fd00384b1e5111000a202020202020000000fc00533234433435300a2020202020000000ff00483446473630363632360a2020016802010400023a80d072382d40102c4580132b2100001e011d007251d01e206e285500132b2100001e011d00bc52d01e20b8285540132b2100001e8c0ad090204031200c405500132b210000188c0ad08a20e02d10103e9600132b210000180000000000000000000000000000000000000000000000000000000000000000000e")
		xrandr --output ${1%% *} --left-of LVDS1
		;;

		# Doma, ViewSonic
		("HDMI1 connected 00ffffffffffff005a635120010101010312010380311d782ee525a65444a125145054bfef80b300a94081808140714f01010101010121399030621a274068b03600ef231100001c000000ff005154533038303330353833320a000000fd00324b1e5211000a202020202020000000fc00564132323236772d330a2020200072")
		xrandr --output ${1%% *} --above LVDS1 --auto
		;;

		# TE2000, MultiSync E223W
		("DP1 connected 00ffffffffffff0038a3ef680000000031170104a52f1e78e23cf5a4554c9b250e5054bfef8081c0814081808bc095009040b300714f21399030621a274068b03600da281100001c000000fd00384b1f5311000a202020202020000000fc0045323233570a20202020202020000000ff00335a33303038363854420a202000fa")
		#xrandr --output ${1%% *} --right-of LVDS1 --auto --rotate left
		xrandr --output ${1%% *} --above LVDS1 --auto --rotate normal
		;;

		# Recombee, Samsung, backend
		("DP1 connected 00ffffffffffff004c2d737151324530121f0104b53c22783b2f95a9544ca3260f5054bfef80b300810081c081809500a9c0714f010165730050a0a034500820680855512100001a000000fd00304b70701e010a202020202020000000fc004c53323741363030550a202020000000ff00484e4d523430323538350a202001d1020324f146901f041312032309070783010000e305c000681a00000101304b00e3060501023a801871382d40582c450055512100001e565e00a0a0a029503020350055512100001a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000066")
		xrandr --output ${1%% *} --above LVDS1 --auto
		;;

		# Recombee, Samsung, research
		("DP1 connected 00ffffffffffff004c2d73714a5658430e200104b53c22783a2f95a9544ca3260f5054bfef80b300810081c081809500a9c0714f010165730050a0a034500820680855512100001a000000fd00324b1e701e000a202020202020000000fc004c53323741363030550a202020000000ff0048345a543430303931390a202001ef02031bf146901f041312032309070783010000e305c000e3060501023a801871382d40582c450055512100001e565e00a0a0a029503020350055512100001a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006e")
		xrandr --output ${1%% *} --above LVDS1 --auto
		;;

		# Recombee, Samsung, open space
		("DP1 connected 00ffffffffffff004c2d737151324530121f0104b53c22783a2f95a9544ca3260f5054bfef80b300810081c081809500a9c0714f010165730050a0a034500820680855512100001a000000fd00324b1e701e000a202020202020000000fc004c53323741363030550a202020000000ff00484e4d523430323536360a2020012402031bf146901f041312032309070783010000e305c000e3060501023a801871382d40582c450055512100001e565e00a0a0a029503020350055512100001a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006e")
		xrandr --output ${1%% *} --above LVDS1 --auto
		;;

		# Recombee, Samsung, research new
		("DP1 connected 00ffffffffffff004c2d73714a5658430d200104b53c22783a2f95a9544ca3260f5054bfef80b300810081c081809500a9c0714f010165730050a0a034500820680855512100001a000000fd00324b1e701e000a202020202020000000fc004c53323741363030550a202020000000ff0048345a543330333335360a202001f302031bf146901f041312032309070783010000e305c000e3060501023a801871382d40582c450055512100001e565e00a0a0a029503020350055512100001a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006e")
		xrandr --output ${1%% *} --above LVDS1 --auto
		;;

		# Aminda, Asus
		("HDMI1 connected 00ffffffffffff000469d223b89401000b17010380331d782ad945a2554da027125054b7ef00d1c0814081809500b300714f81c08100023a801871382d40582c4500fd1e1100001e000000ff0044334c4d54463130333630380a000000fd00324b185311000a202020202020000000fc00415355532056533233390a20200191020322714f0102031112130414050e0f1d1e1f10230917078301000065030c0010008c0ad08a20e02d10103e9600fd1e11000018011d007251d01e206e285500fd1e1100001e011d00bc52d01e20b8285540fd1e1100001e8c0ad090204031200c405500fd1e11000018000000000000000000000000000000000000000000e9")
		xrandr --output ${1%% *} --above LVDS1 --auto
		;;
	esac
}

dump_edid() {
xrandr --verbose | awk '
/^Screen /{ next }
/^[^ \t]/{ cur_out=$1; out_conn[$1]=$2; next; }
/^\tEDID: $/{ edid=1; next }
edid && /^\t\t[0-9a-f]{32}$/{ out_edid[cur_out]=out_edid[cur_out] $1; next }
{ edid=0 }
END{ for(out in out_conn){ printf("%s %s %s\n", out, out_conn[out], out_edid[out]) } }
'
}

case $# in
	(0) ;;
	(1)
		if test "x--dump" = "x$1"; then
			dump_edid
			exit $?
		else
			usage
		fi
		;;
	(*) usage;;
esac

dump_edid | while IFS= read line; do
	match_line "$line"
done