#!/bin/sh
: ; exec klone $0 "$@"
; The above line allows not to embed the exact path of the klone executable

;;Skeleton of a typical klone script

(setq args (getopts "USAGE: %0 [options] number
Outputs numbers one per line up to number: 0, 1, 2...
number is excluded (i.e. not printed: num 2 prints 0 and 1)
Use --- to specify negative numbers: num -- -5"
    ("-v" () verbose "verbose operation")
    ("-b" begin-num start "beginning number (default: 0)")
    ("-1" () start-at-1 "same as -b 1")
    ("-s" step-num step "by how much to step each time (default: 1)
0 means output only number")
    ("-d" digits digits "number of digits (default: digits of number)
With -d 2, will print 00, 01, 02...")
    ("-p" pattern pattern "replaces %0 in pattern string by nums, 
e.g: foo%0.zip for foo01.zip, foo02.zip...")
    ("-l" () space "prints all numbers in same line, separated by a space
default is to print numbers one per line")
))

(setq number (Int (0 args)))
(setqn
  digits (if (not digits) (length (String number)) (Int digits))
  start (if (not start) (if start-at-1 1 0) (Int start))
  step (if (not step) (if (>= number 0) 1 -1) (Int step))
  print-pat (if pattern (+ pattern "%1") "%0%1")
  print-sep (if space " " "\n")
)

(defun main (&aux (n start)
  )
  (if (= 0 step) (print-num number digits)
    (< step 0)
    (while (> n number) 
      (print-num n digits)
      (incf n step)
    )
    (while (< n number) 
      (print-num n digits)
      (incf n step)
    )
  )
  (if (/= "\n" print-sep) (PF "\n"))
)

(defun print-num (n digits)
  (if (< n 0)
    (PF print-pat (+ "-" (expand-num (- n) digits :overflow 'number))
      print-sep)
    (PF print-pat (expand-num n digits :overflow 'number) print-sep)
  )
)

(main)

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

