#!/bin/sh
echo "Wake Log Collecting..."
DBIN=$HOME/Dropbox/bin/
export DBIN
ESUBJ="$KMINFO_MacroName"
export ESUBJ
cname=`scutil --get ComputerName`
script=`basename $0`


echo "${DBIN}wakelogstart"
${DBIN}wakelogstart

#echo "// wake report" >> /tmp/wakereasons.txt
#echo "  " `date` >> /tmp/wakereasons.txt
#echo "   machine: ${cname}" >> /tmp/wakereasons.txt
echo "   script: ${script}" >> /tmp/wakereasons.txt
echo "  " `/usr/bin/sw_vers` >> /tmp/wakereasons.txt
echo "  " `sysctl kern.version` >> /tmp/wakereasons.txt
echo "  " `sysctl kern.ostype` >> /tmp/wakereasons.txt
echo "  " `sysctl kern.osrelease` >> /tmp/wakereasons.txt
echo "  " `sysctl kern.osrevision` >> /tmp/wakereasons.txt
echo "" >> /tmp/wakereasons.txt
echo "" >> /tmp/wakereasons.txt
echo "" >> /tmp/wakereasons.txt
echo "                       █████               ███" >> /tmp/wakereasons.txt
echo "                      ░░███               ░███" >> /tmp/wakereasons.txt
echo " █████ ███ ███████████ ░███ █████  ██████ ░███" >> /tmp/wakereasons.txt
echo "░░███ ░███░░███░░░░░███░███░░███  ███░░███░███" >> /tmp/wakereasons.txt
echo " ░███ ░███ ░███ ███████░██████░  ░███████ ░███" >> /tmp/wakereasons.txt
echo " ░░███████████ ███░░███░███░░███ ░███░░░  ░░░ " >> /tmp/wakereasons.txt
echo "  ░░████░████ ░░███████████ █████░░██████  ███" >> /tmp/wakereasons.txt
echo "   ░░░░ ░░░░   ░░░░░░░░░░░ ░░░░░  ░░░░░░  ░░░ " >> /tmp/wakereasons.txt

divLine() {
  echo "---------------------------------------------" >> /tmp/wakereasons.txt
}


echo "" >> /tmp/wakereasons.txt
echo "Sleep Quick Status" >> /tmp/wakereasons.txt
divLine
pmset -g log|grep -e " Sleep  " -e " Wake  " | tail -n 2 > /tmp/pmsetlog.tmp

sleeptm=$(cat /tmp/pmsetlog.tmp | awk '{print $1,$2}' | head -n 1)
waketm=$(cat /tmp/pmsetlog.tmp | awk '{print $1,$2}' | tail -n 1)
sleepsec=$(date -j -f "%Y-%m-%d %H:%M:%S" "${sleeptm}" "+%s")
wakesec=$(date -j -f "%Y-%m-%d %H:%M:%S" "${waketm}" "+%s")
#echo "sleepsec: " ${sleepsec}
#echo "wakesec: " ${wakesec}
timedelta=$(( ((wakesec-sleepsec)/60)/60 ))
ftimedelta=$(bc <<< "scale=2; ((${wakesec}-${sleepsec})/60)/60")
echo "Sleep Time:" ${sleeptm} >> /tmp/wakereasons.txt
echo "Wake Time:" ${waketm} >> /tmp/wakereasons.txt
printf "Sleep Duration: %0.2f hours\n" $ftimedelta >> /tmp/wakereasons.txt
echo "" >> /tmp/wakereasons.txt
cat /tmp/pmsetlog.tmp >> /tmp/wakereasons.txt 
#echo `date` > /tmp/wakenotify.txt

echo "" >> /tmp/wakereasons.txt
echo "" >> /tmp/wakereasons.txt
echo "pmset -g stats" >> /tmp/wakereasons.txt
divLine
pmset -g stats >> /tmp/wakereasons.txt

echo "" >> /tmp/wakereasons.txt
echo "" >> /tmp/wakereasons.txt
echo "log show --style syslog --last 3h | fgrep \"Wake reason\"" >> /tmp/wakereasons.txt
divLine
log show --style syslog --last 3h | fgrep "Wake reason" >> /tmp/wakereasons.txt

echo "" >> /tmp/wakereasons.txt
echo "" >> /tmp/wakereasons.txt
echo "pmset -g log | grep -e \"due to\" | tail -n 50" >> /tmp/wakereasons.txt
divLine
pmset -g log | grep -e "due to" | tail -n 50 >> /tmp/wakereasons.txt

