#!/usr/bin/env scheme-script
;; -*- mode: scheme; coding: utf-8 -*- !#
;; Copyright © 2009, 2010 Göran Weinholt <goran@weinholt.se>

;; Permission is hereby granted, free of charge, to any person obtaining a
;; copy of this software and associated documentation files (the "Software"),
;; to deal in the Software without restriction, including without limitation
;; the rights to use, copy, modify, merge, publish, distribute, sublicense,
;; and/or sell copies of the Software, and to permit persons to whom the
;; Software is furnished to do so, subject to the following conditions:

;; The above copyright notice and this permission notice shall be included in
;; all copies or substantial portions of the Software.

;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
;; THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
;; DEALINGS IN THE SOFTWARE.
#!r6rs

(import (weinholt compression zip)
        (rnrs))

(define (print . x) (for-each display x) (newline))

(define (parse-args args)
  (define (help . msg)
    (let ((x (current-error-port)))
      (when msg (display (car msg) x) (newline x) (newline x))
      (display "zip - .ZIP archiver

Usage: zip <filename.zip> <files> ...

This program creates a .ZIP file and adds the given files to it.

Author: Göran Weinholt <goran@weinholt.se>.
" x)
      (exit 1)))
  (let lp ((filename #f)
           (args args))
    (cond ((string=? (car args) "--")
           (lp filename (cdr args)))
          ((null? args)
           (help "ERROR: No filename given."))
          (else
           (call-with-port (open-file-output-port (car args))
             (lambda (zipport)
               (print "Creating ZIP archive: " (car args) "\n")
               (let lp ((files (cdr args))
                        (centrals '()))
                 (cond ((null? files)
                        (append-central-directory zipport (reverse centrals)))
                       (else
                        (display "Adding ")
                        (display (car files))
                        (display " ... ")
                        (flush-output-port (current-output-port))
                        (let ((central-rec (append-file zipport (car files))))
                          (print (if (zero? (central-directory-uncompressed-size
                                             central-rec))
                                     100
                                     (exact
                                      (round
                                       (inexact
                                        (* 100 (/ (central-directory-compressed-size
                                                   central-rec)
                                                  (central-directory-uncompressed-size
                                                   central-rec)))))))
                                 "%")
                          (lp (cdr files)
                              (cons central-rec centrals))))))))))))

(parse-args (cdr (command-line)))
