#!/bin/bash

#url=https://www.reddit.com/user/dunksmash666.rss
url=https://www.reddit.com/user/Galen_Sonic.rss
user_agent="linux:simplersschecker:v1.0.0 (by /u/thedrx)"
 
# Make a directory if it doesn't exist yet.
mkdir -p ./rsscompare
cd ./rsscompare
 
# Grab the latest rss file for comparison
curl -A "$user_agent" -s -S -o temp $url
sed 's/>/>\n/g' temp >temp2  # split file into lines
sed '0,/\/updated/{/\/updated/d}' temp2 >rss.old  # remove the first line containing </updated> 
 
# Run le infinite loop
while [ true ]; do
    # Grab a new copy of the .rss file
    curl -A "$user_agent" -s -S -o temp $url
    sed 's/>/>\n/g' temp >temp2
    sed '0,/\/updated/{/\/updated/d}' temp2 >rss.new  # remove the first line containing </updated> 
    diff rss.old rss.new > diffreport.txt
    if [ -s "diffreport.txt" ]
        then
	echo -e "\n\n\n"
	echo Alerting `date`
	cat diffreport.txt
        sed 's/[\\"'"'"']//g' diffreport.txt > temp3
	../send.sh "`cat temp3`"
    fi
    # Change .new to .old, remove the report, and loop again
    rm -f rss.old diffreport.txt
    mv rss.new rss.old
    sleep 60
done

