(defun fix-m2makefile ()
  "Reformat the output of the m2make program for KSOS-32 use."
  (interactive "*")
  ; generate a unique string
  (setq unique "Z")
  (while (search-forward unique nil t)
    (setq unique (concat unique "Z"))
    (goto-char (point-min))
    )
  ; eliminate unwanted tabs
  (goto-char (point-min))
  (replace-regexp "^\t\t" unique)
  (goto-char (point-min))
  (replace-string "\t" " ")
  (goto-char (point-min))
  (replace-string unique "\t")
  (goto-char (point-min))
  ; fix "echo" commands and clean up the front of the file
  (replace-regexp "^\techo .* up to date$" "\ttouch $@")
  (goto-char (point-min))
  (search-forward "symbols:")
  (beginning-of-line 1)
  (kill-region (point-min) (point))
  ; expunge unused makefile symbols
  (replace-string " $(link)" "")
  (goto-char (point-min))
  (replace-string " $(compile)" "")
  (goto-char (point-min))
  (replace-string "$(system)" "")
  ; reorder symbols, objects, and main program targets
  (goto-char (point-min))
  (search-forward "\n\n")
  (kill-region (point-min) (point))
  (search-forward "objects:")
  (search-forward "\n\n")
  (yank)
  (goto-char (point-min))
  (if (search-forward "m2l" nil t)
      (progn (insert " -E")
	     (search-forward "\n\n")
	     (set-mark (point))
	     (search-backward ":")
	     (beginning-of-line 1)
	     (kill-region (point) (mark))
	     (goto-char (point-min))
	     (yank)
	     (goto-char (point-min)))
    )
  (if (search-forward "/u/ksos/ksos32/lib/trusted" nil t)
      (progn (goto-char (point-min))
	     (replace-string "/u/ksos/ksos32/lib/trusted" "$(K32TLIB)")
	     (goto-char (point-min))
	     (insert "K32TLIB = /u/ksos/ksos32/lib/trusted\n\n"))
    )
  (if (search-forward "/usr/local/lib/m2/NEW" nil t)
      (progn (goto-char (point-min))
	     (replace-string "/usr/local/lib/m2/NEW" "$(M2LIB)")
	     (goto-char (point-min))
	     (insert "M2LIB = /usr/local/lib/m2/NEW\n\n"))
    )
  (while (re-search-forward "^\tm2c" nil t)
    (replace-match "\trun.m2c -T -E")
    (end-of-line 1)
    (insert unique)
    )
  (goto-char (point-min))
  (while (search-forward "m2c -T -E -d" nil t)
    (delete-char -3)
    (search-forward unique)
    (replace-match ".def" t)
    )
  (goto-char (point-min))
  (replace-string unique ".mod")
  (goto-char (point-min))
  (replace-string " \n" "\n")
  (goto-char (point-max))
  (re-search-backward ".")
  (beginning-of-line 2)
  (kill-region (point) (point-max))
  (goto-char (point-min))
  )
