#!/bin/bash

tgapi=""
tgch=""

nextcloudpath="/var/www/html"
wwwuser="www-data"
logmax="250"
logprefix="logwatch"

## Level 0 = DEBUG
## Level 1 = INFO
## Level 2 = WARNING
## Level 3 = ERROR
## Level 4 = FATAL
## Will send everything from selected log level and up.
loglevel=3

errorstring=('"level":0,' '"level":1,' '"level":2,' '"level":3,' '"level":4,')
errorname=('DEBUG' 'INFO' 'WARNING' 'ERROR' 'FATAL')

function sendtelegram {
  echo "Nextcloud has new log entries..." > /tmp/nclogentryheader.txt
  echo "Server: $(hostname)" >> /tmp/nclogentryheader.txt
  echo "Path: $nextcloudpath" >> /tmp/nclogentryheader.txt
  echo "Date: $(date)" >> /tmp/nclogentryheader.txt
  echo "" >> /tmp/nclogentryheader.txt
  string=$(cat /tmp/nclogentryheader.txt /tmp/nclogentry.txt)
  wget -qO- "https://api.telegram.org/bot$tgapi/sendMessage?chat_id=$tgch&text=$string" &> /dev/null
  errorcode=$?
  updatelog $errorcode
}

function updatelog {
  logdata="Telegram Sent"
  if [[ ! -z $1 ]]; then
    if [[ $1 == "0" ]]; then
      logerror="Successful!"
    else
      logerror="Sending Failed: Exit Code $1"
    fi
  fi
  echo "$(date +%y%m%d).$(date +%H%M) - $logprefix - $logdata - $logerror" >> /var/log/nextcloud-telegram-notify.log
  logsize=$(cat /var/log/nextcloud-telegram-notify.log | wc -l)
  if [[ $logsize -gt $logmax ]]; then
    mv /var/log/nextcloud-telegram-notify.log /tmp/nextcloud-telegram-notify.log
    tail -n $logmax /tmp/nextcloud-telegram-notify.log > /var/log/nextcloud-telegram-notify.log
    rm /tmp/nextcloud-telegram-notify.log
  fi
}

sudo -u $wwwuser php "$nextcloudpath/occ" user:list > /tmp/ncchecknewusers.txt

array=($(cat $nextcloudpath/config/config.php | grep 'datadirectory'))
nextcloudfilespath=${array[2]:1:-2}
cp $nextcloudfilespath/nextcloud.log /tmp/nclog.tmp

if [[ -f /tmp/nclog.old ]]; then
  rm -f /tmp/nclog.changes.tmp
  for ((lvl=4; lvl>$((loglevel-1)); lvl--)); do
    cat /tmp/nclog.old /tmp/nclog.old /tmp/nclog.tmp | grep "${errorstring[$lvl]}" | sort | uniq -u >> /tmp/nclog.changes.tmp
  done
  rm -f /tmp/nclog.new.*.tmp
  size=$(cat /tmp/nclog.changes.tmp | wc -l)
  if [[ $size -gt 0 ]]; then
    n=0
    while read -r entry; do
      echo "$n" "/tmp/nclog.new.$n.tmp"
      echo $entry > "/tmp/nclog.new.$((n++)).tmp"
    done < /tmp/nclog.changes.tmp
    rm -f /tmp/nclogentry.txt
    for ((c=0; c<$n; c++)); do
      log_level=$(cat "/tmp/nclog.new.$c.tmp" | jq -r .level)
      echo "Log Entry: ${errorname[$log_level]}" >> /tmp/nclogentry.txt
      echo "Time: $(cat "/tmp/nclog.new.$c.tmp" | jq -r .time)" >> /tmp/nclogentry.txt
      echo "ReqID: $(cat "/tmp/nclog.new.$c.tmp" | jq -r .reqId)" >> /tmp/nclogentry.txt
      echo "Remote Address: $(cat "/tmp/nclog.new.$c.tmp" | jq -r .remoteAddr)" >> /tmp/nclogentry.txt
      echo "Username:  $(cat "/tmp/nclog.new.$c.tmp" | jq -r .user)" >> /tmp/nclogentry.txt
      echo "App: $(cat "/tmp/nclog.new.$c.tmp" | jq -r .app)" >> /tmp/nclogentry.txt
      echo "Message: $(cat "/tmp/nclog.new.$c.tmp" | jq -r .message)" >> /tmp/nclogentry.txt
      echo "" >> /tmp/nclogentry.txt
    done
    sendtelegram
  else
    echo "There are no log entries to send..."
  fi
else
  echo "First run... saving current log..."
fi

mv /tmp/nclog.tmp /tmp/nclog.old
