#!/bin/bash

trap '[[ -n "$reload" ]] && herbstclient reload' EXIT

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

primary=$(xrandr -q | awk '/primary/ { print $1 }')
monitors=$(xrandr -q | grep -v "$primary" | grep ' connected' | awk '{print $1}')

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

monitor="$(echo "$monitors" | rofi -dmenu -p "Choose a monitor")"

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

direction="$(echo -e 'left-of\nright-of\nabove\nbelow\noff' | rofi -dmenu -p "Where you want it")"

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

declare -a action
if [[ "$direction" = off ]]
then
    action=(--off)
else
    action=("--${direction}" "$primary" --auto)
fi

xrandr --output "$monitor" "${action[@]}"
reload=yes

