#!/bin/bash
#                       /usr/local/bin/redate_alpha
# https://crystalfaeries.net/posix/bin/redate_alpha
# celeste:crystalfaery REDATE_ALPHA 2018-01-03 02:08:05+00:00
# redate alpha articles is designed to auto-redate
# the Date:s of alphabetically named articles
# within https://crystalfaeries.net/fae/archive/
# to cause them to display in alphabetical order
# when in fact they are filed by date.
# 
# archive title	Date______	Time__
# 00	0*	1970-01-01	00:00+
# 00	1*	1970-01-01	01:00+
# 00	2*	1970-01-01	02:00+
# 00	3*	1970-01-01	03:00+
# 00	4*	1970-01-01	04:00+
# 00	5*	1970-01-01	05:00+
# 00	6*	1970-01-01	06:00+
# 00	7*	1970-01-01	07:00+
# 00	8*	1970-01-01	08:00+
# 00	9*	1970-01-01	09:00+
# 01	A*	1970-01-01	10:00+
# 01	B*	1970-01-01	10:30+
# 02	C*	1970-02-01	10:00+
# 02	D*	1970-02-01	10:30+
# 03	E*	1970-03-01	10:00+
# 03	F*	1970-03-01	10:30+
# 04	G*	1970-04-01	10:00+
# 04	H*	1970-04-01	10:30+
# 05	I*	1970-05-01	10:00+
# 05	J*	1970-05-01	10:30+
# 06	K*	1970-06-01	10:00+
# 06	L*	1970-06-01	10:30+
# 07	M*	1970-07-01	10:00+
# 07	N*	1970-07-01	10:30+
# 08	O*	1970-08-01	10:00+
# 08	P*	1970-08-01	10:30+
# 09	Q*	1970-09-01	10:00+
# 09	R*	1970-09-01	10:30+
# 10	S*	1970-10-01	10:00+
# 10	T*	1970-10-01	10:30+
# 11	U*	1970-11-01	10:00+
# 11	V*	1970-11-01	10:20+
# 11	W*	1970-11-01	10:40+
# 12	X*	1970-12-01	10:00+
# 12	Y*	1970-12-01	10:20+
# 12	Z*	1970-12-01	10:40+
help=47	# this file's line# -1

# work in the blog source directory wherein the files contain publishing dates
cd	~/crystalfaeries.net/src

case $# in
0)	# no arguments: redate all alpha articles unconditionally
	old_prefix="@"	# track changes of prefix
	let increment=0	# increment by 1 for each file of same prefix
	for the_file in $(find . -name '[a-z]*' | sed 's/^\.\///' | sort)	# we're ignoring (partially) numeric prefixes
	do
		#the_day="`head -n 1		${the_file}	| cut -f2`"
		prefix=$(echo "${the_file}"|cut -c1)	# first letter of file name
		if [ ${old_prefix} == ${prefix} ]
		then	# THIS LOGIC WILL FAIL WHEN WE HAVE MORE THAN 10 FILES OF SAME PREFIX
			let increment+=1
		else	#   increment can range 00 - 59 in seconds field
			let increment=0	# range: 0 - 09 by ACTUAL CODE logic
			old_prefix="${prefix}"	# we have a new file-name prefix
		fi
	    case ${prefix} in
	0|1|2|3|4|5|6|7|8|9)
		the_day="1970-01-01"
		the_time="0${prefix}:00:0${increment}+00:00"
		;;
	a)
		the_day="1970-01-01"
		the_time="10:00:0${increment}+00:00"
		;;
	b)
		the_day="1970-01-01"
		the_time="10:30:0${increment}+00:00"
		;;
	c)
		the_day="1970-02-01"
		the_time="10:00:0${increment}+00:00"
		;;
	d)
		the_day="1970-02-01"
		the_time="10:30:0${increment}+00:00"
		;;
	e)
		the_day="1970-03-01"
		the_time="10:00:0${increment}+00:00"
		;;
	f)
		the_day="1970-03-01"
		the_time="10:30:0${increment}+00:00"
		;;
	g)
		the_day="1970-04-01"
		the_time="10:00:0${increment}+00:00"
		;;
	h)
		the_day="1970-04-01"
		the_time="10:30:0${increment}+00:00"
		;;
	i)
		the_day="1970-05-01"
		the_time="10:00:0${increment}+00:00"
		;;
	j)
		the_day="1970-05-01"
		the_time="10:30:0${increment}+00:00"
		;;
	k)
		the_day="1970-06-01"
		the_time="10:00:0${increment}+00:00"
		;;
	l)
		the_day="1970-06-01"
		the_time="10:30:0${increment}+00:00"
		;;
	m)
		the_day="1970-07-01"
		the_time="10:00:0${increment}+00:00"
		;;
	n)
		the_day="1970-07-01"
		the_time="10:30:0${increment}+00:00"
		;;
	o)
		the_day="1970-08-01"
		the_time="10:00:0${increment}+00:00"
		;;
	p)
		the_day="1970-08-01"
		the_time="10:30:0${increment}+00:00"
		;;
	q)
		the_day="1970-09-01"
		the_time="10:00:0${increment}+00:00"
		;;
	r)
		the_day="1970-09-01"
		the_time="10:30:0${increment}+00:00"
		;;
	s)
		the_day="1970-10-01"
		the_time="10:00:0${increment}+00:00"
		;;
	t)
		the_day="1970-10-01"
		the_time="10:30:0${increment}+00:00"
		;;
	u)
		the_day="1970-11-01"
		the_time="10:00:0${increment}+00:00"
		;;
	v)
		the_day="1970-11-01"
		the_time="10:20:0${increment}+00:00"
		;;
	w)
		the_day="1970-11-01"
		the_time="10:40:0${increment}+00:00"
		;;
	x)
		the_day="1970-12-01"
		the_time="10:00:0${increment}+00:00"
		;;
	y)
		the_day="1970-12-01"
		the_time="10:20:0${increment}+00:00"
		;;
	z)
		the_day="1970-12-01"
		the_time="10:40:0${increment}+00:00"
		;;
	H)	# e.g. HEADER.txt
		the_day="2018-12-31"
		the_time="13:59:59+00:00"
		;;
	*)	echo 	"$0: CODING ERROR -1" 1>&2
		exit	255	# Error -1
		;;
	    esac
	echo "Date:	${the_day} ${the_time}"	>  $$.${the_file}
	tail -n +2	${the_file}		>> $$.${the_file}
	mv					   $$.${the_file}	\
			${the_file}		# Updated
	done
	;;
1)	# one argument: could be many things:
    while [ "${1}" != "" ]
    do
	case "${1}" in
	-v|--version)	# display version info
		head -n 4 $0 | tail -n 1
		exit	0
		;;
	-h|--help)	# display help info
		head -n ${help} $0
		exit	0
		;;
	-l|--list)	# list the existing dates:
		echo	"Date______ Time__________	File__________"
		for the_file in [a-z]*.txt
		do
			 the_day="`head -n 1		${the_file}	| cut -f2`"
			the_time="`head -n 1		${the_file}	| cut -f3`"
			echo "${the_day} ${the_time}	${the_file}"
		done
		echo	"Date------ Time----------	File----------"
		shift	# we COULD do this before changing the dates...
		;;
	*)	# anything else
		echo "$0: unknown argument: ${1}" >&2
		exit
		;;
	esac
    done
	;;
2)	# two arguments: ummm... are we gonna have this?
	echo "$0: two many arguments." >&2
	exit
	;;
*)	# many arguments: nope!
	echo "$0: too many arguments." >&2
	exit
	;;
esac

exit	$?	# Return Status

