#!/bin/sh
: ; exec klone $0 "$@"
; The above line finds the klone executable in the $PATH
;;Skeleton of a typical klone script
;;(stack-dump-on-error t)
;;(kdb t)

(setq args (getopts "USAGE: make-file
creates as many files as possible, to fill space, with random contents
(but same contents for each file)"
    ("-r" radix radix "the basename of created files, defaults to xxx")
    ("-1" () onlyone "generates only one file, otherwise iterate")
    ("-s" filesize filesize "file size in bytes (default 8k = 8192)")
    ("-v" () verbose "verbose operation")
))

(if (not radix) (setq radix "xxx"))
(if (not filesize) (setq filesize 8192))
(setq filesize (Int filesize))

(defun main (&aux
  )
  (setq contents (copy ""))
  (dotimes (i filesize) (put contents -1 (random 256)))
  (if onlyone
    (create-file radix contents)
    (catch 'create-file 
      (setq num 0)
      (while t
	(create-file (+ radix (expand-num num 6)) contents)
	(incf num)
  )))
)

(defun create-file (name contents &aux fd err s)
  (if verbose (? "."))
  (setq fd (open name :if-exists :supersede :direction :output :error ()))
  (if (not fd) (progn (verbose? "\n")
      (PF *stderr* "error opening %0, exiting\n" name)
      (throw 'create-file))
  )
  (catch 'ALL (setq err t)(write-string contents fd)(setq err ()))
  (if err (progn (verbose? "\n")
      (PF *stderr* "error writing on %0\n" name)
      (throw 'create-file)
  ))
  (close fd)
  (catch 'ALL (setq err t)
    (setq s (String (open name)))
    (setq err ())
  )
  (if err (progn (verbose? "\n")
      (PF *stderr* "error reading %0\n" name)
  ))
  (if (/= s contents) (progn (verbose? "\n")
      (PF *stderr* "contents of %0 garbled\n" name)
  ))
)
  

(main)

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

