#!/usr/bin/bash

function showmons
{
xrandr --current|grep -w connected |sed 's/[x,+]/ /g'
}

function getMaX
{
xrandr --current|grep -w connected |sed 's/[x,+]/ /g'|awk 'BEGIN{x=0}{
t=$3
if ( x < t ) { x=t }

} END{print x}'
}

function getMaY
{
xrandr --current|grep -w connected |sed 's/[x,+]/ /g'|awk 'BEGIN{y=0}{
t=$4
if ( y < t ) { y=t }

} END{print y}'
}

function totX
{
xrandr --current|grep -w connected |sed 's/[x,+]/ /g'|awk 'BEGIN{x=0}{
x=x+$3
} END{print x}'
}

function totY
{
xrandr --current|grep -w connected |sed 's/[x,+]/ /g'|awk 'BEGIN{y=0}{
y=y+$4
} END{print y}'
}

screen0()
{
xrandr --listactivemonitors |grep 0:|awk '{print $1,$NF}'
}

screen1()
{
xrandr --listactivemonitors |grep  1:|awk '{print $1,$NF}'
}

function showit
{

cat <<EOF

usage:
screens next to each other, top-side aligned :
  `basename $0` <screen 0> left-of <screen 1> [ scale 0 ] [ scale 1 ]
  OR
  `basename $0` <screen 1> left-of <screen 0> [ scale 0 ] [ scale 1 ]

screens on top of each other, left side aligned.
  `basename $0` <screen 0> top-of <screen 1> [ scale 0 ] [ scale 1 ]
  OR
  `basename $0` <screen 1> top-of <screen 0> [ scale 0 ] [ scale 1 ]

  scale # is optional, it is a float value like : 0.6 1.0 2.0
  it makes your hi DPI screen usable in combination with 
  less dpi HD monitors ( e.g. a 2550x1440 220dpi with a 1920x1080 92dpi )
EOF
exit 1

}


function connected
{
xrandr --current|grep -w connected | sed 's/[x,+]/ /g'
}

function setfb
{
case $placem in 
left-of)
echo $sc0 $placem $sc1, 1 sec.....
totwidth=`totX`
tothigh=`getMaY`
;;
top-of)
echo $sc0 $placem $sc1, 1 sec.....
totwidth=`getMaX`
tothigh=`totY`
;;
esac
echo xrandr -d :0 --fb ${totwidth}x${tothigh}
xrandr -d :0 --fb ${totwidth}x${tothigh}
}
export widsc0=0
export highsc0=0
export scal0=1
export scal1=1
export fbx=0
export fby=0

function setscalwxh
{
widsc0=`xrandr --current|grep -A 5 -e $sc0 |grep -e \* |sed 's/x/ /g'|awk -v fact=$scal0  '{print $1 * fact }'`
highsc0=`xrandr --current|grep -A 5 -e $sc0|grep  -e \* |sed 's/x/ /g'|awk -v fact=$scal0  '{print $2 * fact }'`
widsc1=`xrandr --current|grep -A 5 -e $sc1 |grep -e \* |sed 's/x/ /g'|awk -v fact=$scal1  '{print $1 * fact }'`
highsc1=`xrandr --current|grep -A 5 -e $sc1|grep  -e \* |sed 's/x/ /g'|awk -v fact=$scal1  '{print $2 * fact }'`

case $placem in
left-of)
# screen next to each other, sum of x width and y = highest y value.
fbx=$(( $widsc0 + $widsc1 ))
fby=$highsc0
[ $fby -lt $highsc1 ] && fby=$highsc1
;;
top-of)
# screen above each other, sum of y height and widest x = highest x value.
fby=$(( $highsc0 + $highsc1 ))
fbx=$widsc0
[ $fbx -lt $widsc1 ] && fbx=$widsc1
;;
*)
showit
exit 1
;;
esac



cat <<EOF
===================
scal0 $scal0
widsc0 $widsc0
highsc0 $highsc0
scal1 $scal1
widsc1 $widsc1
highsc1 $highsc1
fbx  $fbx
fby  $fby
===================
EOF
}

function doit
{ 

cat <<EOF
this is hard 
0 - check if monitors exist.
1 - scale the monitors. assume 1.0 if not given.
2 - plase second monitor according to right-of or below or same-as

note: first monitor mentioned is always at 0x0

EOF


sc0ok=`xrandr --listactivemonitors| grep -c -w $sc0 `
sc1ok=`xrandr --listactivemonitors| grep -c -w $sc1 `
echo sc0ok $sc0ok
echo sc1ok $sc1ok

if [ $(($sc0ok - $sc1ok )) -eq 0 ]
then 
echo monitors seem to exist and to be active
else
echo monitors do not exist or are NOT active
xrandr --listactivemonitors
exit 4
fi

echo display status :
connected



 echo $sc0 is always at position x,y = 0,0 
 echo $sc1 is either at position x,y = left-of-width, 0 OR    0,top-of-height

setscalwxh
case $placem in 
left-of)
newx=$widsc0
newy=0
;;
above|top-of)
newx=0
newy=$highsc0
;;
esac

echo displaying command to exec. Hit return to execute, otherwise ctrl-C to break.
echo xrandr --fb ${fbx}x${fby} --output $sc0 --pos 0x0 --scale ${scal0}x$scal0 --output $sc1 --scale ${scal1}x$scal1 --pos ${newx}x${newy} 
read shallweornot
echo executing reset both monitors to scale 1x1.
sleep 1
xrandr --fb 2560x2520 --output HDMI-1 --pos 0x0 --scale 1.0x1.0 --output eDP-1 --scale 1.0x1.0 --pos 0x1080
echo executing desired config
xrandr --fb ${fbx}x${fby} --output $sc0 --pos 0x0 --scale ${scal0}x$scal0 --output $sc1 --scale ${scal1}x$scal1 --pos ${newx}x${newy} 

}

case $# in
0)
showit
doit
;;
3|4|5)
# either we just place monitors, or also apply scaling on 1 or even 2 (4 en 5 params)
;;
esac

sc0=$1
placem=$2
sc1=$3
scal0="1.0"
scal1="1.0"
[ $# -gt 3 ] && scal0=$4
[ $# -gt 4 ] && scal1=$5

doit

