#!/usr/bin/env bash
#
# findrss - search (un)common paths to find atom/rss feed of a site
#
# one-liner:
#   while read -r a; do curl -L "$a" | awk '/(atom|rss)\+xml/'; done <list
#
# ---
# source: https://github.com/begriffs/findrss

# shellcheck shell=bash disable=SC2312,SC3009

if [ -z "$1" ]; then
  printf '%s\n' "$(basename "$0") [url]" >&2
  exit 1
fi

# $(echo {,feed/,feeds/,rss/,blog/}{,all,atom,feed,index,posts,posts/default,rss,en,default,rssfeed,blog}{,.rss,.atom,.rss2}{,.xml,?feed=rss2,?format=atom})

for path in $(echo {,feed/,feeds/,rss/,blog/}{,all,atom,feed,index,posts,posts/default,rss,en,default,rssfeed,blog}{,.rss,.atom,.rss2}{,.xml,?feed=rss2,?format=atom}); do

  line=$(curl -L -s "$1/${path}" | head -n 1)

  if printf '%s' "${line}" | grep -v 'xhtml' | grep -Eq 'feed|xml'; then
    # show canonical redirect location
    # curl --disable --silent --location --head --output /dev/null --write-out
    curl -sLI -o /dev/null -w '%{url_effective}' "$1/${path}"
    echo
    exit
  fi
done
exit 1
