#!/bin/bash
#                       /usr/local/bin/expire
# https://crystalfaeries.net/posix/bin/expire
# celeste:crystalfaery EXPIRE 2020-07-14 01:46:59+00:00
# This script implements part of self-destructing blog articles.
# It is an adjunct to the "blog" script which wraps chronicle.
# Its intended useage is to add a header-line to the html source file, structured:

# 1 Date:   2017-07-15 16:59:37+00:00
# 2 Expiry: 1500137981
# 3
# 4 <p	align="justify">
# 5	A paragraph.
# 6 </p>

# To generate the Expiry line, while editing the source file
# with cursor on the Date: line execute the vi command:
# :r!expire 2017-08-15
# where the argument is the date to expire
# or
# :r!expire n
# (where   the argument "n" is expiry in days)
# (without the argument "n" expiry is in 32 days)

# To review articles which contain Expiry: entries, at a command line execute:
# expire -l
# or
# expire --list

# To edit articles which contain Expiry: entries, at a command line execute:
# expire -e
# or
# expire --edit
let help=33	# number of this line -1
cd	# There's no place like $HOME except ~

echo	'EXPIRE BEGIN' 2>> $HOME/.chronicle.log 1>&2
# EXPIRE Articles containing "Expiry:" entry
for f in $(grep '^Expiry:	' "$HOME"/crystalfaeries.net/src/*.txt | sed 's/:.*$//')
do
	if [ $(date +%s) -gt $(grep '^Expiry:	' ${f} | cut -f2-) ]
	then	# this article is beyond its expiry timestamp
		echo "Expired:	${f}"	>>	$HOME/.chronicle.log 2>&1
		delete		${f}	>>	$HOME/.chronicle.log 2>&1
	else	# this article is within its limited lifespan
		echo "Visible:	${f}"	>>	$HOME/.chronicle.log 2>&1
	fi
done
# UNLESS THE USER ERRED in article creation, Expiry SHOULD post-date Publish date, RIGHT?
# So above we only expired already published articles, not Future Publish articles we may now reveal.
# This creates a minimum exposure of once for any article before it expires, per run of this script.

echo	'REVEAL BEGIN' 2>> $HOME/.chronicle.log 1>&2
# NOTE: User may PREVIEW hidden files with /usr/local/bin/preview in $HOME/crystalfaeries.net/fay/
# User may generate "tomorrow's" blog entry as a hidden file
# which is not published by chronicle until first run on publish date.
pushd	"$HOME"/crystalfaeries.net/src/	2>/dev/null 1>&2
for f in .20*.fae.txt; do	# YES, CODE BREAKS FOR DATES PAST 2099-12-31 :-)
	  hidden="$(echo ${f}		| sed 's/\.fae\.txt$//')"
	revealed="$(echo ${hidden}	| sed 's/^\.//')"
    if	[ "${revealed}" > "$(now	| sed 's/\ .*$//')" ]
    then	# next UNrevealed future file displayed
#	    preview "${hidden}.fae.txt"				2>> $HOME/.chronicle.log 1>&2 <	/dev/null &
	    break # out of loop...
    else	# do NOT overwrite the REVEALED file with a later-dated zero-size (deleted) UNrevealed file
	rsync -auvH	$hidden.fae.txt $revealed.fae.txt	2>> $HOME/.chronicle.log 1>&2	\
	&& delete	$hidden.fae.txt				2>> $HOME/.chronicle.log 1>&2	\
	&& touch			$revealed.fae.txt	2>> $HOME/.chronicle.log 1>&2	\
	&& touch	.					2>> $HOME/.chronicle.log 1>&2	# source was updated
    fi
done
popd	2>/dev/null 1>&2

case $# in
0 )	# No argument defaults to epxire a month from now
	let expiry=32						# default to a month
	echo "Expiry:	$[`date +%s` + 60*60*24*${expiry}]"	# 60 seconds per minute, 60 minutes per hour, 24 hours per day
	exit
	;;
1 )	# Single argument for many reasons:
	case "${1}" in
	-h | --help )	# help
		head -n ${help} $0
		exit
		;;
	-v | --version )	# version
		head -n 4 $0 | tail -n 1
		exit
		;;
	-l | --list )	# list existing Expiry entries
		echo	"Expiration_Date__________	File_______________"
		for article in $( grep Expiry: crystalfaeries.net/src/{,.}*.txt | sort -n -k2 | cut -f 1 | sed 's/:.*$//' )
		do
			echo "$( date -u --rfc-3339=seconds --date=@$( grep Expiry: ${article} | cut -f 2 ))	${article}"
		done
		exit	$?
		;;
	-e | --edit )	# edit existing Expiry entries
		vi $( grep Expiry: crystalfaeries.net/src/{,.}*.txt | sort -n -k2 | cut -f 1 | sed 's/:.*$//' )
		exit	$?
		;;
	* )	if echo "${@}" | grep "-" >& /dev/null
		then	# This is a date specification of the date to expire
			let expiry=$(date --date="$@" +%s)
			echo "Expiry:	${expiry}"				# expire on specified date
			exit
		else	# Argument is the number of days from today until expiry
			let expiry=`echo "${1}" | tr -d -c '[:digit:]'`		# sanitize input argument integer number of days retention
			echo "Expiry:	$[`date +%s` + 60*60*24*${expiry}]"	# 60 seconds per minute, 60 minutes per hour, 24 hours per day
			exit
		fi
		;;
	esac
	;;
* )	# huh?
	echo "$0 does not cognize more than one argument: $@" >&2
	exit	254
	;;
esac
exit	255

