#!/bin/sh
# do not run compile time !
# generates simple canvasdraw usage html-page and a javascript live example

# See the generated canvasdraw help at http://localhost/wims/?module=adm/createxo&cmd=help&special_parm=canvasdraw

##echo "install into wims distribution(D)
##or use for local testing (L) ?
##D,L ?"
##read ans
ans="D"
helpdir="../../../public_html/scripts/help"
if [ $ans = "D" ]; then
  # path used for href/http link !
  jsdir="scripts/help/canvas_examples"
  mkdir -p "$helpdir/canvas_examples" 2>/dev/null
else
  jsdir="./examples"
fi
in="canvasdraw.c"
phtml="canvasdraw.phtml"
# html="canvasdraw.html"
tmp="canvas_index"
keys="keywords"
datum=`date +%d-%m-%Y`
# htmlhead="<!DOCTYPE html><html><body>"
# htmltail="</body></html>"
# interaction="<script>\"use strict\";function read_canvas(){if( typeof popup !== 'undefined' ){var fun = popup['read_canvas'+canvas_scripts[0]];return fun();}else{var script_len = canvas_scripts.length;var draw_reply = \"\";var found_result = false;for(var p = 0 ; p < script_len ; p++){var fun = eval(\"read_canvas\"+canvas_scripts[p]);if( typeof fun === 'function'){var result = fun();if( result && result.length != 0){if(script_len == 1 ){ return result;};found_result = true;draw_reply = draw_reply + \"[\"+p+\"]=\"+ result + \"\n\";};};};if( found_result ){return draw_reply;}else{return null;}};};function read_dragdrop(){if( typeof popup !== 'undefined' ){var fun = popup['read_dragdrop'+canvas_scripts[0]];return fun();}else{var script_len = canvas_scripts.length;var dragdrop_reply = \"\";var found_result = false; for(var p = 0 ; p < script_len ; p++){var fun = eval(\"read_dragdrop\"+canvas_scripts[p]); if(typeof fun === 'function'){var result = fun();if( result && result.length != 0 ){if(script_len == 1 ){ return result;};found_result = true;dragdrop_reply = dragdrop_reply + \"[\"+p+\"]=\" + result +\"\n\";};};};if( found_result ){return dragdrop_reply;}else{return null;}};};</script><input type=\"button\" onclick=\"alert(read_canvas());\" value=\"read_canvas()\"><input type=\"button\" onclick=\"alert(read_dragdrop());\" value=\"read_dragdrop()\">"

# these are only used to simulate a wims session
export "w_wims_session"="example"
export "w_session"="123"
export "w_wims_home"="/var/wims/src/Misc"
#wims_tmp="../sessions/example/getfile/"
#mkdir -p $wims_tmp
# the popup html pages and corresponding javascript includes are locate here
tmpdir="canvas_examples"
rm -rf $tmpdir
mkdir $tmpdir 2>/dev/null

##echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN\" \"http://www.w3.org/Math/DTD/mathml2/xhtml-math11-f.dtd\">
##<html>
##<head><meta charset=\"UTF-8\"></head>
##<body xmlns=\"http://www.w3.org/1999/xhtml\" class=\"main_body\" dir=\"ltr\">
##<style type='text/css'>
## a:link {color:#004077;text-decoration:none;cursor:pointer;}
## a:hover {color:#dd6300;}
## a:visited {color:#004077;}
##</style>"> $html

echo "!!generated by Misc/canvasdraw/canvasinfo.sh | Do NOT Modify here.
<div class='wims_search_engine'><div class='wimscenter'>
  <label for='search'>enter a command name</label>
  <input type='search' id='search'
         onkeydown='javascript:if(event.keyCode == 13){look();}'><button class='wims_button icon_button' type='submit' onclick='javascript:look();'>
  <span class='Searching'>\$wims_name_search</span></button>
</div></div>
<h2 class='wims_title'>Implemented canvasdraw commands ($datum)</h2>
<ul class='flex_box grid-x small-up-3 medium-up-4 large-up-8 small'>
" > $phtml

