#!/bin/bash
#                       /usr/local/bin/recently_bookmarked
# https://crystalfaeries.net/posix/bin/recently_bookmarked
# celeste:crystalfaery RECENTLY_BOOKMARKED 2021-04-04 19:52:03+00:00
# original version from http://commandlinefu.com
# list firefox bookmarks by date in format: "dateAdded	url"

if [ $# -eq 0 ]
then	# called with no arguments (e.g. by crontab):	generate website file while firefox browser is not running...
	# @reboot	/usr/local/bin/recently_bookmarked
	# 55 01 * * *	/usr/local/bin/recently_bookmarked
	until [ "$(ps -ef --forest | grep "/usr/lib/firefox-esr/firefox-esr" | grep -v grep | sed "s/^$(whoami) *//;s/ .*$//")" == "" ]
	do
		sleep 900	# check every 15 minutes until FIREFOX is no longer running because it KEEPS the DATABASE LOCKED OPEN
	done

	# generate the webpage (which auto-refreshes daily):
	echo 'Content-Type: text/html'		>	$HOME/public_html/recently_bookmarked.html
	echo ''					>>	$HOME/public_html/recently_bookmarked.html
	echo -e	'<html><head><title>Recently Bookmarked</title><meta http-equiv="refresh" content="86400;url=/recently_bookmarked.html"></head><body bgcolor="#000000" text="#FFFFFF" vlink="#FFFFFF" alink="#333333" link="#CCCCCC"><a href="/"><h1 align="center">Recently Bookmarked</h1></a>'	\
						>	$HOME/public_html/recently_bookmarked.html
	echo -e	"<a href="/recently_updated.html"><img src="/imgs/clipart/portrait/clock.png" alt="[clock]" align="right" width="219" height="219"></a><pre>"	\
						>>	$HOME/public_html/recently_bookmarked.html
	echo 'YYYY-MM-DD.HH-MM-SS	URL___'	>>	$HOME/public_html/recently_bookmarked.html
	echo '____-__-__.__-__-__	______'	>>	$HOME/public_html/recently_bookmarked.html

	sqlite3	${HOME}/.mozilla/firefox/*.[dD]efault/places.sqlite	\
	"SELECT strftime ('%Y-%m-%d.%H-%M-%S', dateAdded/1000000, 'unixepoch', 'localtime'),url FROM moz_places, moz_bookmarks WHERE moz_places.id = moz_bookmarks.fk ORDER BY dateAdded;" | tac | sed 's/|/	/g'		|	uniq	|\
	grep -v 'place:' | grep -v "file:///"	>>	$HOME/public_html/recently_bookmarked.html
	echo '</pre>'				>>	$HOME/public_html/recently_bookmarked.html
	cat "${HOME}"/public_html/README.html	>>	$HOME/public_html/recently_bookmarked.html
	echo '</body></html>'			>>	$HOME/public_html/recently_bookmarked.html

#	while we're playing with bookmarks, purge ICONs from our manually saved bookmarks file:
	mv "${HOME}"/.mozilla/firefox/bookmarks.html	"${HOME}"/.mozilla/firefox/bookmarks.html-
	sed 's/ ICON="[^"]*"//g;s/ ICON_URI="[^"]*"//g'<"${HOME}"/.mozilla/firefox/bookmarks.html- \
	>  "${HOME}"/.mozilla/firefox/bookmarks.html
	rm						"${HOME}"/.mozilla/firefox/bookmarks.html-
	firefox "${HOME}"/public_html/recently_bookmarked.html	</dev/null>&/dev/null&disown %1
else	# this is user requesting to review the file interactively
	tail	-n +5	$HOME/public_html/recently_bookmarked.html	\
	| cut	-f2-							\
	| while read URL
	do
		echo "viewing:	${URL}"
		firefox	       "${URL}"
		echo -n	"view next older bookmark?(Y/n):	"
		read disposition < /dev/tty
		case "${disposition}" in
NO | no)	exit 0;;
N | n)		exit 0;;
*)		;;
		esac
	done
fi
exit $?

