#!/bin/sh
: ; exec klone $0 "$@"
; The above line finds the klone executable in the $PATH
;;Skeleton of a typical klone script

(setq args (getopts "USAGE: %0 [options] files...
Examines files and add the line in the header if not already present:
    <META NAME=\"ROBOTS\" CONTENT=\"NOINDEX, NOFOLLOW\">
To prevent WWW search engines to index these pages.
If file is a directory, recurse into it.
Does not follow symbolic links
"
    ("-v" () verbose 
      "verbose operation: list modified files, otherwise silent")
    ("-nr" () norecurse "do not recurse into subdirs")
))

(setq refile (re-nocase "[.]html?$"))
(setq re-robots (re-nocase 
    "<meta[ \t\n]+name[ \t\n]*=[ \t\n]*\"robots\"[ \t\n]+content[ \t\n]*=[ \t\n]*\"noindex,[ \t\n]*nofollow\">"
))
(setq re-header (re-nocase "<[/]head>"))

(defun main ()
  (dofile (file stats dir args :no-recurse norecurse 
      :do-not-follow-symbolic-links t)
    (if (regexec refile file) (progn
	(setq s (String (open file)))
	(if (regexec re-robots s)
	  (verbose? "file %0 already marqued" (concat-paths dir file))
	  (progn
	    (verbose? "adding marker to %0" (concat-paths dir file))
	    (if (regexec re-header s) 
	      (print-format
		(open file :direction :output :if-exists :supersede)
		"%0%3%1\n%2"
		(substring-ptr s 0 (0 (get re-header 0)))
		"<META NAME=\"ROBOTS\" CONTENT=\"NOINDEX, NOFOLLOW\">"
		(substring-ptr s (0 (get re-header 0)))
		(if (/= #\newline ((- (0 (get re-header 0)) 1) s)) "\n" "")
	      )
	      (print-format *standard-error*
		"ERROR: no header detected in file %0!!!\n"
		(concat-paths dir file)
	      )
	)))
))))

(main)

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

