#!/usr/bin/env sh
#
# rssadd - add url to newsboat

if echo "$1" | grep -q "https*://\S\+\.[A-Za-z]\+\S*"; then
  url="$1"
else
  url="$(grep -E -m 1 -o '<[^>]+(rel="self"|application/[a-z]+\+xml)[^>]+>' "$1" \
    | grep -o "https?://[^\" ]")"

  echo "${url}" | grep -q "https*://\S\+\.[A-Za-z]\+\S*" \
    || notify "RSS" "That doesn't look like a full URL" && exit 1
fi

urlfile="${XDG_CONFIG_HOME:-${HOME%/}/config}/newsboat/urls"

if awk '{ print $1 }' "${urlfile}" | grep "^${url}$" >/dev/null; then
  notify "RSS" "You already have this RSS feed."
else
  # if [ "$#" -gt 1 ]; then
  # tags="${(@)${@:2}//(#m)*/\"${MATCH}\"}"
  # echo "${url} ${tags}" >> "${urlfile}" && notify-send "RSS" "RSS feed added."
  # else
  echo "${url}" >>"${urlfile}" && notify "RSS" "RSS feed added."
  # fi
fi
