#!/bin/bash

# Nextcloud username and password. This is a per-app password only for use with curl.
ncloud_un_pw='un:pw'
ncloud_url='localhost/ocs/v2.php/apps/notifications/api/v2/notifications'

# ntfy private instance username and password
ntfy_un_pw='un:pw'
ntfy_url='https://example.com'

# Plan B ntfy instance in case the other ntfy instance does not work
ntfy_url_other='https://ntfy.sh/mytopic'

ntfy_error () {
curl -d "There was an issue with the local ntfy instance. Script has exited." "$ntfy_url_other"
exit 1
}

talk_error () {
echo -e '\n'"$(date +%Y-%m-%d\ %R) $notif_data" >> talkn.log
curl -u "$ntfy_un_pw" -d "Warning: There was an issue${err_detail} with the Nextcloud notification script. It has exited." "${ntfy_url}/talk" && exit 1
}

while :; do

notif_data=$(curl -s -H 'Accept: application/json' -H 'OCS-APIREQUEST: true' -u "$ncloud_un_pw" "$ncloud_url")

# The notification data shows the following string when there are no notifications currently.
if [ "$notif_data" != '{"ocs":{"meta":{"status":"ok","statuscode":200,"message":"OK"},"data":[]}}' ]; then
        subject=$(echo "$notif_data" | jq -r '.ocs.data[0].subject')
        message=$(echo "$notif_data" | jq -r '.ocs.data[0].message')
        notif_id=$(echo "$notif_data" | jq -r '.ocs.data[0].notification_id')

	# If I am updating an app or Nextcloud, this will give it time to do so before running the next check.
	# I need to do continue so on the next pass it updates $notif_data.
	[[ "$notif_data" =~ "<message>Service unavailable</message>" ]] || [[ "$notif_data" =~ "<title>404 Not Found</title>" ]] || [ -z "$notif_data" ] && { sleep 300; continue; }

        if [ -z "$subject" ] || [ "$subject" == "null" ] || [ "$message" == "Unauthorised" ]; then
		# Any Nextcloud notification will have a subject.
                talk_error
                curl -d "There was an issue querying Nextcloud notifications and also using the local ntfy instance. Script has exited." "$ntfy_url_other"; exit 1
        fi

        if [[ "$subject" =~ "would like to talk with you" ]]; then
		# I use a separate ntfy topic for a call so it rings instead of dings.
                curl -u "$ntfy_un_pw" -d "$subject" "${ntfy_url}/talkcall" || ntfy_error
        elif [[ "$subject" =~ "You missed a call from" ]] || [[ "$subject" =~ "reacted with" ]]; then
		# There is no message for a missed call or reaction notification, only a subject.
                curl -u "$ntfy_un_pw" -d "$subject" "${ntfy_url}/talk" || ntfy_error
        else
                if [ -z "$message" ]; then
			# If I receive a notification for a Nextcloud app update, then that only has a subject and no message. If that is the case, I also want to see the Word of the Day. Why not...
                        message=$(w3m 'https://www.thefreedictionary.com/' | grep -A7 '               Word of the Day' | sed '/^$/d;1,4d')
                fi
		# The following will be for any regular message or app update.
                curl -H "Title: $subject" -u "$ntfy_un_pw" -d "$message" "${ntfy_url}/talk" || ntfy_error
        fi

	# Delete the notification that has just been checked. It is really important to exit if this does not work. Otherwise, my phone will ping me every four seconds about the same notification.
        curl -H "OCS-APIRequest: true" -u "$ncloud_un_pw" --request "DELETE" "${ncloud_url}/${notif_id}" || { err_detail=" deleting"; talk_error
	curl -d "There was an issue deleting Nextcloud notifications and also using the local ntfy instance. Script has exited." "$ntfy_url_other"; exit 1; }
fi

sleep 4

done
