#!/bin/bash

# WildlifeSystems
#
# This script is part of the WildlifeSystems project. For further information
# please refer to https://docs.wildlife.systems, or for more information on
# the project itself, please refer to https://wildlife.systems.

# Return codes
#
# Further information on WildlifeSystems standard reurn codes can be found
# at https://docs.wildlife.systems/return-codes.html
# 0 - Success
# 1 - Already running
# 11 - Incorrect filename pattern


#Check if this script already running
if [[ "$(pgrep -c ws-run-sensors)" -gt 1 ]]; then
  echo "ws-run-sensors already running. Exiting."
  exit 1
fi

trap "echo 'Terminating ws-run-sensors...'; exit 0" SIGINT SIGTERM

echo "Starting ws-run-sensors..."

#Config
CONFIG_FILE="/etc/ws/sensors.conf"
if [[ ! -f "$CONFIG_FILE" || ! -r "$CONFIG_FILE" ]]; then
  echo "Error: Config file '$CONFIG_FILE' not found or not readable."
  exit 2
fi
CONFIG=$(cat "$CONFIG_FILE")

#Check config exists
if [[ -z "$CONFIG" ]]; then
  echo "No config file found."
  exit 2
fi

function onOff {
  pi-pwr off "$(echo "$CONFIG" | jq -r '.init .off[]' | tr '\n' ' ')"
  pi-pwr on "$(echo "$CONFIG" | jq -r '.init .on[]' | tr '\n' ' ')"
}


SENSOR_DATA=""
N=$(echo "$CONFIG" | jq -r ".run | length")
I=0
while [ $I  -lt $N ];
do
        pi-pwr off $(echo "$CONFIG" | jq -r ".run[$I] .off[]" | tr '\n' ' ')
        pi-pwr on $(echo "$CONFIG" | jq -r ".run[$I] .on[]" | tr '\n' ' ')
        ACTION=`echo $CONFIG | jq -r ".run[$I] .action"`
        echo $ACTION
        #ws-heartbeat $ACTION
        case  $ACTION in
                        sensor_read)
                                SENSOR=$(echo "$CONFIG" | jq -r ".run[$I] .sensor")
                                SENSOR_TYPE=$(echo "$CONFIG" | jq -r ".run[$I] .sensor_type")

                                SENSOR_DATA=$(sr "$SENSOR" "$SENSOR_TYPE")
                                echo $SENSOR_DATA
                                ;;
                        sleep)
                                DURATION=$(echo "$CONFIG" | jq -r ".run[$I] .duration")
                                sleep "$DURATION"
                                ;;
                        upload-delete)
                                METHOD=`echo $CONFIG| jq -r ".run[$I] .method"`
                                case $METHOD in
                                        file)
                                                FILE=$(echo $CONFIG | jq -r ".run[$I] .file")
                                                if [[ -z "$FILE" ]]; then
                                                        echo "Error: File path is not defined in the configuration."
                                                        continue
                                                fi
                                                if [[ -z "$SENSOR_DATA" ]]; then
                                                        echo "Warning: No sensor data to write to $FILE."
                                                        continue
                                                fi
                                                echo "Writing to file: $FILE"
                                                echo "$SENSOR_DATA" >> $FILE
                                                ;;
                                        script)
                                                SCRIPT=`echo $CONFIG| jq -r ".run[$I] .script"`
                                                if [[ ! -x "$SCRIPT" ]]; then
                                                  echo "Script $SCRIPT not executable."
                                                  continue
                                                fi

                                                if [[ -n "$SENSOR_DATA" ]]; then
                                                  # Run synchronously, capture stdout+stderr and exit code
                                                  OUTPUT=$("$SCRIPT" <<< "$SENSOR_DATA" 2>&1)
                                                  EXIT=$?
                                                  if [[ $EXIT -ne 0 ]]; then
                                                    echo "Script $SCRIPT failed (exit $EXIT): $OUTPUT"
                                                  fi
                                                fi
                                                ;;
                                esac
                                ;;
                        *)
                                echo "Unknown action: $ACTION" 1>&2
                                ;;
                esac

        I=$(($I + 1))
        #ws-indicate heartbeat
onOff
done

exit 0
