#!/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] oldpath newpath files...
Examines files and fixes first line to point to the path of the executable
Paths can be = to mean /usr/local/bin/klone
e.g.:
    %0 = /new/temp/test/bin/klone *
"
    ("-v" () verbose "verbose operation")
))

(defun main (&aux
  )
  (setq oldpath (get args 0))
  (if (= "=" oldpath) (setq oldpath "/usr/local/bin/klone"))
  (if (not (match "^#!" oldpath)) (setq oldpath (+ "#!" oldpath)))
  (setq newpath (get args 1))
  (if (= "=" newpath) (setq newpath "/usr/local/bin/klone"))
  (if (not (match "^#!" newpath)) (setq newpath (+ "#!" newpath)))
  (dolist (file (subseq args 2))
    (do-file file)
))

(defun do-file (f &aux
    (fd (open f))
    (line (read-line fd))
    buffer
  )
  (if (= oldpath line) (progn
      (setq buffer (String fd))
      (verbose? "patching file %0" f)
      (close fd)
      (setq fd (open f :direction :output :if-exists :supersede))
      (write-line newpath fd)
      (write-string buffer fd)
  ))
)

(main)

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

