#!/bin/bash
#			/usr/local/bin/alphabetical_articles
# https://crystalfaeries.net/posix/bin/alphabetical_articles
# celeste@crystalfaeries.net ALPHABETICAL_ARTICLES 2020-08-27 21:25:54+00:00
# Edit Interactively the Dates of Articles filed Alphabetically by Date

cd	$HOME/crystalfaeries.net/src/	# where the source files live
for f in			     {,[0-9]}[a-z]*
do						# generate the list
	echo -n "$(head -n 1 ${f} | sed 's/Date:	//')	"
	echo "$(basename     ${f})"
done | sort -k3	>	.alphabetical.txt

vi			.alphabetical.txt	# edit the list

while read the_date the_time the_filename
do						# update the files
	if	[ "${the_date}" == "" ]
	then
		echo	"missing date"
		exit	1		# Debug
	fi
	if	[ "${the_time}" == "" ]
	then
		echo	"missing time"
		exit	2		# Debug
	fi
	if	[ "${the_filename}" == "" ]
	then
		echo	"missing filename"
		exit	3		# Debug
	fi
#	Did we change this file's Date?
	head -n 1 "${the_filename}" >	/tmp/alphabetical_article_old.txt
	echo	"Date:	${the_date} ${the_time}" >	/tmp/"${the_filename}"
	diff						/tmp/"${the_filename}"	\
				  	/tmp/alphabetical_article_old.txt >& /dev/null
	if	[ $? -ne 0 ]
	then	# the file's Date was changed, so replace it
		tail -n +2	"${the_filename}"	\
		>>	   /tmp/"${the_filename}"
		mv	   /tmp/"${the_filename}"	\
				"${the_filename}"
	fi
done	<		.alphabetical.txt
exit	$?
####################################################################
# Alphabetical Filing of Named Blog Articles is by Date within 1971:
0 -> 1971/00/01 10:00:00
1 -> 1971/00/01 11:00:00
2 -> 1971/00/01 12:00:00
3 -> 1971/00/01 13:00:00
4 -> 1971/00/01 14:00:00
5 -> 1971/00/01 15:00:00
6 -> 1971/00/01 16:00:00
7 -> 1971/00/01 17:00:00
8 -> 1971/00/01 18:00:00
9 -> 1971/00/01 19:00:00
a -> 1971/01/01 10:00:00
b -> 1971/01/01 10:30:00
c -> 1971/02/01 10:00:00
d -> 1971/02/01 10:30:00
e -> 1971/03/01 10:00:00
f -> 1971/03/01 10:30:00
g -> 1971/04/01 10:00:00
h -> 1971/04/01 10:30:00
i -> 1971/05/01 10:00:00
j -> 1971/05/01 10:30:00
k -> 1971/06/01 10:00:00
l -> 1971/06/01 10:30:00
m -> 1971/07/01 10:00:00
n -> 1971/07/01 10:30:00
o -> 1971/08/01 10:00:00
p -> 1971/08/01 10:30:00
q -> 1971/09/01 10:00:00
r -> 1971/09/01 10:30:00
s -> 1971/10/01 10:00:00
t -> 1971/10/01 10:30:00
u -> 1971/11/01 10:00:00
v -> 1971/11/01 10:20:00
w -> 1971/11/01 10:40:00
x -> 1971/12/01 10:00:00
y -> 1971/12/01 10:20:00
z -> 1971/12/01 10:40:00
