#!/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] cell-widths
expands a text table generated by StarOffice spreadsheet on stdin to stdout
arguments can be a list of cell widths (default to 8).
Justifies to the left, or to the right if the number is negative
"
    ("-v" () verbose "verbose operation")
    ("-debug" () enter-debugger-on-error "enter klone debugger on error")
    ("-stackdump" () stackdump-on-error "verbose stack dump on error")
))

(if enter-debugger-on-error (kdb t))
(if stackdump-on-error (stack-dump-on-error t))

(setq widths (list))
(setq positions (list 0))
(setq rjusts (list))
(setq curpos 0)
(dolist (arg args) 
  (setq w (Int arg))
  (if (< w 0) (progn
      (setq w (- w))
      (lappend rjusts t)
    )
    (lappend rjusts ())
  )
  (lappend widths w)
  (lappend positions (incf curpos w))
)
(setq offset (- (* (length args) 8) curpos))

(defun main (&aux
  )
  (catch 'EOF (while t
      (setq line (read-line))
      (with (line-fd (open line :type :string)) 
	(setq i 0)
	(setq out (copy ""))
	(catch 'EOF (while t
	    (setq cell (read line-fd))
	    (nconc out (make-string (- (get positions i '(- (* i 8) offset))
		  (length out)
	    )))
	    (if (getn rjusts i) (progn
		(nconc out (make-string 
		    (- (- (get widths i 8) 1) (length (String cell)))
		  ) (String cell) " "
		)
	      )
	      (nconc out (String cell) " ")
	    )
	    (incf i)
	))
	(write-line out)
))))

(main)

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

