#!/bin/sh
# derived from yt-id (https://codeberg.org/Denshi/Scripts) and rssadd (https://github.com/lukesmithxyz/voidrice)
# requirements: `pup` for parsing html

## kill early
nothing() {
[ -z "$1" ] && printf "Usage: rssadd [RSSURL | YTURL | @Username | XMLFILE | --file FILE | --scan WEBPAGE ]\n" && exit 0
}
nothing "$1"

say() {
  printf "$1 \e[1;34m$2\e[0m\n"
}

add_rss() {
  RSSFILE="${XDG_CONFIG_HOME:-$HOME/.config}/newsboat/urls"
  if awk '{print $1}' "$RSSFILE" | grep "^$url$" >/dev/null; then
  	say "You already have this RSS feed." "$url"
  else
	echo "$url" >> "$RSSFILE" && say "RSS feed added." "$url"
  fi
}

get_yt_rss() {
  BASEURL="https://www.youtube.com/feeds/videos.xml?channel_id="
  # invidious
  # BASEURL="https://vid.puffyan.us/feed/channel/"
  # BASEURL="https://invidious.snopyta.org/feed/channel/"
  # BASEURL="https://invidious.kavin.rocks/feed/channel/"
  # BASEURL="https://inv.riverside.rocks/feed/channel/"
  URL=$url
  curl -Ls "$URL" > /dev/null || URL="https://www.youtube.com/$URL"
  # Actually getting the ID
  ID="$(curl -Ls "$URL" | grep -o -P '.{0,0}channel_id.{0,25}' | sed '$!d' | cut -c 12-35)"
  [ -z "$ID" ] && say "Channel not found." && exit 1
  #printf "%s\n" $BASEURL$ID
  url="$BASEURL$ID"
}

# WIP
find_rss() {
  echo "$url" | grep -Pq "(http|https)://" || url="https://$url"
  address="$(echo "$url" | sed 's/\/$//')"
  #domain="$(echo "$url" | grep -Po '(http://|https://)(?:[^./]+[.])*[^\./]*\.[^\./]*(?=/)')"
  domain="$(echo "$url" | grep -Po '(http://|https://)(?:[^./]+[.])*[^\./]*\.[^\./]*')"
  read atom < <(curl -Ls "$url" | pup 'link[href][type*="atom+xml"] attr{href}')
  read rss < <(curl -Ls "$url" | pup 'link[href][type*="rss+xml"] attr{href}')
  [ -n "$atom" ] && url="$atom"
  [ -n "$rss" ] && url="$rss"
  [ -z "$atom" ] && [ -z "$rss" ] && say "RSS not found." "$url" && exit 1
  if echo "$url" | grep -q '^/' ; then
      location="$(echo "$url" | sed 's/^\///')"
      url="$domain/$location"
  fi
  echo "$url" | grep -Pq "(http|https)://" || url="$address/$url"
}

# WIP
find_fedi_rss() {
echo "$handle" | grep "^@.*@*\.*" || say "not a handle" "$url" && exit 1

}

add_yt_rss() {
  get_yt_rss
  add_rss
}

execute_command() {
if echo "$url" | grep -q "^https://www.youtube.com/feeds/videos.xml?*" ; then
  add_rss
elif echo "$url" | grep -q "^https://www.youtube.com/*" ; then
  add_yt_rss
elif echo "$url" | grep -q "https*://\S\+\.[A-Za-z]\+\S*" ; then
  add_rss
elif echo "$url" | grep -q "^@.*" ; then
  add_yt_rss
else
  url="$(grep -sEom1 '<[^>]+(rel="self"|application/[a-z]+\+xml)[^>]+>' "$url" | grep -Eo "(http|https)://[a-zA-Z0-9./?=_%:-]*")"
    echo "$url" | grep -Eq "(http|https)://[a-zA-Z0-9./?=_%:-]*" && add_rss ||
  say "That doesn't look like a full URL." "$url"
#TODO be more verbose
fi
}

execute_loop() {
url="$1" && execute_command
}

case "$1" in
  -h|--help)
printf "Usage: rssadd [RSSURL | YTURL | @Username | XMLFILE | --file FILE | --scan WEBPAGE ]

USAGE
    -s, --scan
      scan webpages for rss or atom links.
      format can be a domain name or http/https links
    --file
      input from file seperated with lines\n"
     exit 0
  ;;
  -s|--scan)
    url="$2"
    nothing "$url"
    find_rss
    execute_command
    exit 0
  ;;
  --file)
    nothing "$2"
    while IFS= read -r line; do
    execute_loop "$line"
    done < "$2"
    exit 0
  ;;
esac

url="$1"
execute_command