#!/bin/bash

#This file uploads a app telemetry file to http://pi-ware-telemetry.ml/

#load functions
source "$HOME/pi-ware/func/api" || echo "failed to source $HOME/pi-ware/func/api"

#Main
#Check if connected to internet
wget -q --spider http://google.com

if [ $? -eq 0 ]; then
    #User online
    internet="1"
else
    #User offline
    internet="0"
fi
if [ "$internet" == "1" ]; then
  echo "Checking for telemetry file..."
  if [ -f "$HOME/.local/share/pi-ware/telementry/data-1.json" ]; then
    echo "Sending file..."
    curl -F "fileToUpload=@$HOME/.local/share/pi-ware/telemetry/data-1.json" http://pi-ware-telemetry.ml/uploader.php | echo "ERROR: I was unable to send the telemetry file."
  else
    error "ERROR: Unable to find '$HOME/.local/share/pi-ware/telemetry/data.json'\nA log has been generated in: '$HOME/pi-ware/logs/$logname'"
  fi
else
  error "ERROR: You are not connected to the internet,\nI am unable to send a telemetry file to http://pi-ware-telemetry.ml\nA log has been created in: '$HOME/pi-ware/logs/$logfile'"
fi
