#!/bin/bash
# Telegram Variables
TOKEN="xxxxxxxxxxxxxxxxxx"
ID="xxxxxxxxx"

# Log location
LOG="/remote_logs/borg.log"

enviar_documento_telegram() {
  URL="https://api.telegram.org/bot$TOKEN/sendDocument"
  curl -v -4 -F \
    chat_id=$ID \
    -F document=@$LOG \
    $URL 2> /dev/null
}

DATELOG=`date +%Y-%m-%d`

# Creating the temporary file for Telegram
telegramlog="/tmp/BORG-$1-$DATELOG.log"

# Obtain the log for today
grep -B 1 -A 100 "Archive name: Unraid-$DATELOG" $LOG > $telegramlog

enviar_documento_telegram $telegramlog

# Delete the temporary file
rm $telegramlog
