#!/bin/bash

OUTPUT='/tmp/statusmail.txt'
OUTPUT2='/tmp/maintwakes.txt'
TIMEINT='1h'
START="$(date +%s)"
echo "" > ${OUTPUT}
log show --style syslog --last ${TIMEINT} | fgrep "Wake reason" | fgrep "Maintenance" >> ${OUTPUT2}
lines=`cat ${OUTPUT2} | grep -c '^'`
DURATION=$[ $(date +%s) - ${START} ]
if [ "$lines"  -gt 1 ]
then
  echo "Occurances: $lines" >> ${OUTPUT}
  echo "Duration: ${DURATION}" >> ${OUTPUT}
  echo "Time Interval: ${TIMEINT}" >> ${OUTPUT}
  echo "------------------------------------------------------------------" >> ${OUTPUT}
  echo "" >> ${OUTPUT}
  echo "" >> ${OUTPUT}
  cat ${OUTPUT2} >> ${OUTPUT}
  echo "" >> ${OUTPUT}
  echo "" >> ${OUTPUT}
  echo "------------------------------------------------------------------" >> ${OUTPUT}
  #echo "$ott" > /tmp/wakemaints.txt
  /usr/bin/python3 $HOME/Dropbox/bin/send_status_mail3.py
else
  echo "No maintenance wakes last ${TIMEINT}"
fi
#cat /tmp/wakemaints.txt | fgrep "Wake reason"
rm ${OUTPUT}
rm ${OUTPUT2}
if [ "$lines"  -gt 10 ]
then
  sleep 10
  osascript -e 'tell application "System Events" to shut down'
fi
