#!/bin/sh
#urn ==============================
#Display text  (maximun a screen full) in a spiral using NiLuJe's fbink and fbdepth
# ʇɟǝןʎdoƆ (ↄ) 2020-11-09 PoP under Creative Commons Attribution-ShareAlike 3.0 Unported License
#2019-11-09 v0.10 initial version
#2019-11-11 v0.20 calculate in pixels instead of characters
#2019-11-13 v0.30 add spacing between each line, 
#                                faked a screenshot resilient ro frame buffer rotations
#Usage:
#turn [-S NUM] [-F FONTNAME] [other] [-T "TEXT"] where
#           -S is the font scaling factor (fbink -S)
#             for example S1 8x8 is upscaled to 24x24
#           -F FONTNAME fixed pixel fontname to use
#           -T "TEXT" ASCII text to print (defaults to an appropriate song from "The Byrds")
#           other parameters to pass to fbink
#dependencies: fbink / fbdepth / fbgrab v1.22.2  from usbnet v0.22 hack 
#                         convert cli from ImageMagick v6.9.11-34  from linkss v0.25 hack 

T="There is a season - turn, turn, turn * And a time to every purpose under heaven * A time to be born, a time to die * A time to plant, a time to reap * A time to kill, a time to heal * A time to laugh, a time to weep * To everything - turn, turn, turn * There is a season - turn, turn, turn * And a time to every purpose under heaven * A time to build up, a time to break down * A time to dance, a time to mourn * A time to cast away stones * A time to gather stones together * To everything - turn, turn, turn * There is a season - turn, turn, turn * And a time to every purpose under heaven * A time of love, a time of hate * A time of war, a time of peace * A time you may embrace * A time to refrain from embracing * To everything - turn, turn, turn * There is a season - turn, turn, turn * And a time to every purpose under heaven * A time to gain, a time to lose * A time to rend, a time to sew * A time for love, a time for hate * A time for peace, I swear it's not too late!                                    " #padded with spaces to prevent truncation
S=0;F="BLOCK";other="";COLOR="BLACK";

while [ "$1" != '' ]; do #parse parameters, no validation!
  case "$1" in
        -S) shift;S=$1;shift;;
        -F) shift;F=$1;shift;;
        -T) shift;T="$1";shift;;
         *) other="$other $1";shift;;
  esac
done

lipc-set-prop com.lab126.powerd preventScreenSaver 1 #disable screen saver
killall -stop cvm # pause framework
fbink -c #clear screen
cd /mnt/us
fbgrab fbgrab.png #grab it

#Query fbink info to get the maximum number screen and font pixel dimensions
#fbink -qe -F block
#FBINK_VERSION='v1.22.2-68-g0c705d5 for Kindle';viewWidth=1072;viewHeight=1448;screenWidth=1072;screenHeight=1448;viewHoriOrigin=0;viewVertOrigin=4;viewVertOffset=4;DPI=300;BPP=8;lineLength=1088;FONTW=32;FONTH=32;FONTSIZE_MULT=1;FONTNAME='BLOCK';glyphWidth=32;glyphHeight=32;MAXCOLS=33;MAXROWS=45;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;
# fbink -qe -F ibm
#FBINK_VERSION='v1.22.2-68-g0c705d5 for Kindle';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;[root@kindle root]#

fbdepth -q -r 3 #portrait Top
i="$(fbink -S $S -F $F -qe)"
MC=$(echo $i  | sed 's/.*screenWidth=\(.*\)*/\1/')
MC=${MC%;screenHeight=*}  #screen width
MR=$(echo $i  | sed 's/.*screenHeight=\(.*\)/\1/')
MR=${MR%;viewHoriOrigin=*}  #screen height
P=$MC;L=$MR #portrait and landscape screen size
MC=$(echo $i  | sed 's/.*FONTW=\(.*\)*/\1/')
MC=${MC%;FONTH=*}  #character width
MR=$(echo $i  | sed 's/.*FONTH=\(.*\)/\1/')
MR=${MR%;FONTSIZE_MULT=*}  #character height
CW=$MC;CH=$MR #character size
X=0; Y=0; #initial column and row position
ROT=3;TT="";TI=1
PMGL=0;PMGR=0; #Portrait Margin Left, Margin Right
LMGL=0;LMGR=0; #Landscape Margin Left, Margin Right
SPACING=$(($CH/3)) #tried 0 or $((2*$CH)) or $(($CH/2)) and unsure

while :; do

  case "$ROT" in
     3) #Portrait Top
         NUMCHAR=$(($P-$PMGL-$PMGR))
         LMGL=$(($LMGL+$CH))
         X=$PMGL
         LMGL=$(($LMGL+$SPACING))
         ;;
     0) #Landscape Right 
         NUMCHAR=$(($L-$LMGL-$LMGR))
         PMGR=$(($PMGR+$CH))
         X=$LMGL
         PMGR=$(($PMGR+$SPACING))
         ;;
     1) #Portrait Bottom 
         NUMCHAR=$(($P-$PMGR-$PMGL))
         LMGR=$(($LMGR+$CH))
         X=$PMGR
         LMGR=$(($LMGR+$SPACING))
         ;;
     2) #Landscape Left 
         NUMCHAR=$(($L-$LMGR-$LMGL))
         PMGL=$(($PMGL+$CH))
         X=$LMGR
         PMGL=$(($PMGL+$SPACING))
         ;;
  esac
  NUMCHAR=$((($NUMCHAR-$SPACING)/CW)) #to truncate 1 character when exactly fits, rounding the corner

  if [ $TI -gt ${#T} ] || [ $NUMCHAR -lt 1 ]
  then
    #originally needed to take a physical photo since frame buffer rotations mess up screenshots
    #Revive the Kindle
    fbdepth -q -r 3
    killall -cont cvm #resume framework activity
    lipc-set-prop com.lab126.powerd preventScreenSaver 0  #enable screen saver
    exit
  fi

  TT=$(expr substr "$T" $TI $NUMCHAR)  #next bytes of T to print (Warning: ASCII only. Other Unicodes might be several bytes)
 #echo "ROT=$ROT X=$X Y=$Y P= $P PGML=$PMGL PGMR=$PMGR L=$L LMGL=$LMGL LMGR=$LMGR TI=$TI $NUMCHAR $TT"
  if [ "$TT" != "" ]
  then

    fbink  -g file=fbgrab.png #paste the previous grab
    fbink -q -O -X $X -Y $Y -S $S -C $COLOR -F $F "$TT" #overly the text to write
    fbgrab fbgrab.png #grab the overlay

    TI=$((TI+NUMCHAR))
    ROT=$(($ROT+1))
    if [ $ROT -eq 4 ]; then ROT=0; fi
    fbdepth -q -r $ROT  #turn cockwise (at least on PW3)
    ./linkss/bin/convert -rotate -90 fbgrab.png fbgrab.png #prepage the next overlay graphic
    if [ $ROT -eq 3 ];  then Y=$(($Y+$CH+$SPACING)); fi
  fi

done