;;; $Header: /home/gene/library/website/docsrc/sca/RCS/go.lisp,v 395.1 2008/04/20 17:25:48 gene Exp $

;;;
;;; Use this file to create the TeX versions of the source files.
;;; It also creates the tmp/directory.tex file.
;;; Just load it into Lisp.  It does its work as it's loaded.
;;;

;;;
;;; Create *.tex versions of all the *.c & *.h files.
;;; They can be \included into sca.tex.
;;;
(dolist (src-pn (apply #'append
		       (mapcar
			#'directory
			(mapcar #'(lambda (type)
				    (make-pathname
				     :directory '(:relative "src")
				     :name :wild :type type))
				'("c" "h")))))
  (let ((dst-pn (make-pathname :directory '(:relative "tmp")
			       :type "tex"
			       :defaults src-pn)))
    (print (list src-pn dst-pn))
    (with-open-file (dst dst-pn :direction :output
			 :if-exists :rename-and-delete)
      (format dst "{\\small \\tt~&")
      (format dst "\\begin{verbatim}~&")
      (with-open-file (src src-pn :direction :input)
	(do ((c (read-char src nil src) (read-char src nil src))
	     (column 0 (if (eql c #\Newline) 0 (1+ column))))
	    ((eq c src))
	    (case c
	      (#\Tab
	       (format dst " ")
	       (incf column)
	       (do ()
		   ((zerop (mod column 8)))
		   (format dst " ")
		   (incf column)))
	      (otherwise
	       (write-char c dst))))
	(format dst "\\end{verbatim}~&")
	(format dst "}~&")))))

;;;
;;; Create a directory.tex file which shows the directories,
;;; their contents, \& the sizes of their contents.
;;; We don't really walk the directory tree because many files
;;; are not available for download.  We just show some of
;;; the files.
;;;
(defun dirent (pn dst)
  (with-open-file (src pn :element-type '(unsigned-byte 8)
		       :direction :input)
    (format dst "\\item \\htmladdnormallink{~A" (pathname-name src))
    (when (pathname-type src)
      (format dst ".~A" (pathname-type src)))
    (format dst "}{~A}" pn)
    (format dst ", ~D octets" (file-length src))
    (format dst "~&")))

(with-open-file (dst "tmp/directory.tex" :direction :output
		     :if-exists :rename-and-delete)
  (format dst "\\begin{itemize}~&")
  (dirent "Makefile" dst)
  (dirent "Makefile.msdos" dst)
  (format dst "\\item bin/~&")
  (format dst "  \\begin{itemize}~&")
  (dirent "bin/counter" dst)
  (dirent "bin/counter.exe" dst)
  (dirent "bin/gen" dst)
  (dirent "bin/gen.exe" dst)
  (dirent "bin/msg-portmapper" dst)
  (dirent "bin/scan" dst)
  (dirent "bin/scan.exe" dst)
  (format dst "  \\end{itemize}~&")
  (format dst "\\item src/~&")
  (format dst "  \\begin{itemize}~&")
  (dirent "src/counter.c" dst)
  (dirent "src/daytime.sca" dst)
  (dirent "src/debug.c" dst)
  (dirent "src/debug.h" dst)
  (dirent "src/echo.sca" dst)
  (dirent "src/gen.c" dst)
  (dirent "src/msg-portmapper.c" dst)
  (dirent "src/portmapper.sca" dst)
  (dirent "src/scan.c" dst)
  (dirent "src/snmp.sca" dst)
  (dirent "src/this.h" dst)
  (format dst "  \\end{itemize}~&")
  (format dst "\\end{itemize}~&"))

;;;
;;; This big expression converted the MS-DOS text files into unix
;;; files.  Not needed now, but might be useful in the future.
;;;

;; (dolist (src-pn (apply #'append
;; 		       (mapcar #'directory
;; 			       (mapcar #'(lambda (type)
;; 					   (make-pathname
;; 					    :directory '(:absolute "home"
;; 								   "gene"
;; 								   "public_html"
;; 								   "briefcase"
;; 								   "sca"
;; 								   "src")
;; 					    :name :wild
;; 					    :type type))
;; 				       '("c" "h")))))
;;   (let ((dst-pn (make-pathname
;; 		 :directory '(:absolute "home" "gene" "library"
;; 					"website" "docsrc" "sca"
;; 					"src")
;; 		 :defaults src-pn)))
;;     (print (list src-pn dst-pn))
;;     (with-open-file (dst dst-pn :element-type '(unsigned-byte 8)
;; 			 :direction :output
;; 			 :if-exists :rename-and-delete
;; 			 :if-does-not-exist :error)
;;       (with-open-file (src src-pn :element-type '(unsigned-byte 8)
;; 			   :direction :input)
;; 	(do ((b (read-byte src nil src) (read-byte src nil src)))
;; 	    ((eq b src))
;; 	    (unless (eql b #x0d)
;; 	      (write-byte b dst)))))))

;;; --- end of file ---
