#!/bin/sh
set -eu

export LANG=POSIX.UTF-8


normalize() {
	xargs realpath --relative-to=. | sort
}

pages() {
	find src/pages/*/*.adoc -type f                          | normalize
}

articles() {
	find src/collections/*/*/*/ -type f -name '*.adoc'       | normalize
}

slides() {
	find src/slides/*/*/ -type f -name '*.adoc'              | normalize
}

indexes() {
	find src/collections/*/*/index.adoc                      | normalize
}

categories() {
	grep -l '^:type: categories$' src/collections/*/*/*.adoc | normalize
}

media() {
	find src/content/ \( -type f -and \! -type l \) -and \( \
			-name '*.flac' -or \
			-name '*.ogg'  -or \
			-name '*.webm'     \
		\)
}

tarballs() {
	find src/content/ \( -type f -and \! -type l \) -and \( \
			-name '*.tar.gz'   \
		\)
}

extras() {
	media
	tarballs
}

listings() {
	indexes
	categories
}

files() {
	pages
	articles
	listings
}


pages                                      | varlist      'pages.adoc'
articles                                   | varlist   'articles.adoc'
slides                                     | varlist     'slides.adoc'
categories                                 | varlist 'categories.adoc'
indexes                                    | varlist    'indexes.adoc'
indexes | sed 's|/index\.adoc$|/feed.xml|' | varlist 'feeds.xml'
find src/content/img/ -name '*.svg'        | varlist 'images.svg'
media                                      | varlist 'sources.media'
tarballs                                   | varlist 'sources.tarballs'
extras                                     | varlist 'sources.extras'
find po/*.po po/*.pot                      | varlist 'sources.po'


{
	files   | sed 's/^\(.*\)\.adoc$/\1.html/'
	files   | sed 's/^\(.*\)\.adoc$/\1.snippets/'
	slides  | sed 's/^\(.*\)\.adoc$/\1.pdf/'
	indexes | sed 's|^\(.*\)/index\.adoc$|\1/feed.xml|'
	media   | sed 's/^\(.*\)$/\1.torrent/'
} | sed 's/^\(.*\)$/\1.gz:\t\1/'
printf '\n'

files    | sed 's/^\(.*\)\.adoc$/\1.htmlbody\t\1.snippets\t\1.conf:\t\1.adoc/'
files    | sed 's/^\(.*\)\.adoc$/\1.html:\t\1.conf\t\1.htmlbody/'
printf '\n'

files    | sed 's/^\(.*\)\.adoc$/\1.updatedat-check:\t\1.conf/'
files    | sed 's/^\(.*\)\.adoc$/\1.links-internal-check:\t\1.links/'
files    | sed 's/^\(.*\)\.adoc$/\1.caslinks:\t\1.links/'
printf '\n'

articles | sed 's/^\(.*\)\.adoc$/\1.feedentry:\t\1.conf\t\1.htmlbody/'
articles | sed 's/^\(.*\)\.adoc$/\1.sortdata:\t\1.conf/'
articles | sed 's/^\(.*\)\.adoc$/\1.categorydata:\t\1.conf/'
printf '\n'

listings | sed 's/^\(.*\)\.adoc$/\1.htmlheader\t\1.htmlfooter:\t\1.conf/'
listings | sed 's/^\(.*\)\.adoc$/\1.htmllisting:\t\1.conf/'
listings | sed 's/^\(.*\)\.adoc$/\1.html:\t\1.htmlheader\t\1.htmlfooter/'
listings | sed 's/^\(.*\)\.adoc$/\1.html:\t\1.htmllisting\t\1.htmlbody/'
printf '\n'

media    | sed 's/^\(.*\)$/\1.torrent:\t\1/'
printf '\n'

slides   | sed 's/^\(.*\)\.adoc$/\1.ps:\t\1.adoc/'
slides   | sed 's/^\(.*\)\.adoc$/\1.pdf:\t\1.ps/'
printf '\n'



collectionentries() {
	langlink="$1"
	colllink="$2"
	lang="$(basename "$langlink")"
	c="$(printf '%s' "$colllink" | normalize)"
	plural="$(cat src/names/categories/"$lang".txt)"

	printf '\n\n'
	name="$(basename "$c")"
	art=articles."$lang"
	find "$c"/*/ -type f -name '*.adoc' | varlist "$art.$name.adoc"
	echo "$art.$name.sortdata     = \$($art.$name.adoc:.adoc=.sortdata)"
	echo "$art.$name.indexentry   = \$($art.$name.adoc:.adoc=.indexentry)"
	echo "$art.$name.feedentry    = \$($art.$name.adoc:.adoc=.feedentry)"
	echo "$art.$name.categorydata = \$($art.$name.adoc:.adoc=.categorydata)"

	printf '%s/sortdata.txt:\tdeps.mk\n' "$c"
	printf '\tprintf %s $(%s.%s.sortdata) > $@\n\n' "'%s\n'" "$art" "$name"

	listings="
		feed.xml
		index.htmllisting
		$plural.htmllisting
		$plural.txt
	"
	for lst in $listings; do
		printf '%s/%s:\t%s/sortdata.txt\n'   "$c" "$lst" "$c"
		printf '%s/%s:\t$(%s.%s.sortdata)\n' "$c" "$lst" "$art" "$name"
	done

	printf      '%s/index.htmllisting\t'   "$c"
	printf '%s/%s.htmllisting:\t'          "$c" "$plural"
	printf '$(%s.%s.indexentry)\n'         "$art" "$name"

	printf '%s/%s.txt:\t'                  "$c" "$plural"
	printf '$(%s.%s.categorydata)\n'       "$art" "$name"

	printf '%s/%s.txt\t'                   "$c" "$plural"
	printf '%s/feed.xml:\t'                "$c"
	printf '$(%s.%s.feedentry)\n'          "$art" "$name"

	printf '%s/%s.htmllisting\t'           "$c" "$plural"
	printf '%s/%s.xml:\t'                  "$c" "$plural"
	printf '%s/%s.txt\n'                   "$c" "$plural"

	printf '%s/%s.xml.gz:\t%s/%s.xml\n' "$c" "$plural" "$c" "$plural"
}


for langlink in src/collections/*; do
	for colllink in "$langlink"/*; do
		collectionentries "$langlink" "$colllink"
	done
done