echo "" >> /tmp/wakereasons.txt
echo "" >> /tmp/wakereasons.txt
echo "" >> /tmp/wakereasons.txt
echo "pmset -g sched" >> /tmp/wakereasons.txt
divLine
pmset -g sched >> /tmp/wakereasons.txt

echo "" >> /tmp/wakereasons.txt
echo "" >> /tmp/wakereasons.txt
echo "" >> /tmp/wakereasons.txt
echo "pmset -g log | grep \"Wake Requests\" | tail -n 25" >> /tmp/wakereasons.txt
divLine
pmset -g log | grep "Wake Requests" | tail -n 25 >> /tmp/wakereasons.txt

echo "" >> /tmp/wakereasons.txt
echo "" >> /tmp/wakereasons.txt
echo "" >> /tmp/wakereasons.txt
echo "sysctl -a | grep -iE \"dark|wake\"" >> /tmp/wakereasons.txt
divLine
sysctl -a | grep -iE "dark|wake"  >> /tmp/wakereasons.txt

echo "" >> /tmp/wakereasons.txt
echo "" >> /tmp/wakereasons.txt
echo "" >> /tmp/wakereasons.txt
echo "pmset -g assertions" >> /tmp/wakereasons.txt
divLine
pmset -g assertions >> /tmp/wakereasons.txt


echo "" >> /tmp/wakereasons.txt
echo "" >> /tmp/wakereasons.txt
echo "" >> /tmp/wakereasons.txt
echo "pmset -g log | grep DarkWake | tail -n 25" >> /tmp/wakereasons.txt
divLine
pmset -g log | grep DarkWake | tail -n 25 >> /tmp/wakereasons.txt


echo "" >> /tmp/wakereasons.txt
echo "" >> /tmp/wakereasons.txt
echo "" >> /tmp/wakereasons.txt
echo "log show --last 1h --style syslog | fgrep \"system wake events\"" >> /tmp/wakereasons.txt
divLine
log show --last 1h --style syslog | fgrep "system wake events" >> /tmp/wakereasons.txt

echo "" >> /tmp/wakereasons.txt
echo "" >> /tmp/wakereasons.txt
echo "" >> /tmp/wakereasons.txt
echo "pmset -g log|grep -e \" Sleep \" -e \" Wake \" | tail -n 100" >> /tmp/wakereasons.txt
divLine
pmset -g log|grep -e " Sleep " -e " Wake " | tail -n 100 >> /tmp/wakereasons.txt

#echo "" >> /tmp/wakereasons.txt
#echo "" >> /tmp/wakereasons.txt
#echo "" >> /tmp/wakereasons.txt
#echo "pmset -g everything" >> /tmp/wakereasons.txt
#divLine
#pmset -g everything >> /tmp/wakereasons.txt

echo "" >> /tmp/wakereasons.txt
echo "" >> /tmp/wakereasons.txt
echo "" >> /tmp/wakereasons.txt
echo "Process list (ps auxc -r)" >> /tmp/wakereasons.txt
divLine
ps auxc -r >> /tmp/wakereasons.txt

echo "" >> /tmp/wakereasons.txt
echo "" >> /tmp/wakereasons.txt
echo "" >> /tmp/wakereasons.txt
echo "Adjust Sleep Settings to Avoid Wakes"  >> /tmp/wakereasons.txt
divLine
echo "sudo -S pmset schedule cancelall"  >> /tmp/wakereasons.txt
# more /etc/sudoers.d/shughey
# shughey ALL=(ALL) NOPASSWD: /usr/bin/pmset
sudo -S pmset schedule cancelall
sudo pmset repeat cancel
# echo "sudo pmset -a hibernatemode 0"
# sudo pmset -a hibernatemode 0
echo "sudo pmset -a autopoweroff 0" >> /tmp/wakereasons.txt
sudo pmset -a autopoweroff 0
echo "sudo pmset -a standby 0" >> /tmp/wakereasons.txt
sudo pmset -a standby 0
echo "sudo pmset -a tcpkeepalive 0" >> /tmp/wakereasons.txt
sudo pmset -a tcpkeepalive 0
echo "sudo pmset -a powernap 0" >> /tmp/wakereasons.txt
sudo pmset -a powernap 0

${DBIN}wakelogend
python3 $HOME/Dropbox/bin/send_wake_mail3.py
echo "Done!"
rm /tmp/wakereasons.txt

