#!/bin/bash

DL_DIRECTORY="/tmp/pre-commit-rss-check";
RSS_NEW_FILE_PATH="$(dirname "$0")/output/feeds/all.rss.xml"
RSS_ORIGINAL_URL="https://tacosteemers.com/feeds/all.rss.xml"
RSS_ORIGINAL_FILE_PATH="$DL_DIRECTORY/all.rss.xml"

# Check the RSS feed equality. We want to return 1 if we have
# the same amount of titles but the hash is not the same.
# In that situation we have accidentally changed the feed.
# If we have a different amount of titles then it means the
# feed has changed due to a new article.
# THat change could also hide an accidental change.

# We need to get the current RSS feed from the website
# Create the download directory or remove the existing RSS file
if [ ! -d "$DL_DIRECTORY" ]; then
  mkdir "$DL_DIRECTORY";
else
    rm $RSS_ORIGINAL_FILE_PATH 2> /dev/null
fi
wget "$RSS_ORIGINAL_URL" -O "$RSS_ORIGINAL_FILE_PATH"

# If we have more articles now than we had before, we can skip the check
TITLES_ORIGINAL=$(grep -roh "<title>" $RSS_ORIGINAL_FILE_PATH | wc -w)
TITLES_NEW=$(grep -roh "<title>" $RSS_NEW_FILE_PATH | wc -w)
if [[ "$TITLES_ORIGINAL" != "$TITLES_NEW" ]]; then
    echo "New article detected. Skipping further RSS feed checking."
    exit 0
fi

# Remove the lastBuildDate tag and compare the file hashes.
HASH_ORIGINAL=$(git hash-object | sed "s/\<lastBuildDate\>//g; s/\<\/lastBuildDate\>//g" "$RSS_ORIGINAL_FILE_PATH")
HASH_NEW=$(git hash-object | sed "s/\<lastBuildDate\>//g; s/\<\/lastBuildDate\>//g" "$RSS_NEW_FILE_PATH")
if [[ "$HASH_ORIGINAL" == "$HASH_NEW" ]]; then
    echo "RSS hashes match"
    exit 0
fi
echo "RSS hashes do not match! Possible accidental change detected."
exit 1