#! /bin/sh
#
# Generate list of Wandystan’s legal acts from the old journal.
# Usage: ./genindex.sh > index.html
#
# Copyright (c) 2017 Paulina Laura Emilia <vilene@posteo.net>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

extract() {
	sed -ne "
		/^<$1>/ {
			s,^<$1>,,
			s,</$1>$,,
			p
			q
		}
	" "$2"
}

pluralise() {
	first=${1%% *}
	rest=${1#"$first"}

	case $first in
		*[eo])	first=${first%[eo]}a ;;
		*[ij]a)	first=${first%a}e ;;
		*a)	first=${first%a}y ;;
		*[kg])	first=${first}i ;;
		*)	first=${first}y
	esac

	rest=$(printf '%s\n' "$rest" | sed -Ee 's/(ow|jn).[[:>:]]/\1e/g;s/ nr [0-9]+$//g')
	printf '%s%s\n' "$first" "$rest"
}

# Create temporary directory for group files:
if tmp=$(mktemp -dt genindex.XXXXXX) && [ -d "$tmp" ]; then
	trap 'rm -rf "$tmp"' 0
else
	printf '%s: %s\n' "$0" 'Cannot create temporary directory' >&2
	exit 1
fi

# Extract act names to group files:
find . -maxdepth 1 -name 'akt,*.html' | sort -V | while IFS= read -r akt; do
	title=$(extract h1 "$akt")
	subtitle=$(extract h2 "$akt")
	type=${title%% z dnia*}

	printf '\t\t<li><a href="%s">%s <strong>%s</strong></a></li>\n' \
		"${akt#./}" "$title" "$subtitle" >> "$tmp/$type"
done

# Print header:
printf '%s\n\n' '<!DOCTYPE html>
<html lang="pl">
<head>
	<meta charset="utf-8">
	<meta name="generator" content="genindex.sh">
	<link rel="stylesheet" href="//wandystan.eu/varia/style_light.css" type="text/css">
	<style type="text/css">
		h1 {
			background: url("grafika/naglowek.gif") no-repeat top / auto;
			padding-top: 27.5%;
		}

		@media screen and (max-width: 740px) {
			h1 { background-size: 100%; }
		}
	</style>
	<title>Archiwum Dziennika Praw Mandragoratu Wandystanu 2004-2007</title>
</head>
<body>
	<h1>Archiwum Dziennika Praw Mandragoratu Wandystanu 2004-2007</h1>'

# Print index:
sec=1
printf '\t<h2>Spis treści</h2>\n\t<ol>\n'
for group in "$tmp"/*; do
	printf '\t\t<li><a href="#sec%d">%s</a></li>\n' \
		"$sec" "$(pluralise "${group##*/}")"
	: $(( sec = sec + 1 ))
done | iconv -f ISO8859-2 -t UTF-8
printf '\t</ol>\n\n'

# Print groups:
sec=1
for group in "$tmp"/*; do
	printf '\t<h2 id="sec%d">%s</h2>\n\t<ul>\n' \
		"$sec" "$(pluralise "${group##*/}")"
	cat "$group"
	printf '\t</ul>\n\n'
	: $(( sec = sec + 1 ))
done | iconv -f ISO8859-2 -t UTF-8

# Print footer:
printf '%s\n' '	<address>Lista wygenerowana przez <a href="genindex.sh">genindex.sh</a></address>
</body>
</html>'
