#!/bin/bash
#
#  This script calculates a value from 0-100 if a star comes near the ship..
#  Then the right sensor beneath the windscreen will light up. (like a PDC)
#

if [ "$1" = "" ] || [ "$2" = "" ]; then
  echo -e "\n\e[31m  Can only be started from within conky!\e[m\n"
  exit
fi

# Get start stop frames
Rfrstart=$1
Rfrstop=$2
x=$3

tmpfile="./tempfiles/star-sensor-right"
if [ ! -f "$tmpfile" ]; then
  echo 1 > "$tmpfile"
fi


# Get space frame number
if [ -f "./images/space/current/conky_slideshow_index" ]; then
   Rspace_frame=$(cat "./images/space/current/conky_slideshow_index")
fi

# Debug
#Rfrstart=1540
#Rfrstop=1580

# All output in english please
export LC_ALL=C

# If space_frame nr is between start/stop frame nrs
if [ "$3" = "random" ]; then
  if [[ $Rspace_frame -ge $Rfrstart  ]] && [[ $Rspace_frame -le $Rfrstop ]]; then
    # random number
    echo $(shuf -i 0-25 -n1) > "$tmpfile"
  fi
elif [ "$3" = "" ]; then
  if [[ $Rspace_frame -ge $Rfrstart  ]] && [[ $Rspace_frame -le $Rfrstop ]]; then
    # How many frames active
    Ractiveframes=$((Rfrstop-Rfrstart))
    # How many steps up per frame to get to 100
    Radd_val=$(echo "100/$Ractiveframes" | bc -l | xargs printf "%.2f")
    # Get current sensor value
    Rcurrent_val=$(cat "$tmpfile")
    # Ads step to current sensor value
    Rnew_val=$(echo "$Rcurrent_val+$Radd_val" | bc -l | xargs printf "%.2f")
    echo $Rnew_val > "$tmpfile"
    # debug
#    echo "RIGHT: $Rspace_frame | $Ractiveframes | $Radd_val | $Rcurrent_val | $Rnew_val |"
    exit
  fi
fi


# To debug uncomment below line
#echo "RIGHT: | $Rspace_frame | $Rfrstart | $Rfrstop | $Ractiveframes | $Radd_val | $Rcurrent_val | $Rnew_val |"