#!/bin/bash
######################################################################
#Copyright (C) 2022 Kris Occhipinti
#https://filmsbykris.com
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program. If not, see .
######################################################################
dir="$HOME/.fbk"
log="$dir/nextcloud_notification.log"
mkdir -p "$dir" || exit 1
touch "$log"
source $HOME/.fbk/nex/push.conf
#nextcloud data
#domain="server.com"
#port="3214"
#
##ntfy data
#ntfy_server="server.com"
#ntfy_port="654"
#ntfy_topic="messages"
#ntfy_user="username"
#ntfy_password="password"
#declare -A users
#users["user1"]="password1"
#users["user2"]="password2"
for user in ${!users[*]}
do
password="${users[$user]}"
curl -s -H 'Accept: application/json, text/plain, */*' -u $user:$password https://$domain:$port/nextcloud/ocs/v2.php/apps/notifications/api/v2/notifications|jq '.ocs.data[]|(.subject + "|" + .message + "|" + .link)'|while read n
do
nlog="$(echo $n|base64 -w0)"
subject="$(echo "$n"|cut -d\| -f1)"
message="$(echo "$n"|cut -d\| -f2|tr "\n" " ")"
link="$(echo "$n"|cut -d\| -f3)"
#echo "$n"
#echo "---------------------"
#curl -k -H \"Click:$link\" -H \"Title: $subject\" -u $ntfy_user:$ntfy_password -d \"$message\" https://$ntfy_server:$ntfy_port/$ntfy_topic"|tr "\n" " "
# echo "$message"
# grep "$nlog" "$log"|wc -l
if [[ $(grep "$nlog" "$log"|wc -l) == 0 ]]
then
echo "$nlog" >> $log
curl -k -H "Click:$link" -H "Title: $subject" -u $ntfy_user:$ntfy_password -d "$message" https://$ntfy_server:$ntfy_port/$ntfy_topic
echo "$message"
fi
done
done