#!/bin/bash
#                      /usr/local/bin/index2header
# http://crystalfaeries.net/posix/bin/index2header
# celeste:crystalfaery 2015-09-15 09:17:25+00:00
# Convert index.htm and index.html files to HEADER.html files,
# thereby exposing otherwise hidden contents of a directory,
# when viewed via a web browser.
# This is particularly useful when a downloaded website
# has been altered in a manner which breaks all its links,
# so that it can still be navigated without repairing the HTML.
# The inspiration for this is my own /home/downloads
# directory, full of downloaded websites, but upon which,
# I have run fdedupe (which runs name_tidy -r) and / or name_tidy -r
# which lower-cases and otherwise alters file names,
# thereby assuring that links between files become broken.
# (why would I do that? because I live on the command line :-)
# We also want to merge files if we have both, i.e.:
# append an existing index.html file to an existing HEADER.html file
# and then delete the index.html file.
# Because I'm starting with intent to fix my whole downloads directory,
# I'm going to assume and execute recursively, whereas
# it would be nice to have to invoke that with a "-r" or "-R" or "--recursive"
# Initially we hard-code things to prevent errors:

cd /home/downloads/
cd -P .
echo -n "convert index.html files to HEADER.html files RECURSIVELY in `pwd`?: " >&2
read	 REPLY
if [ ! "$REPLY" = "yes" ]
then
	echo "USAGE: reply 'yes' to proceed else we exit." >&2
	exit
fi
for dir in $( find . -depth -type d -print )
do	# iterate directories
	cd "$dir"
	cat index.html index.htm >> HEADER.html	&&	\
	rm  index.html index.htm		||	\
	echo "FAILURE in `pwd`" >&2
done


syntax highlighted by Code2HTML, v. 0.9.1