#!/bin/sh
: ; exec klone $0 "$@"
; The above line finds the klone executable in the $PATH
(setq max-level *maxint*)

;;verbose error dump
(mail-stack-dump-on-error-to "colas@sa.inria.fr" 0
  '(PF "<HEAD><TITLE>ERROR</TITLE></HEAD>
<BODY>Sorry, there was an error in the script! 
Details are being mailed to maintainer..</BODY>
"))

;;(kdb t)

;(:= args (getopts "USAGE:"
;    ("-v" () verbose "verbose operation")
;))

;; add here the path of the klone library accesible for cgi scripts:
;;(insert *load-pathname* 0 "/users/colas/Klone/src/kl")

;; a sample script to access the klone documentation

(defun main (&aux
  )
  (cgi:init :error "/u/koala/0/koala/colas/t/colas-klone-cgi-errors")

  ;; first, decide what want the user?
  (if
    (= cgi:path-info "")		;no subpart? mainpage
    (show-main-page)
    
    (= cgi:path-info "/manual")		;a query from an URL (from apropos)
    (show-manual cgi:query-string)
    
    (= cgi:path-info "/apropos")	;a query from an URL (from manual)
    (show-apropos cgi:query-string)

    (= cgi:path-info "/form-manual")	; a query from tha form (manual)
    (show-manual #[cgi:arguments "manual"])
    
    (= cgi:path-info "/form-apropos")	;a query from tha form (apropos)
    (show-apropos #[cgi:arguments "apropos"]
      :from #[cgi:arguments "from"]
      :quote (not #[cgi:arguments "regexp"])
      :case #[cgi:arguments "case_sensitive"]
    )

    (= cgi:path-info "/form-script")	; a query from tha form (manual)
    (show-script #[*arguments* 0])

    (PF "Unknown command!\n")		;there is a ball in the soup
  )
  
  (cgi:end :validity 15)
)


(defun show-main-page ()
  (PF "<HEAD><TITLE>Klone on-line documentation</TITLE></HEAD>
<BODY BGCOLOR=\"CCCCFF\"><H1>Klone v%1 on-line documentation</H1><P>

Fill in one of these forms:
<input type=submit>
<hr>
<form method=\"post\" action=\"%0-manual\">
Enter the precise name you are looking for:
<br>
<input type=submit value=\"Manual of\">: <input name=manual size=50 type=text>
<input type=reset value=\"clear\">
</form>
<hr>

<form method=\"post\" action=\"%0-apropos\">
Or look for all entries containing the substring:
<br>
<input type=submit value=Apropos>: <input name=apropos size=50 type=text>
<input type=reset value=\"clear\">
<br>
<input type=checkbox name=case_sensitive> Case Sensitive
<input type=checkbox name=regexp> Regular Expression
<br>
Search scope: 
<input type=radio name=from value=all checked=on> All
<input type=radio name=from value=doc> Only built-ins (C)
<input type=radio name=from value=code> Only autoloadable (Klone)
<hr>
<A href=http://www.inria.fr/koala/colas>Colas Nahaboo</A>
<br>
Everything is done by a Klone script that can <A href=\"%0-script\">display itself!</A>
</form>
</BODY>
"
    (+ cgi:script-name "/form")		; %0
    *version*				; %1
  )
)

(defun show-manual (string &aux
    (result (smartdoc:help string))
  )
  (if result (progn
      (PF "<HEAD><TITLE>Klone on-line doc: %0</TITLE></HEAD>
<BODY BGCOLOR=\"CCCCFF\"><H1>Klone on-line documentation</H1><H2>Documentation of <tt>%0</tt>:</H2><P>\n"
	string
      )
      (if (> (length result) 1)
	(PF "%0 documentations found:<P>\n" (length result))
      )
      (dolist (text result)
	(PF "<hr><P><PRE>%n0</PRE><P>\n" (html:quote-pre text))
      )
      (PF "<hr><P><A HREF=\"%0\">retry another search?</A></BODY>"
	(+ "http://" (getenv "SERVER_NAME") ":" (getenv "SERVER_PORT") 
	  cgi:script-name
	)
      )
    )
    (progn				;not found
      (PF "<HEAD><TITLE>Klone on-line doc: %0</TITLE></HEAD>
<BODY BGCOLOR=\"CCCCFF\"><H1>Documentation of <tt>%0</tt> not found!</H1><P>
No symbol of name <b>%0</b> could be found in the Klone v%1 online documentation,
do you want to 
<UL>
<LI> <A HREF=\"%2\">retry another search?</A>
<LI> <A HREF=\"%3\">do an <b>apropos</b> search on this name?</A>
</UL>
</BODY>\n"
	string *version* 
	(+ "http://" (getenv "SERVER_NAME") ":" (getenv "SERVER_PORT") 
	  cgi:script-name
	)
	(+ "http://" (getenv "SERVER_NAME") ":" (getenv "SERVER_PORT") 
	  cgi:script-name "/apropos?" string
	)
      )
    )
  )
)

(defun show-apropos (string &key from quote case  &aux
    (text (copy ""))
    (fd (open text :type :string :direction :io))
    line
    (re (regcomp "^([^ ]*) *- *(.*)$"))
    (query-url (+ "http://" (getenv "SERVER_NAME") ":" (getenv "SERVER_PORT") 
	cgi:script-name "/manual?"
    ))
  )
  (with (*standard-output* fd)
    (apply apropos (list string :from from :quote quote :case case)) (flush ())
  )
  (if (/= text "") (progn
      (PF "<HEAD><TITLE>Klone on-line apropos: %0</TITLE></HEAD>
<BODY BGCOLOR=\"CCCCFF\"><H1>Klone on-line documentation</H1><H2>Apropos of <tt>%0</tt>:</H2><P>\n"
	string
      )
      (file-position fd 0)
      (while (setq line (read-line fd ()))
	(if (regexec re line)
	  (PF "<br><tt><A href=\"%0\">%1</A>   </tt>%2\n"
	    (+ query-url (regsub re 1)) (regsub re 1) (regsub re 2)
	  )
	  (PF "<br><i>could not parse: </i><tt>%0</tt>\n" line)
    )))
    ;; not found
    (PF "<HEAD><TITLE>Klone on-line apropos: %0</TITLE></HEAD>
<BODY BGCOLOR=\"CCCCFF\"><H1>Klone on-line documentation</H1><H2>Apropos of <tt>%0</tt>:</H2><P>
nothing could be found!\n"
      string
    )
  )
  (PF "<hr><P><A HREF=\"%0\">retry another search?</A></BODY>"
    (+ "http://" (getenv "SERVER_NAME") ":" (getenv "SERVER_PORT") 
      cgi:script-name
    )
  )
)

(defun show-script (path)
  (PF "<HEAD><TITLE>Klone on-line doc script source</TITLE></HEAD>
<BODY BGCOLOR=\"CCCCFF\"><H1>Klone v%1 on-line doc script</H1><P>
This is my body... Click <A HREF=\"%2\">here</a> to see my output.
<hr><pre>%0</pre><hr></body>
" 
    (html:quote-pre (String (open path))) ; %0
    *version*				; %1
    (+ "http://" (getenv "SERVER_NAME") ":" (getenv "SERVER_PORT") 
      cgi:script-name
    )					; %2

))


(main)

;;; EMACS MODES
;;; Local Variables: ***
;;; mode:lisp ***
;;; End: ***

