#!/bin/bash
#
# haddock-i: list haddock's -i parameters.
#
# Copyright (c) 2015-2025 Rudy Matela.
# Distributed under the 3-Clause BSD licence.
#
# $ haddock-i
#
# will print -i parameters necessary for haddock to link to Haddock
# documentation installed on your system, so you can:
#
# $ haddock-i | xargs haddock <files>

# This is a grep/sed/bash hack.
# It works for all my packages,
# but it isn't foolproof,
# use with care.
list-deps() {
	cat *.cabal |
	sed -e 's/  */\n/g' |
	grep -A 999 build-depends: |
	tail -n +2 |
	grep -v '^$' |
	while read tok
	do
		echo $tok | grep -q :$ && break
		# TODO: make this slightly more efficient
		echo $tok | grep -q '^[a-zA-Z-]*$' || continue
		[ "$tok" == "if" ] && continue
		echo "$tok"
	done
}

find-find-args() {
	echo '-name this-should-not-match-anything.haddock'
	list-deps |
	sed -e 's/^/-o -name /;s/$/.haddock/'
}

realghc=$(basename $(readlink -f /usr/bin/ghc))

find \
	/usr/share/doc/ghc/html/libraries \
	~/.cabal/store/$realghc \
	$(find-find-args) |
sed -e 's/^/-i /'
