(defun undebug-kernel ()
  "Undo the commenting out of some debug code in KSOS-11."
  (interactive "*")
  (make-local-variable 'old-fold)
  (setq old-fold case-fold-search)
  (setq case-fold-search nil)
  (message "Pass 1...")
  (goto-char (point-min))
  (while (search-forward "(*DBbegin-" nil t)
    (delete-horizontal-space)
    (if (equal (following-char) 10)
	(progn (beginning-of-line)
	       (kill-line 1))
      (progn (teco-fs "(*DBbegin-" "" -1)
	     (beginning-of-line))
      )
    (insert "#ifdef DEBUG1\n")
    (teco-fs "-DBend*)" "")
    (delete-horizontal-space)
    (if (not (equal (preceding-char) 10))
	(insert "\n"))
    (insert "#endif DEBUG1")
    )
  (message "Pass 2...")
  (goto-char (point-min))
  (while (search-forward "(*DBB" nil t)
    (delete-horizontal-space)
    (if (equal (following-char) 10)
	(progn (beginning-of-line)
	       (kill-line 1))
      (progn (teco-fs "(*DBB" "" -1)
	     (beginning-of-line))
      )
    (insert "#ifdef DEBUG2\n")
    (teco-fs "DBE*)" "")
    (delete-horizontal-space)
    (if (not (equal (preceding-char) 10))
	(insert "\n"))
    (insert "#endif DEBUG2")
    )
  (message "Pass 3...")
  (goto-char (point-min))
  (while (search-forward "ASSERT" nil t)
    (search-forward ",")
    (setq x (1- (point)))
    (search-forward ")")
    (delete-region x (1- (point))))
  (message "Pass 4a...")
  (goto-char (point-min))
  (replace-string "(* Assert (" "(* Assert(")
  (message "Pass 4b...")
  (goto-char (point-min))
  (while (search-forward "(* Assert(" nil t)
    (replace-match "ASSERT(")
    (teco-fs " *)" ";"))
  (message "Pass 4c...")
  (goto-char (point-min))
  (replace-string ";;" ";")
  (progn (message "Pass 5...")
	 (goto-char (point-min))
	 (comment-out "ASSERT((")
	 (comment-out "ASSERT(")
	 (comment-out "ASSERT(")
	 (comment-out "already performed")
	 (comment-out "already performed")
	 (comment-out "already performed")
	 (comment-out "swapped in")
	 (comment-out "pseudo")
	 (teco-fs "    (*  ASSERT" "	ASSERT")
	 (teco-fs " *)" "")
	 (teco-fs "(*  ASSERT" "ASSERT")
	 (teco-fs " *)" "")
	 (teco-fs "(* ASSERT" "ASSERT")
	 (teco-fs " *)" "")
	 (search-forward " defined )")
	 (end-of-line)
	 (insert " *)")
	 (search-backward "A")
	 (insert "(* ")
	 )
  (setq case-fold-search old-fold)
  )

(defun comment-out (str)
  (interactive "*sSearch for: ")
  (search-forward str)
  (beginning-of-line)
  (skip-chars-forward "	 " nil)
  (insert "(* ")
  (end-of-line)
  (insert " *)")
  )

(defun teco-fs (from-string to-string &optional arg)
  "Perform the TECO \"fs\" (find and substitute) command.
Replaces one occurrence of FROM-STRING with TO-STRING -- the ARGth
occurrence, to be precise.  Negative ARG means search backward.

When calling from a program, nil means \"no arg\",
a number counts as a prefix arg."
  (interactive "*sFind: \nsFind \"%s\" and substitute with: \nP")
  (if (or (equal arg nil) (equal arg 0))
      (setq arg 1))
  (if (< arg 0)
      (search-backward from-string nil nil (- arg))
    (search-forward from-string nil nil arg)
    )
  (replace-match to-string t t)
  )
