#!/bin/bash

all_monitors=$(xrandr -q | awk '/ connected/ {print $1}')
default_screen=$(xrandr | awk '/ connected/ {print $1;exit;}')
extra_monitors=$(xrandr -q | awk '/ connected/ {print $1}' | grep -v $default_screen)

#First, configure stuff with xrandr
#Get user path
[[ $(who) != "" ]] && USER=$(who | grep :0\) | cut -f 1 -d ' ') || \
USER=$(echo /home/* | cut -f 3 -d '/')

export DISPLAY=:0.0
export XAUTHORITY=/home/$USER/.Xauthority

for monitor in $extra_monitors; do
  prev_mon=$(xrandr | awk '/ connected/ {print $1}' | grep -B1 "^$monitor" | grep -vE "^$monitor|^--$")
  xrandr --output $monitor \
    --auto \
    --right-of $prev_mon
done