#!/bin/bash
#                   /usr/local/bin/recently_linked
# https://crystalfaeries/posix/bin/recently_linked
# celeste:crystalfaery RECENTLY_LINKED 2017-09-16 06:04:10+00:00
# recently_linked generates a webpage indexing by date (inverse order)
# the URLs linked-to from webpages in the blog generated by chronicle (ignores rest of website)
# this is primarily invoked during the run of /usr/local/bin/blog,
# so that upon each regeneration of the blog we update the link date page.
# NOTE: first hack of code will fail to see more than one URL per line of text.

# The DATABASE of external links is simply two columns of text:
# 1:	UNIX TimeStamp
# 2:	URL
DATABASE=${HOME}/crystalfaeries.net/links.txt
touch	${DATABASE}				|| exit 1
# Update the URL DataBase which is sorted oldest first to newest last:
for URL in $(grep http ${home}/crystalfaeries.net/src/*.txt | grep -v http://crystalfaeries.net/ | grep -v https://crystalfaeries.net/ | sed 's/^.*http:\/\//http:\/\//g ; s/^.*https:\/\//https:\/\//g ; s/".*$//g ; s/<\/a>.*$//g');
do
	grep -i	"${URL}"	${DATABASE}	>  /dev/null
	if [ $? -ne 0 ]
	then
		echo "`date +%s`	${URL}"	>> ${DATABASE}
	fi
done

# Generate the HTML page:
echo -e "<html><head><title>Recently Linked</title></head><body bgcolor="#000000" text="#FFFFFF" vlink="#FFFFFF" alink="#333333" link="#CCCCCC"><a href="/fae/"><h1 align="center">Recently Linked</h1></a>" \
						>  /home/crystalfaeries.net/recently_linked.html	# Title and Header
echo -e "<a href="/recently_updated.html"><img src="/clipart/clock.png" alt="[clock]" align="right" width="219" height="219"></a>" \
						>> /home/crystalfaeries.net/recently_linked.html	# Title and Header
echo -e "<p>Date______\tTime-Timezone\tURL<br>" \
						>> /home/crystalfaeries.net/recently_linked.html	# Column Headings

# read URLs newest to oldest
tac ${DATABASE}	|	\
while	read STAMP URL
do
	echo "$(date --rfc-3339=seconds --date=@${STAMP})	<a href=\"${URL}\">${URL}</a><br>"	\
						>> /home/crystalfaeries.net/recently_linked.html
done

echo -e "</p>"					>> /home/crystalfaeries.net/recently_linked.html
cat	${home}/crystalfaeries.net/README.html	>> /home/crystalfaeries.net/recently_linked.html
exit	$?



syntax highlighted by Code2HTML, v. 0.9.1