p=0
echo "" > $tmp
cntex=0
while read line ; do
  chk=`echo "$line" | grep "@"`
  # Lines starting with "@" are docstrings (z test if string empty)
  if [ ! -z "$chk" ] ; then
    chk=`echo "$chk" | grep @%`
    # if it's not a docstring samples
    if [ -z "$chk" ] ; then
      # if it's first line
      if [ $p -eq 0 ]; then
        string=`echo "$line" | tr '@' ' '`
        idx=`echo "$string" | awk '{ print $1 }'`
        echo ",'$idx'" >> $keys
        echo "<li class='card cell'><div class='card-section'>" >> $phtml
        echo "<a id='${idx}_top' href='#$idx'>$idx</a>" >> $phtml
        echo "<h3 id='$idx'>$idx <a href='#${idx}_top' title='return at the command list'>&#8593;</a></h3>
        <code>$string</code>
        <ul>" >> $tmp
        p=1
      # it's not the first line
      else
        p=2
        alt=`echo "$line" | grep "@ alternative:" | awk -F'alternative:' '{ print $2 }' | tr -d '[:blank:]'`
        # it's a command alias
        if [ ! -z $alt ] ; then
          echo ",'$alt'" >> $keys
          echo "<a href='#$idx'>$alt</a>" >> $phtml
          echo "<li><span>alternative command: <a id='${alt}' href='#${alt}_top'><code>$alt</code></a></span></li>" >> $tmp
        else
          echo $line | sed 's/@/<li>/g' | sed "s/''/<span class=\"wims_emph\">/g" | sed "s!\`\`!</span>!g" >> $tmp
          echo "</li>" >> $tmp
        fi
      fi
    else
      # begin generating live examples signaled by @%
      name=`echo "$chk" | awk -F'%' '{ print $2 }' `
      echo "command $name"
      cont=`echo "$chk" | tr '%' '\n'`
      #echo "!!generated by canvasinfo.sh" > $tmpdir/$name
      echo "$cont" | sed '1,2d'> $tmpdir/$name
      ##echo "$htmlhead" > $tmpdir/$name.html
      ##cat $tmpdir/$name | ~/other/bin/canvasdraw | sed 's/src=\"wims.cgi?session=123*.*.js\"/src=\"'$name'.js\"/g' >> $tmpdir/$name.html
      if [ $cntex -eq 0 ]; then echo "<li>" >> $tmp ; fi
      cntex=$(($cntex+1))
      echo "
      !set wims_ref_class=wims_button
      !href cmd=help&special_parm=canvas_examples,$name \$wims_name_Example: $name
      " >> $tmp
      ##echo "<textarea cols='60' rows='10'>" >> $tmpdir/$name.html
      ##echo "$cont" | sed '1,2d' >> $tmpdir/$name.html
      ##echo "</textarea>" >> $tmpdir/$name.html
      ##echo "$interaction" >> $tmpdir/$name.html
      ##echo "$htmltail" >> $tmpdir/$name.html
      ##mv -v $wims_tmp/*.js $tmpdir/$name.js
    fi
  else
    if [ $p -eq 2 ]; then
      if [ $cntex -gt 0 ]; then
        echo "</li>" >> $tmp
      fi
      cntex=0
      echo "</ul>" >> $tmp
      echo "</div></li>" >> $phtml
    fi
    p=0
  fi
done < $in

echo "</ul>" >> $phtml

cat $tmp >> $phtml
keywords=`cat $keys | tr -d '\n'`
echo "
<script>
 var keys = ['canvasdraw' $keywords];
 var keys_len = keys.length;
 function match(s1,s2){
    var n1 = s1.length;
    if(n1 < 3){return 0;}
    var n2 = s2.length;
    var c1,c2,found;
    var count = n1 - Math.abs(n1 - n2);
    for(var p = 0;p < n1;p++){
      c1=s1.charAt(p);
      found = false;
      for(var i = 0;i < n2;i++){
        c2 = s2.charAt(i);
        if(c1 == c2){found = true;count = count + n1 - Math.abs(p - i);}
      };
      if(! found ){count = count - n2;}
    };
    return count;
 };
 function look(){
  var s = ((document.getElementById('search').value).replace(/\s/g, '')).toLowerCase();
  var typo;var next_best = -1;var next_idx = s.length;var tmp;var ss;
  for(var p = 0; p < keys_len ; p++){
    ss = keys[p];
    if( s == ss ){
      document.getElementById(s).scrollIntoView({behavior: \"smooth\", block: \"center\"});
      return;
    };
    /* not ok? ... try to find a match for a reasonable typo... */
    tmp = match(s,ss);
    if(tmp > next_idx){
     next_idx = tmp;
     next_best = p;
    };
  };
  if(next_best != -1 ){
    typo = keys[next_best];
    if(confirm('\"'+s+'\" is not a valid canvasdraw command. Did you mean \"'+typo+'\" ?')){
      document.getElementById(typo).scrollIntoView({behavior: \"smooth\", block: \"center\"});
      return;
    };
  }
  else{
    alert(s+' is not a valid canvasdraw command');
  };
  return;
 };
</script>" >> $phtml

##cat $phtml >> $html
##echo "
##</body>
##</html>" >> $html

# install in wims distribution scripts/help/en
if [ $ans = "D" ] ; then
  mv $phtml "$helpdir/en"
  cp -rv $tmpdir $helpdir
fi

rm $tmp
rm $keys
