#!/bin/sh
#turn ==============================
#Display text (maximun a screen full) in a spiral using NiLuJe's tools
# ʇɟǝןʎ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
#2019-11-17 v0.40 support K3 rotations (vs originally supported only PW3 & PW4)
#2019-11-20 v0.50 stop argument parsing within TEXT string
#Usage:
#turn [-S NUM] [-F FONTNAME] [other] [-T "TEXT"] where
#           -S is the font scaling factor
#           -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


#Query fbink info to get the dimensions and orientation
i="$(fbink -S $S -F $F -e)" #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;isNTi16bLandscape=0;currentRota=3;canRotate=0;
i1=$(echo $i  | sed 's/.*screenWidth=\(.*\)*/\1/')
i1=${i1%;screenHeight=*}  #screen width
i2=$(echo $i  | sed 's/.*screenHeight=\(.*\)/\1/')
i2=${i2%;viewHoriOrigin=*}  #screen height
P=$i1;L=$i2 #portrait and landscape screen size
i1=$(echo $i  | sed 's/.*FONTW=\(.*\)*/\1/')
i1=${i1%;FONTH=*}  #character width
i2=$(echo $i  | sed 's/.*FONTH=\(.*\)/\1/')
i2=${i2%;FONTSIZE_MULT=*}  #character height
CW=$i1;CH=$i2 #character size
i2=$(echo $i  | sed 's/.*currentRota=\(.*\)/\1/')
i2=${i2%;canRotate=*}  #current rotation
MODEL=$i2 #K3=0, PW3=3 PW4=3 
X=0; Y=0; #initial column and row position
ROT=$MODEL;TT="";TI=1
PL=0;PR=0; #portrait left and right margins
LL=0;LR=0; #landscape portrait left and right margins
SPACING=$(($CH/3)) #tried 0 or $((2*$CH)) or $(($CH/2)) $(($CH/3)) and unsure which is best
ORIENTATION=0
fbdepth -q -r $ROT #portrait Top
fbink -qc #clear screen
cd /mnt/us
fbgrab fbgrab.png #grab it

while :; do

  case "$ORIENTATION" in
     0) #portrait top
         CHARS=$(($P-$PL-$PR))
         LL=$(($LL+$CH))
         X=$PL
         LL=$(($LL+$SPACING))
         ;;
     1) #landscape right 
         CHARS=$(($L-$LL-$LR))
         PR=$(($PR+$CH))
         X=$LL
         PR=$(($PR+$SPACING))
         ;;
     2) #portrait bottom 
         CHARS=$(($P-$PR-$PL))
         LR=$(($LR+$CH))
         X=$PR
         LR=$(($LR+$SPACING))
         ;;
     3) #landscape left 
         CHARS=$(($L-$LR-$LL))
         PL=$(($PL+$CH))
         X=$LR
         PL=$(($PL+$SPACING))
         ;;
  esac
  CHARS=$((($CHARS-$SPACING)/$CW)) #to truncate 1 character when exactly fits, rounding the corner

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

  TT=$(expr substr "$T" $TI $CHARS)  #next bytes of T to print (Warning: ASCII only. Other Unicodes might be several bytes)
  #echo "MODEL=$MODEL ORIENTATION=$ORIENTATION ROT=$ROT X=$X Y=$Y P=$P PL=$PL PR=$PR L=$L LL=$LL LR=$LR S=$S TI=$TI CHARS $TT"
  if [ "$TT" != "" ]
  then

    fbink -q -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 Thanks NiLuJe for "--" stopping the parsing
    fbgrab fbgrab.png #grab the overlay

    TI=$((TI+CHARS))
    ORIENTATION=$(($ORIENTATION+1))
    if [ $ORIENTATION -eq 4 ]; then ORIENTATION=0; fi

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

done