#!/bin/bash
#
#  This script calculates a value from 0-100 if a star comes near the ship..
#  Then the left 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
Lfrstart=$1
Lfrstop=$2
x=$3

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

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

# All output in english please
export LC_ALL=C

# If space_frame nr is between start/stop frame nrs
if [ "$3" = "random" ]; then
  if [[ $Lspace_frame -ge $Lfrstart  ]] && [[ $Lspace_frame -le $Lfrstop ]]; then
      # random number
      echo $(shuf -i 0-25 -n1) > "$tmpfile"
  fi
elif [ "$3" = "" ]; then
  if [[ $Lspace_frame -ge $Lfrstart  ]] && [[ $Lspace_frame -le $Lfrstop ]]; then
      # How many frames active
      Lactiveframes=$((Lfrstop-Lfrstart))
      # How many steps up per frame to get to 100
      Ladd_val=$(echo "100/$Lactiveframes" | bc -l | xargs printf "%.2f")
      # Get current sensor value
      Lcurrent_val=$(cat "$tmpfile")
      # Ads step to current sensor value
      Lnew_val=$(echo "$Lcurrent_val+$Ladd_val" | bc -l | xargs printf "%.2f")
      echo $Lnew_val > "$tmpfile"
      # debug on terminal, uncomment line below
      #echo "LEFT: spfr:$Lspace_frame | actfrs:$Lactiveframes | add:$Ladd_val | curr:$Lcurrent_val | new:$Lnew_val |"
      exit
  fi
fi
