#!/bin/bash
#		       /usr/local/bin/oldest
# http://crystalfaeries.net/posix/bin/oldest
# celeste:crystalfaery OLDEST 2017-08-21 05:56:13+00:00
# Edit the oldest files we usually manage manually, ignoring files we have already seen.
# Combine with /usr/local/bin/newest

#	Configuration
remove=/bin/rm			# for solo operation
remove=/usr/local/bin/delete	# for LAN  operation
viewed=$HOME/documents/.dush.webpages.txt	# List of files already viewed, not to be shown again.
unhardlink "${viewed}"	# in case file and its backup got fdeduped

#	Initialization
appname="$(basename $0)"
tmpfile="$(mktemp /tmp/${appname}.XXXXXX)" || exit 1
trap "let exitval=$?; rm -f $tmpfile; exit $exitval" 0 1 2 15
touch "$viewed" "$viewed"-	|| exit -1	# verify we have write permissions

#	Find our target files

# in		/usr/local/bin	files may have a variety of file types, so just take them all
# nice ionice -c 3 find	/usr/local/bin -type f >		"$tmpfile" 2>/dev/null
nice find	/usr/local/bin -type f >		"$tmpfile" 2>/dev/null

# from the remaining directories we want any file named .txt or .htm*
for d in ~/txt/ /home/crystalfaeries.net/ /usr/local/src/txt/ /usr/local/doc/{txt,html} ;do
#	nice ionice -c 3 find "$d"/ \( -iname '*.txt' -o -iname '*.htm*' \) >> "$tmpfile"
	nice find "$d"/ \( -iname '*.txt' -o -iname '*.htm*' \) >> "$tmpfile"
done

# View from oldest to newest, ask to delete, and record having viewed
for f in $( ls -rt $( cat "$tmpfile" ) )
do
	grep "$f" "$viewed" > /dev/null
	if [[ $? = 0 ]]
	then
		echo previewed:	"$f"	1>&2
	else
		echo __viewing:	"$f"	1>&2
					# display the file and ask to delete
		echo "$f" | grep .html > /dev/null
		if [[ $? = 0 ]]
		then
		    /usr/bin/iceweasel	"$f"	2>/dev/null	&
		fi
		vi		"$f"
		echo ___viewed:	"$f"	1>&2
		"$remove" -i	"$f"	# you want I should remove this file? and the copy on my_twin?
	fi
	rm			"$viewed"-	|| exit $?
	mv 	"$viewed"	"$viewed"-	|| exit $?
	if [ -f		"$f" ]
	then					# user did not choose to remove the file
		echo "$f" >> "$viewed"		# record that we viewed the file
		sort -u      "$viewed"- > "$viewed"
	else					# user did     choose to remove the file
		echo __removed:	"$f"	1>&2
		grep -v "$f" "$viewed"- > "$viewed"
	fi
done
exit $?



syntax highlighted by Code2HTML, v. 0.9.1