#!/bin/bash
# WF 2019-10-20
# document the python software with plant uml
# used e.g. for  https://github.com/WolfgangFahl/play-chess-with-a-webcam
scriptdir=$(dirname $0)
# source the color message handling
. $scriptdir/colormsg

# create plantuml documentation for the given
# directory title and github url
umldoc() {
  local l_src="$1"
  local l_title="$2"
  local l_githuburl="$3"
  cat << EOF
     click classes to see source code at [$l_githuburl $l_title]
  <uml>
     hide circle
     left to right direction
EOF
for src in $(find "$l_src" -name "*.py")
do
  srcfile=$(basename $src)
  module=$(basename $srcfile .py)
  if [ $module != "__init__" ]
  then
    echo "package $module {"
    cat $src | gawk -v srcfile="$srcfile" -v githuburl="$l_githuburl" '
/#<uml>/ { inuml=1;next}
/#<\/uml>/ { inuml=0;next }
inuml {
  gsub ("#","",$0)
  print
}
/class .*:/ {
  finishClass()
  classLine=$0
  match($0,/(Interface)/)
  if (RLENGTH>=0) {
    classType="Interface"
  } else {
    classType="Class"
  }
  gsub(/:/,"",$0)
  gsub(/class /,"",$0)
  gsub(/\(.*\)/,"",$0)
  className=$0
  inClass=1
}
# Attributes
/.*=Attribute/ {
  match($0,/[[:space:]]*(.*)=Attribute/,groups)
  attributes[$0]=groups[1]
}
# Operations
/def / {
  gsub(/def /,"",$0)
  gsub(/:/,"",$0)
  gsub(/self,/,"",$0)
  gsub(/self/,"",$0)
  gsub(/ /,"",$0)
  methods[$0]=$0
  inClass=0
}
/"""/ {
  gsub(/"""/,"",$0)
  if (inClass==1)
    doc=$0
  inClass=0
}
function ltrim(s) { sub(/^[ \t\r\n]+/, "", s); return s }
function rtrim(s) { sub(/[ \t\r\n]+$/, "", s); return s }
function trim(s) { return rtrim(ltrim(s)); }
function finishClass() {
   if (className=="")
     return
   if (trim(doc)!="") {
      dot="."
      if (match(doc,"\\."))
         dot=""
      printf("\n  note top of %s: %s%s\n",className,trim(doc),dot)
  }
   printf("  %s %s [[%s]] {\n",classType,className,githuburl"/"srcfile)
   for (attribute in attributes) {
     printf("    %s\n",attributes[attribute])
   }
   for (method in methods) {
     printf("    %s\n",methods[method])
   }
   printf("  }\n\n")
   if (match(classLine,"Exception"))
     printf("  %s --|> Exception\n",className)
  delete attributes
  delete methods
  className=""
  doc=""
}
END {
  finishClass()
}
'
    echo "}"
  fi
done
cat << EOF
  skinparam titleFontSize 18
  skinparam titleFontStyle bold
  skinparam titleFontName Arial

  skinparam classBackgroundColor white
  skinparam classBorderColor #FF8000
  skinparam classFontColor black
  skinparam classFontSize 14
  skinparam classFontStyle bold
  skinparam classFontName Arial

  skinparam classAttributeFontName Arial
  skinparam classAttributeFontSize 14

  skinparam classArrowFontName Arial
  skinparam classArrowFontSize 14

  skinparam noteBackgroundColor #FFFFFF
  skinparam noteBorderColor #000080
  skinparam noteFontName Arial
  skinparam noteFontSize 14
</uml>
EOF
}

umldoc $scriptdir/../pcwawc "Play Chess With A WebCam github repository" https://github.com/WolfgangFahl/play-chess-with-a-webcam/tree/master/pcwawc
