#!/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  Ne générer la documentation que pour les outils
    -l  Ne générer la documentation que pour ulib
    -s  Synchroniser avec nutools.html après la génération de la documentation
Par défaut, la documentation est (re)générée pour les outils et pour ulib"
}

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

function write_twpage() {
    local oldcontent="$1" title="$2" newcontent="$3" twpage="$4"
    if testdiff "$oldcontent" "$newcontent"; then
        local created="$(date +"%d/%m/%Y %H:%M")"
        echo "# -*- coding: utf-8 mode: markdown -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
##@creator: $USER
##@created: $created
##@modifier: $USER
##@changecount: 1
##@tags: 
##@title: $title" >"$twpage"
        cat "$newcontent" >>"$twpage"
    fi
}

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

[ -n "$auto" ] && {
    tools=1
    ulib=1
    sync=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")"
        twpage="$cmdname.twp"
        [ -x "$cmd" ] || continue
        estep "$cmdname"

        dump_content "$twpage" "$oldcontent"
        echo "
{{{
$("$cmd" --help)
}}}" >"$newcontent"
        
        write_twpage "$oldcontent" "$cmdname" "$newcontent" "$twpage"
    done
fi

if [ -n "$ulib" ]; then
    array_from_lines ulibnames "$(list_files ../lib/ulib)"
    # faire l'entête
    dump_content ulib.twp "$oldcontent"
    echo "
!Liste des librairies de ulib" >"$newcontent"
    for ulibname in "${ulibnames[@]}"; do
        echo "* [[ulib/$ulibname]]" >>"$newcontent"
    done
    write_twpage "$oldcontent" ulib "$newcontent" ulib.twp
    # faire les pages
    for ulibname in "${ulibnames[@]}"; do
        ulib="../lib/ulib/$ulibname"
        twpage="ulib_$ulibname.twp"
        title="ulib/$ulibname"
        dump_content "$twpage" "$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_twpage "$oldcontent" "$title" "$newcontent" "$twpage"
    done
fi

if [ -n "$sync" ]; then
    ../twsync ${force:+--force}
fi
