#!/bin/bash
#                       /usr/local/bin/rundog
#  http://crystalfaeries.net/posix/bin/rundog
# celeste:crystalfaery RUNDOG 2020-09-15 05:03:51+00:00
# run RAWDOG to update our news page unless there's already a copy running
# or with -a | --add	option Add	URL(s) to	rawdog's config and newsboat's urls
# or with -r | --remove	option Remove	URL(s) from	rawdog's config and newsboat's urls
# or with -g | --grep	"string" shows  URL(s) from	rawdog's config and newsboat's urls
# or with -e | --edit    option Edit	URL(s) in       rawdog's config and newsboat's urls
# or with -p | --publish option publish the combined subscriptions list
# and the -h | --help	and	-v | --version	options should work cannonically.
# NOTE: alteration of subscriptions via -a or -r auto-triggers auto-publish
# EXAMPLE:   > rundog  -a  https://crystalfaeries.net/fae/index.rss
#          Subscribed:     https://crystalfaeries.net/fae/index.rss
#	     > rawdog  -r  https://crystalfaeries.net/fae/index.rss
# will unsubscribe rawdog but leave newsboat subscribed...
# thus i the local user still see the feed, but no longer republish it via rawdog.
let help=17	# number of this line - 1

case $# in
0)
	/usr/bin/rawdog -Wuw		>  "${HOME}"/.rawdog/log.txt 2>&1	# update rawdog page
	sed -i 's/<title>rawdog<\/title>/<title>crystalfaeries gaian news<\/title>/'	"${HOME}"/crystalfaeries.net/rawdog.html	\
					>> "${HOME}"/.rawdog/log.txt 2>&1	# custom` rawdog title
	sed -i 's/<h1>rawdog<\/h1>/<a href="https:\/\/www.mayanmajix.com\/TZOLKIN\/DT\/DT.html"><img src="\/imgs\/astro\/moon.png" align="right" width="32" height="32"><\/a><a href="https:\/\/crystalfaeries.net\/rawdog.rss"><img src="\/imgs\/clipart\/portrait\/rss-small.png" align="left" width="32" height="32"><\/a><h1><a href="https:\/\/crystalfaeries.net\/">crystalfaeries<\/a> <a href="https:\/\/crystalfaeries.net\/newsfeeds.txt">gaian news<\/a><\/h1>/'		"${HOME}"/crystalfaeries.net/rawdog.html	\
					>> "${HOME}"/.rawdog/log.txt 2>&1	# custom rawdog header
	syncrawdog			>> "${HOME}"/.rawdog/log.txt 2>&1	# update server
    ;;
*)  case "${1}" in
	-a | --add )
		while shift # does this return an error code upon argument exhaustion?
		do
			# presumably rawdog's return code is real:
		    if [ "${1}" == "" ]
		    then
			exec rundog -p
		    else
			rawdog -a "${1}"	1> /tmp/.out.$$.txt 2> /tmp/.err.$$.txt
			case $? in
			0)	URL="$(cat /tmp/.err.$$.txt | sed 's/^Adding feed //;s/^Feed //;s/ is already in the config file$//')"
				if [ "$(echo "${URL}" | sed 's/^Cannot find any feeds in .*$/Cannot find any feeds in /' )" \
											  = "Cannot find any feeds in " ]
				then
					echo "${URL}"	1>&2
					exit 1
				fi
				echo	"Subscribed:	${URL}"	1>&2
				;;
			1)	echo	"Bad URL: $0 -a	${1}"	1>&2
				exit	1	# Bad URL
				;;
			*)	URL="$(cat /tmp/.out.$$.txt)"
				URL="$(cat /tmp/.err.$$.txt)"
				echo	"$0:	${URL}"		1>&2
				;;
			esac	# add the feed to newsboat's file of URLs
			echo	"$URL"			>  /tmp/.url.$$.txt
			cat	"${HOME}"/.newsboat/urls	>> /tmp/.url.$$.txt
			sort -u	-k2 -t/			   /tmp/.url.$$.txt \
			>	"${HOME}"/.newsboat/urls
