#!/bin/bash
# -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
source "$(dirname "$0")/../lib/ulib/ulib" || exit 1
urequire DEFAULTS

function display_help() {
    uecho "$scriptname: Mettre à jour la documentation pour les outils de nutools

USAGE
    $scriptname [options]

OPTIONS
    -t, --tools
        Ne générer la documentation que pour les outils. S'il ne faut regénérer
        la documentation que pour certains outils, il suffit de les lister en
        arguments.
    -l, --ulib
        Ne générer la documentation que pour ulib
Par défaut, la documentation est (re)générée pour les outils et pour ulib"
}

function dump_content() {
    local mdpage="$1" content="$2"
    if [ -f "$mdpage" ]; then
        awk '
BEGIN { dump = 0 }
!dump && ($0 == "" || $0 ~ /^[^#]/) { dump = 1 }
dump { print }' <"$mdpage" >"$content"
    else
        >"$content"
    fi
}

function write_mdpage() {
    local oldcontent="$1" title="$2" newcontent="$3" mdpage="$4"
    if testdiff "$oldcontent" "$newcontent"; then
        local created="$(date +"%d/%m/%Y %H:%M")"
        echo "# $title" >"$mdpage"
        cat "$newcontent" >>"$mdpage"
        echo -n "
-*- coding: utf-8 mode: markdown -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8:noeol:binary" >>"$mdpage"
    fi
}

auto=1
tools=
ulib=
force=
parse_opts "${PRETTYOPTS[@]}" \
    --help '$exit_with display_help' \
    -t,--tools '$auto=; tools=1' \
    -l,--ulib '$auto=; ulib=1' \
    --force force=1 \
    @ args -- "$@" && set -- "${args[@]}" || die "$args"

[ -n "$auto" ] && {
    tools=1
    ulib=1
}
ac_set_tmpfile oldcontent
ac_set_tmpfile newcontent
cd "$scriptdir"

if [ -n "$tools" ]; then
    if [ -n "$*" ]; then
        cmds=("$@")
    else
        array_lsfiles cmds ..
    fi

    for cmd in "${cmds[@]}"; do
        cmdname="$(basename "$cmd")"
        mdpage="$cmdname.md"
        [ -x "$cmd" ] || continue
        estep "$cmdname"

        dump_content "$mdpage" "$oldcontent"
        echo "
~~~
$("$cmd" --help)
~~~" >"$newcontent"

        write_mdpage "$oldcontent" "$cmdname" "$newcontent" "$mdpage"
    done
fi

if [ -n "$ulib" ]; then
    array_from_lines ulibnames "$(list_files ../lib/ulib)"
    # faire l'entête
    dump_content ulib.md "$oldcontent"
    echo "
## Liste des librairies de ulib" >"$newcontent"
    for ulibname in "${ulibnames[@]}"; do
        echo "* [ulib/$ulibname](ulib_$ulibname)" >>"$newcontent"
    done
    write_mdpage "$oldcontent" ulib "$newcontent" ulib.md
    # faire les pages
    for ulibname in "${ulibnames[@]}"; do
        ulib="../lib/ulib/$ulibname"
        mdpage="ulib_$ulibname.md"
        title="ulib/$ulibname"
        dump_content "$mdpage" "$oldcontent"
        awkrun 'BEGIN {
  in_func = 0
  dump_doc = 0
  dumped_doc = 0
  print
}
!in_func && $0 ~ /^function / {
  if (match($0, /function +([^ ]+)\(\)/, vs)) {
    funcname = vs[1]
    if (funcname !~ /^_/) {
      in_func = 1
      dump_doc = 1
      dumped_doc = 0
      print "## `" funcname "`"
      if ($0 ~ /}$/) {
        in_func = 0
        dump_doc = 0
        dumped_doc = 0
      }
      next
    }
  }
}
in_func && dump_doc && $0 !~ /^ *#/ {
  dump_doc = 0
}
in_func && dump_doc && $0 ~ /^ *#/ {
  if (!dumped_doc) print "~~~"
  gsub(/^ *#+ */, "")
  print
  dumped_doc = 1
}
in_func && $0 ~ /}$/ {
  if (dumped_doc) print "~~~"
  in_func = 0
  dump_doc = 0
  dumped_doc = 0
}
END { if (in_func) print "~~~" }
' <"$ulib" >"$newcontent"
        write_mdpage "$oldcontent" "$title" "$newcontent" "$mdpage"
    done
fi
