#! /bin/sh
exec ../src/sizzle -s $0 $*
!#
;; scripts/sort-docstrings -- Sort the docstring file.
;;
;; Copyright (C) 2000 Martin Grabmueller <mgrabmue@cs.tu-berlin.de>
;;
;; This is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; 
;; This software is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;; 
;; You should have received a copy of the GNU General Public License
;; along with this package; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.  

(use-modules (core sort))

(define (help)
  (display "Usage: sort-docstrings.scm INFILE OUTFILE\n"))

(define (read-docstrings in-port)
  (let loop ((str (%read-line in-port)) (accu ""))
    (if (or (eof-object? str) (eof-object? (car str)))
	'()
	(let ((str (car str)))
	  (if (string=? str "\f")
	      (cons accu (loop (%read-line in-port) ""))
	      (loop (%read-line in-port) (string-append accu str "\n")))))))

(define (process-strings strings)
  (sort-list strings string<?))

(define (write-docstrings out-port strings)
  (for-each (lambda (str) (display str out-port) (display "\f\n" out-port))
	    strings)
  (display (length strings) (current-error-port))
  (display " docstrings processed\n" (current-error-port)))

(define (handle-docstrings infile outfile)
  (let ((docstrings (call-with-input-file infile read-docstrings)))
    (let ((processed-strings (process-strings docstrings)))
      (call-with-output-file outfile
	(lambda (port)
	  (write-docstrings port processed-strings))))))

(define (handle-command-line command-line)
  (cond ((= (length command-line) 3)
	 (handle-docstrings (cadr command-line) (caddr command-line)))
	(else
	 (help))))

(handle-command-line (command-line))
