#!/bin/bash

trap '[[ -n "$reload" ]] && herbstclient detect_monitors && systemctl restart --user panel-handler@$DISPLAY' EXIT

if [[ -z "$*" ]]
then
	for mon in $(xrandr | grep -E 'disconnected [[:digit:]]{3,4}' | awk '{ print $1}')
	do
		xrandr --output "$mon" --off
		reload=yes
	done
fi

MONITORS=$(xrandr | grep -v LVDS1 | grep ' connected' | awk '{print $1}')

[[ -z "$MONITORS" ]] && exit

CONFIG="$(echo "$MONITORS" | rofi -dmenu)"

[[ -z "$CONFIG" ]] && exit

DIRECTION="$(echo -e 'left\nright' | rofi -dmenu)"

[[ -z "$DIRECTION" ]] && exit

xrandr --output "$CONFIG" "--$DIRECTION-of" LVDS1 --auto
reload=yes

