;; -*- mode:scheme -*-
;;
;; A script to dump dbm content in S-expr
;;
;; Usage: gosh dbm/dump [-o <outfile>][-t <type>] <dbmname>
;;

(use gauche.parseopt)
(use dbm)
(use util.match)
(use file.filter)

(define (main args)
  (let-args (cdr args) ([ofile "o=s" #f]
                        [type  "t=y" 'gdbm]
                        [else _ (usage)]
                        . args)
    (let1 class (dbm-type->class type)
      (unless class (exit 1 "dbm type `~a' unknown" type))
      (match args
        [(dbmname) (do-dump dbmname class (or ofile (current-output-port)))]
        [else (usage)]))
    0))

(define (usage)
  (print "Usage: gosh dbm/dump [-o outfile][-t type] dbmname")
  (exit 0))

(define (do-dump name class output)
  (let1 dbm (guard (e [else (exit 1 "couldn't open dbm database: ~a"
                                  (~ e'message))])
              (dbm-open class :path name :rw-mode :read))
    (file-filter
     (^(in out)
       (dbm-for-each dbm (^(k v) (write (cons k v) out) (newline out))))
     :output output :temporary-file output)
    (dbm-close dbm)))


