#!/bin/bash

function primary() {
    xrandr | grep primary | awk '{print $1}'
}

displays=$(xrandr --listactivemonitors | grep -v Monitors | awk '{print $NF}')
case "$1" in
    "auto")
        echo "$displays" | while read display; do
            xrandr --output $display --auto
        done
        ;;
    "connected")
        echo "$displays"
        ;;
    "lemonbar")
        echo "DISP ${displays[@]}" | sed "s/ /:/g"
        ;;
    *)
        # -F Fix strings, -x exactly, -q quiet
        if echo "$displays" | grep -Fqx "$1"; then
            case "$2" in
                "off")
                    action="--off"
                    ;;
                "mirror")
                    action="--same-as $(primary)"
                    ;;
                "left")
                    action="--left-of $(primary) --auto"
                    ;;
                "right")
                    action="--right-of $(primary) --auto"
                    ;;
                *)
                    exit 1
                    ;;
            esac
            xrandr --output $1 $action
        fi
esac

