#!/bin/bash

set -e

# usage: asciideps SOURCE.adoc [TARGET...]
#
# Emit a make rule hanging SOURCE's include:: and image: prerequisites off
# TARGET.  The caller names the target because it is not derivable from the
# source path: a Master document has no .html and its English PDF lands in
# objects/.  Defaults to the source stem, for a standalone run.
SRC=$1
[ -z "$SRC" ] && exit 0
[ -f "$SRC" ] || { echo "asciideps: '$SRC' not a file" >&2; exit 1; }
shift
TARGETS="$*"
[ -n "$TARGETS" ] || TARGETS="${SRC%.adoc}.html"

includestack=( )

includes () {
	DIR=$(dirname "$1")

	while IFS= read -r f; do
		# components_gen.adoc will contain only generated content
		case "$f" in
			*/components_gen.adoc)
			touch "$f"
			;;
		esac
		echo "$f"
		ff=$(realpath "$f")
		if [[ " ${includestack[*]} " =~ [[:space:]]${ff}[[:space:]] ]]; then
			echo "Include file recursion detected on '$f'. Skipped." >&2
			continue
		fi
		includestack+=("$ff")
		includes "$f"
		unset 'includestack[${#includestack[@]}-1]'
	done < <(sed -ne "s|^include::\(.*\)\[\]$|$DIR/\1|p" "$1")
}

images() {
	DIR=$(dirname "$1")
	# Drop line comments first: the macro is matched anywhere on the line to
	# catch inline refs, which otherwise also matches a commented-out one.
	# That is no dependency, and requiring it breaks the build once the file
	# it names is removed.
	grep -v '^[[:space:]]*//' "$1" \
		| sed -ne "s|^.*image:\{1,2\}\([^[]*\)\[.*\].*$|$DIR/\1|p" \
		| tr '\n' ' '
}

INCLUDES=$(includes "$SRC" | tr '\n' ' ')
IMAGES=$(images "$SRC")
for f in $INCLUDES; do
	IMAGES="$IMAGES $(images "$f")"
done

echo "$TARGETS: $INCLUDES $IMAGES"