#			vi	"${HOME}"/.newsboat/urls	   /tmp/.url.$$.txt	/tmp/.{err,out}.$$.txt
			rm	/tmp/.{err,out}.$$.txt	   /tmp/.url.$$.txt	# cleanup
		    fi
		done
		vi	"${HOME}"/.rawdog/config +32767	# manually alter feed update period
		exec rundog -p
	;;
	-r | --remove )
		shift	# discard the option and execute upon the argument URLs
		until [ "${1}" == "" ]
		do
			grep	 "${1}"	"${HOME}"/.newsboat/urls >> "${HOME}"/.newsboat/urls-	# backup
			grep -v  "${1}"	"${HOME}"/.newsboat/urls	>	/tmp/.url.$$.txt	# remove URL from "${HOME}"/.newsboat/urls
			sort -u >	"${HOME}"/.newsboat/urls	<	/tmp/.url.$$.txt
			rawdog -r "${1}"	# make best effort				$ remove URL from "${HOME}"/.rawdog/config
		shift
		done
		exec rundog -p
	;;
	-p | --publish )
		# first extract the feeds from RAWDOG and convert to simple URLs as used in NEWSBOAT
		grep '^feed ' "${HOME}"/.rawdog/config | sed 's/^feed .* //' |	sort -u	\
		| sort -t/ -k2 > /tmp/$$.rawdog.config
		# from that list verify NEWSBOAT is subscribed to every URL RAWDOG subscribes to.
		for f in $(cat /tmp/$$.rawdog.config )
		do
			grep "${f}" "${HOME}"/.newsboat/urls > /dev/null || echo "${f}" >> "${HOME}"/.newsboat/urls
		done
		# now make a clean orderly list out of NEWSBOAT's URLs
									sort -u "${HOME}"/.newsboat/urls \
		| sort -t/ -k2 > /tmp/$$.newsboat.urls
		# notify the user of the difference, i.e. which URLs are only subscribed in NEWSBOAT?
		diff /tmp/$$.rawdog.config /tmp/$$.newsboat.urls > "${HOME}"/crystalfaeries.net/src/.newsfeed_diff.txt
		# now install the cleaned-up sorted meta-list into NEWSBOAT
		mv			   /tmp/$$.newsboat.urls	"${HOME}"/.newsboat/urls
		# now transform the meta-list into the published meta-list (with a little clean-up)
		echo "# celeste:crystalfaery monitors these newsfeeds via newsboat, and publishes" \
		>  "${HOME}"/crystalfaeries.net/newsfeeds.txt
		echo "# a subset thereof, as listed at bottom of the auto-refreshing page:" \
		>> "${HOME}"/crystalfaeries.net/newsfeeds.txt
		echo "# https://crystalfaeries.net/rawdog.html"	\
		>> "${HOME}"/crystalfaeries.net/newsfeeds.txt
		# filter to PG level:
		cat "${HOME}"/.newsboat/urls							\
		| grep -v heelslut								\
		| grep -v nylonsparade								\
		| grep -v yandy.com								\
		| sed 's/^http:/ http:/' >> "${HOME}"/crystalfaeries.net/newsfeeds.txt
		exit	$?
	;;
	-g | --grep )
		shift
		grep -H "${1}" "${HOME}"/.newsboat/urls "${HOME}"/.rawdog/config
		exit $?	# pau
	;;
	-e | --edit )
		vi "${HOME}"/.newsboat/urls "${HOME}"/.rawdog/config "${HOME}"/.newsboat/urls "${HOME}"/.rawdog/config
		exec rundog -p	# publish the new URLs
		exit 255	# how did you possibly return from an exec?
	;;
	-v | --version )
		head -n 4	"${0}"	| tail -n 1	1>&2
		exit
	;;
	-h | --help )
		head -n	$help	"${0}"			1>&2
		exit
	;;
	* )
		echo "$0 does not cognize ${1}"		1>&2
		shift
		exec	"${0}"	"${@}"	# puke on unrecognized options/arguments, but carry on...
	;;
    esac
    ;;
esac
exit	$?
