#!/bin/bash
#                       /usr/local/bin/expire
# https://crystalfaeries.net/posix/bin/expire
# celeste:crystalfaery EXPIRE 2017-08-25 23:38:27+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 n
# (where the argument "n" is expiry in days)
# or
# :r!expire 2017-08-15
# where the argument is the date to expire

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

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
		head -n ${help} $0
		exit
		;;
	--help)	# help
		head -n ${help} $0
		exit
		;;
	-v)	# version
		head -n 4 $0 | tail -n 1
		exit
		;;
	--version)	# version
		head -n 4 $0 | tail -n 1
		exit
		;;
	-l)	# list existing Expiry entries
		grep '^Expiry:'	/home/crystalfaeries.net/src/{.,}*.txt
		exit	$?
		;;
	--list)	# list existing Expiry entries
		grep '^Expiry:'	/home/crystalfaeries.net/src/{.,}*.txt
		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
			# One 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



syntax highlighted by Code2HTML, v. 0.9.1