#!/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] oldfile newfile recname fieldnames...
filter, converts a filedatabase from old format to new one
reads it from file argument, and creates  new file"
    ("-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))

(if (> 2 (length args)) (getopts :usage))

(require 'defstruct)
(require 'filedatabase)

(defun main (&aux base
  )
  (load (get args 0 "*FIRST ARGUMENT*"))
  (if (/= 'Structure #[fdb::base 0])
    (exit (PF "%0 is not in old format, aborted!\n%r1\n" (get args 0) fdb::base) 1)
  )
  (setq oldbase fdb::base)
  (setq rectype (apply fdb:defstruct (map List intern (subseq args 2))))
  (setq base (fdb:create (getn args 1) rectype :plist (4 oldbase)))
  (dolist (oldrec (6 oldbase))
    (setq rec (fdb:add-record base))
    (setq of (7 oldrec))
    (dotimes (i (- (length args) 3))
      (apply (eval (intern (+ (getn args 2) "-" (getn args (+ i 3)))))
	(list rec (getn of (+ i 2)))
    ))
    (fdb:save-record base rec)
  )
)

(main)

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

