#!/usr/bin/bash

#!/bin/bash
# asnmon autoscales your monitor(s)

# this sets the scaling... something in between 5 and 8 should be sort of ok.
# it is the number of virtual pixels per mm screen you have, per monitor.
# you can give it any number, it wont work limitless... you have been warned....

factor=$1

set  -- `xrandr -q |grep -e \* | sed 's/x/ /g'`
echo $*
natX=$1
natY=$2
echo natX $natX natY $natY

# echo  printing  x y pix / mm  
#xrandr -q |grep -w -e connected|sed 's/[x,+]/ /g'|awk '{x=NF-1; print $3 / $x, $4/ $NF , 2560 / $x, 1440 / $NF}'
#echo  printing  x y width height
#xrandr -q |grep -w -e connected|sed 's/[x,+]/ /g'|awk '{x=NF-1; print $3 , $4 , 1 * $x,  1 * $NF}'

echo  printing  multiple x y pix / mm  
# we need this temp script hack.....
echo "declare -A mons" > /tmp/mons.temp
for monitor in 	`xrandr -q |grep -w connected |awk '{print $1}'`
do
# while read aline...  the while creates a new environment, which emptys ALL variables once you exit it....
# meaning you cant do anything usefull with the result of the loop. the bash acronym actually
# means brain as shit helpfull 

xrandr -q |grep -w  -e connected |grep -w $monitor | while read aline
do
echo "$aline" |sed 's/[x,+]/ /g'|awk -v monitor=$monitor '{x=NF-1; print monitor, $3 , $4, 1 * $x , 1 * $NF }'

echo finding native resolutions for $monitor 
set  -- `xrandr -q |grep -A 5 -w $monitor |grep -e \* | sed 's/x/ /g'`
natX=$1
natY=$2
echo natX $natX natY $natY

set  -- ` echo "$aline" |sed 's/[x,+]/ /g'|awk -v monitor=$monitor '{x=NF-1; print monitor, $3 , $4, 1 * $x , 1 * $NF }'` `echo $natX $natY`
x=1
echo params : $# $*
# params : eDP-1 1536 864 294 165 2560 1440
# flds are: curx cury mmx mmy natx naty

cat <<EOF >> /tmp/mons.temp
mons[$monitor,name]=$1
mons[$monitor,curx]=$2
mons[$monitor,cury]=$3
mons[$monitor,mmx]=$4
mons[$monitor,mmy]=$5
mons[$monitor,natx]=$6
mons[$monitor,naty]=$7
EOF
done # while read aline
done # for monitor
# sourcing the array script we just created, since bash is a fuck up shell processor pretending to be a programming lang,
# but it is not, really...
chmod 755 /tmp/mons.temp
. /tmp/mons.temp
echo trying to print the tables
for name in `xrandr -q|grep -w connected|awk '{print $1}'`
do
echo name $name
for part in name curx cury mmx mmy natx naty
do
echo $name: $part : ${mons[$name,$part]}
done
done

for name in `xrandr -q|grep -w connected|awk '{print $1}'`
do
echo name $name factor $factor
for part in mmx mmy 
do
# echo $name: $part : ${mons[$name,$part]} new cur${part##mm} $(( ${mons[$name,$part]}  * $factor ))
echo FP $name: $part : ${mons[$name,$part]} new cur${part##mm} \
` echo |awk -v mmu=${mons[$name,$part]}  -v fact=$factor '{printf(" mmu %5d fact %4.1f  %6.2f \n",mmu,fact,mmu*fact) }'`
curpart=cur${part##mm} 
# newu=$(( ${mons[$name,$part]}  * $factor )
newu=`echo |awk -v mmu=${mons[$name,$part]}  -v fact=$factor '{print mmu*fact }'`

natu=nat${part##mm}
echo $part $natu ${mons[$name,$natu]} $newu |awk '{printf(" %8s %8s %4d %4d %2.3f\n",$1,$2,$3,$4,$4/$3)}'
done
done
