#!/bin/bash

VERSION=1.0
CMD=`basename $0`
_do_not_edit="### do not edit - automagically created by $CMD"

trap "rm -rf $tmpdocdir ; exit 255" 1 2 3

# aggiunge libdir di __TOOLKIT__ al path di ricerca dei documenti
#
export JT_DOCS_SEARCH="__LIB__"

cleanup()
{
	(cd $tmpdocdir ; rm -f *)
}

# (MAIN)

[ -d __LIB__/modules ] || {
	echo "__LIB__/modules directory not exists, abort"
	exit 1
}
tmpdocdir=`mktemp -d /tmp/$CMD-XXXXXX`
cleanup

# wishlist generale per i moduli
# (ricostruita ad ogni lancio)
#
wishlist=$tmpdocdir/modules-wishlist.txt
echo -e "meta modules wishlist\n$_do_not_edit" >$wishlist

cd __LIB__/modules

# per ogni modulo che abbia un file 'desc.txt' ....
#
for i in `ls */desc.txt`
do
	module=`dirname $i`
	outf=$tmpdocdir/$module.txt

	echo -en "  processing module $module ... "

	# la prima riga e` il titolo (descrizione breve) del modulo
	#
	title=`head -1 $module/desc.txt`

	echo -e "modules $title\n$_do_not_edit" >$outf

	# e` abbinato ad un servizio?
	#
	[ -f $module/svc-name ] && {
		echo "'''servizio: `cat $module/svc-name`'''" >>$outf
	}

	# elenco pkgs da installa e/o rimuovere
	#
	[ -f $module/pkgs-install ] && {
		echo -en "(pkgs) "
		echo -e "\n== packages richiesti ==\n" >>$outf
		for pkg in `cat $module/pkgs-install | sed -e 's/#.*//'`
		do
			echo " * $pkg" >>$outf
		done
	}
	[ -f $module/pkgs-remove ] && {
		echo -e "\n== packages rimossi ==\n" >>$outf
		for pkg in `cat $module/pkgs-remove | sed -e 's/#.*//'`
		do
			echo " * $pkg" >>$outf
		done
	}

	# elenco variabili di configurazione utilizzate
	#
	[ -f $module/vars.txt ] && {
		echo -en "(vars) "
		echo -e "\n== variabili di configurazione ==\n" >>$outf	|| exit $?
		f_freetext=false
		exec 9<&0 <$module/vars.txt
		while read varname type desc
		do
			if $f_freetext
			then
				echo "$varname $type $desc" >>$outf	|| exit $?
			else
			    case "$varname" in
				"") ;;
				---) f_freetext=true ;;
				*) echo " || $varname || $type || $desc ||" >>$outf ;;
			    esac
			fi
		done
		exec 0<&9 9<&-
	}

	# elenco defaults
	#
	[ -f $module/dist-$module ] && {
		echo -en "(defaults) "
		echo -e "\n== defaults ==\n"			>>$outf	|| exit $?
		echo "{{{"					>>$outf || exit $?
		sed -e 's/::/\\::/g' $module/dist-$module	>>$outf	|| exit $?
		echo "}}}"					>>$outf || exit $?
	}

	# al termine, inserisce file desct.txt (tranne la prima riga)
	#
	echo -e "\n== descrizione modulo ==" >>$outf	|| exit $?
	tail --lines=+2 $module/desc.txt >>$outf	|| exit $?

	# esiste un file di documentazione tecnica (internals)?
	#
	[ -f $module/desc-int.txt ] && {
		echo -en "(int) "
		outf=$tmpdocdir/int-$module.txt
		echo -e "99internals:modules $title\n$_do_not_edit\n" >$outf	|| exit $?
		cat $module/desc-int.txt >>$outf	|| exit $?
	}

	# esiste un file di wishlist? nel caso aggiunge alla wishlist
	# generale dei moduli
	#
	[ -f $module/wishlist.txt ] && {
		echo -en "(wish) "
		echo -e "\n== $module ==" >>$wishlist	|| exit $?
		cat $module/wishlist.txt >>$wishlist	|| exit $?
	}

	echo "ok"
done


jtdoc-rebuild || exit $?

cleanup
exit 0
