#!/bin/bash
TMP1=$(mktemp)
TMP2=$(mktemp)
TMP3=$(mktemp)
TMP4=$(mktemp)
ONE=$(mktemp)
TWO=$(mktemp)
HERE=/home/phil/2emoncms
cd $HERE

HOST="http://10.128.0.7:8080"    # Internet network address of emoncms server
APIKEY="143c42b0c1f7d4424aa61d859a65270f"

SITE=hydrom004

ps -fel | grep hydrom004
echo "0: $0"
echo "dollardollar: $$"
echo "basename: $(basename $0)"
echo "pidof: $(pidof -x $(basename $0))"
ps -fel | grep $0 | grep -v grep 
kount=$(ps -fel | grep $0 | grep -v grep | grep -v $$ | wc -l)
echo "kount: $kount"
if [ $kount -gt 2 ]
then
   echo "Already running, aborting"
   exit 2
fi



#for SUBSITE in $SITE
#do
	echo "*****"
	echo "Processing: $SUBSITE"
	echo "*****"
   DATALOCATION="$HOME/data/HM/$SITE"      # NFS share
#   CONTROLFILE=$HERE/control/$SITE/$SUBSITE/last_record_uploaded_to_emoncms
   CONTROLFILE=$HERE/control/$SITE/last_record_uploaded_to_emoncms
echo $CONTROLFILE
if [ -s "$CONTROLFILE" ]
   then
      #Gather the next records after the last one processed
      LASTREC=$(cat $CONTROLFILE)
      echo $LASTREC
      #Gather the next record to end of file after the last one processed, also some "data fix" for incomplete records (no trailing "}")
     #Code updated Nov 21 to only select latest 10 days of files 
find $DATALOCATION/*$SUBSITE* -newermt $(date +%Y-%m-%d -d '10 days ago') -type f -exec sort {} \; > $TMP4
jq -cs 'sort_by(.timestamp) | .[]' $TMP4 > THISHM004$SUBSITE

echo "First 10 records"
head THISHM004$SUBSITE
#head -1 THISHM004$SUBSITE > $TMP1
      sed -n "/\"Timestamp\":$LASTREC/{ :a; n; p; ba; }" THISHM004$SUBSITE  > $TMP1
      #rm THIS$SUBSITE
      ##########
      ### Additional Quality checks on data 
      #if [ "$SUBSITE" = "emonpi" ]
      #then
        # cat $TMP1 | jq -c 'if (.power1 < 0) then (.power1pluspower2 = (.power1 * -1) + .power2) else . end' | jq -c 'if (.power1 < 0) then (.power1 = (.power1 * -1)) else . end'> $TMP3
         #cp $TMP3 $TMP1
      #fi
   else
      echo "No control file - aborting"
      exit 1
   fi

   if [ -s $TMP1 ]
   then
     wc -l $TMP1
     while read line
     do
        datapart=$line
        timefield=$(echo $datapart | jq '.timestamp')
        echo "Posting to emoncms $datapart"
        curl --data "time=$timefield&node=$SITE&data=$datapart&apikey=$APIKEY" "$HOST/input/post" >$ONE 2>$TWO
	checkone=$(cat $ONE)
	if [ "$checkone" = "ok" ]
	then
           # Range check 
#           echo $timefield | awk -vlow=1007163722 -vhigh=2607163722 '{if ($1 >= low && $1 <= high) exit 0;else exit 1}' 
#           if [ $? -ne 0 ]; then echo "timefield: $timefield" >> $HERE/errors/$SITE_data_out_of_range;exit 1;fi
           #Write lastest record to control file
           echo $timefield > $CONTROLFILE
	   sleep 0.1
        else
           echo "Error in posting"
	   echo "ONE is $(cat $ONE)"
	   echo "TWO is $(cat $TWO)"
	   exit 2
        fi
     done < $TMP1
   else
     echo "Empty so no processing required"
   fi
#done

rm $TMP1
rm $TMP2
rm $TMP3
rm $TMP4
rm $ONE
rm $TWO
