#!/bin/sh
#mondrianx v0.30 ==============================
# ʇɟǝןʎdoƆ (ↄ) 2020-02-03 PoP under Creative Commons Attribution-ShareAlike 3.0 Unported License
# 2020-02-02 v0.10 initial release
# 2020-02-03 v0.20 replace RANDOM shell variable (not available on the K3) by /dev/urandom
# 2020-02-04 v0.30 variant to draw only the lines at on the perimeter of the rectangles
#
#Usage: mondrianx
#
#dependeny: fbink 1.21.0 minimum, to support -k parameter.

#function to return a random number between X and Y inclusively
#usage ran x y
ran() {
local X=$1 Y=$2	
DIFF=$((Y-X+1))
set $(hexdump -n2 -e '1/2 "%6u"' /dev/urandom); local RANDOM=$1 # 1 string 2 bytes long 6 characters unsigned numbers
R=$(($(($RANDOM%$DIFF))+X))
echo $R
}

#quiet the Kindle
lipc-set-prop com.lab126.powerd preventScreenSaver 1
killall -stop cvm # pause framework

#Using the query parameter, parse fbink 1.7.0.1.0 info to get the width and height of the device
#viewWidth=1072;viewHeight=1448;screenWidth=1072;screenHeight=1448;viewHoriOrigin=0;viewVertOrigin=4;viewVertOffset=4;DPI=300;BPP=8;lineLength=1088;FONTW=24;FONTH=24;FONTSIZE_MULT=3;FONTNAME='IBM';glyphWidth=8;glyphHeight=8;MAXCOLS=44;MAXROWS=60;isPerfectFit=0;FBID='mxc_epdc_fb';USER_HZ=100;penFGColor=0;penBGColor=255;deviceName='PaperWhite 3';deviceId=513;deviceCodename='Muscat';devicePlatform='Wario';isKoboNonMT=0;ntxBootRota=0;ntxRotaQuirk=0;isNTX16bLandscape=0;currentRota=3;canRotate=0;

i="$(fbink -qe)"
W=$(echo $i  | sed 's/.*viewWidth=\(.*\)*/\1/')
W=${W%;viewHeight=*}  #view width
H=$(echo $i  | sed 's/.*viewHeight=\(.*\)/\1/')
H=${H%;screenWidth=*}  #view height

fbink -c #clear the screen

#draw 50 rectangles
for i in `seq 50`
do

#get a random area within screen boundaries 
TOP=$(ran 1 $H)
HEIGHT=$(($H - $(ran $TOP $H)))
LEFT=$(ran 1 $W)
WIDTH=$(($W - $(ran $LEFT $W)))

#draw that rectangle outline in a random background color
COL=$(ran 0 15)
case $COL in
    0) COL="BLACK" ;;
  15) COL="WHITE" ;;
    *) COL="GRAY""$(printf  "%1x" $COL)" ;;
esac

BOLDNESS=$(ran 4 12) #size of the outline
fbink -k top=$TOP,left=$LEFT,width=$WIDTH,height=$BOLDNESS -B $COL
fbink -k top=$TOP,left=$LEFT,width=$BOLDNESS,height=$HEIGHT -B $COL
fbink -k top=$(($TOP+$HEIGHT-$BOLDNESS)),left=$LEFT,width=$WIDTH,height=$BOLDNESS -B $COL
fbink -k top=$TOP,left=$(($LEFT+$WIDTH-$BOLDNESS)),width=$BOLDNESS,height=$HEIGHT -B $COL

#screenshot
done

#revive the Kindle
killall -cont cvm
lipc-set-prop com.lab126.powerd preventScreenSaver 0
