#!/bin/bash

# Usage: <path_to_tred>/devel/make_pod_html [target-html-dir [pod-dir]]

dir=`dirname $0`

. "$dir/config.sh"

pod2xhtml="${dir}/pod_to_xhtml"

target_dir="${1:-$PWD}"
[ -d "$target_dir" ] || mkdir $target_dir || exit 1
pushd "$target_dir" > /dev/null
target_dir=`pwd -P` # get absolute path
popd > /dev/null

html_dir="$target_dir"
pod_dir="${2:-$target_dir/pod}"

[ -d "$pod_dir" ] || mkdir $pod_dir || exit 1
#[ -d "$html_dir" ] || mkdir $html_dir || exit 1

for f in $dir/../[bn]tred \
         $dir/../tredlib/tred.def ; do
    n="basename ${f%.pm}"
    n=$(basename "$n")
    pod="$pod_dir/${n//./_}.pod"
    podchecker "$f"
    podselect "$f" > "$pod"
done

for module in "${local_pm[@]}" ; do
    f=`echo $dir/../tredlib/libs/*/${module//://}.pm`
    pod="$pod_dir/${module}.pod"
    podchecker "$f"
    podselect "$f" > "$pod"
done

{
  pushd $dir/../tredlib/contrib > /dev/null
  pod="$pod_dir/contrib.pod"
  rm -f "$pod"
  find -name '*.mak' -or -name '*.inc' -or -name '*.mac' | xargs grep -l '^=' | \
    xargs podselect > "$pod"
  podchecker "$pod"
  popd >/dev/null
}

{
    pushd $dir/../tredlib/contrib/pml >/dev/null
    pod="$pod_dir/PML_mak.pod"
    rm -f "$pod";
    for m in PML; do 
	# PML_T PML_T_Edit PML_T_View PML_A PML_A_Edit PML_A_View; do
	podselect ${m}.mak >> "$pod"
	if [ -f ${m}.inc ]; then
	    podselect ${m}.inc >> "$pod"
	fi
    done
#    cat {PML,PML_T,PML_T_Edit,PML_T_View,PML_A,PML_A_Edit,PML_A_View}.mak |\
#       podselect - > "$pod"
    podchecker "$pod"
    popd >/dev/null
}

for pod in $pod_dir/*.pod ; do
    n=$(basename "${pod%.pod}")
    html="$html_dir/${n//:/_}.html"
    "$pod2xhtml" --css blue.css --infile "$pod" --outfile "$html" --title "$n documentation"
done

