Ugarit Manifest Maker
Check-in [fcfe83ff3b]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Ugarit manifest maker, with a reasonably complete ID3 parser in it...
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:fcfe83ff3b204eeee3471d746df5786c901ff7d4
User & Date: alaric 2015-04-20 13:19:08
Context
2015-05-09
14:44
Moved to Dublin Core metadata where possible. check-in: 7eb028a964 user: alaric tags: trunk
2015-04-20
13:19
Ugarit manifest maker, with a reasonably complete ID3 parser in it... check-in: fcfe83ff3b user: alaric tags: trunk
2014-12-28
10:30
Oh, and the licence. check-in: 858880a672 user: alaric tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to ugarit-manifest-maker.meta.

1
2
3
4
5
6
((license "BSD")
 (category data)
 (needs matchable)
 (test-depends test)
 (author "Alaric Snell-Pym")
 (synopsis "A tool to generate manifests for Ugarit archival mode"))


|
|


1
2
3
4
5
6
((license "BSD")
 (category data)
 (needs matchable srfi-37 fnmatch)
 (test-depends test exif)
 (author "Alaric Snell-Pym")
 (synopsis "A tool to generate manifests for Ugarit archival mode"))

Added ugarit-manifest-maker.scm.







































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
(use posix)
(use srfi-1)

(use ugarit-mime)
(use srfi-37)
(use miscmacros)
(use fnmatch)
(use exif)

(define *excludes* '())
(define *total-objects* 0)
(define *total-bytes* 0)

(define (path-size path)
  (if (directory? path)
      (fold
       +
       0
       (map
        (lambda (relpath)
          (path-size (make-pathname path relpath)))
        (directory path)))
      ;; Not a directory
      (file-size path)))

(define (included-file? path)
  (let ((name (pathname-strip-directory path)))
    (not (any
          (lambda (pattern)
            (fnmatch pattern name))
          *excludes*))))

(define (get-jpeg-metadata path)
  ;; FIXME http://www.cipa.jp/std/documents/e/DC-008-2012_E.pdf
  '((description . #f)
    (latitude . #f)
    (longitude . #f)
    (altitude . #f)
    (place-name . #f)
    (featuring . #f)
    (event-name . #f)
    (creator . #f)
    (creation-date . #f)
    (keyword . #f)))

(define debug (lambda x (void)))
#;(define debug printf)

(define (alist-update! alist key value)
  (let ((pair (assq key alist)))
    (if (and (pair? pair) (not (cdr pair)))
        (begin
          (set-cdr! pair value) ;; Exists but is #f, so overwrite
          alist)
        (cons (cons key value) alist))))

(define genres
  '((0 . "Blues")
    (1 . "Classic Rock")
    (2 . "Country")
    (3 . "Dance")
    (4 . "Disco")
    (5 . "Funk")
    (6 . "Grunge")
    (7 . "Hip-Hop")
    (8 . "Jazz")
    (9 . "Metal")
    (10 . "New Age")
    (11 . "Oldies")
    (12 . "Other")
    (13 . "Pop")
    (14 . "R&B")
    (15 . "Rap")
    (16 . "Reggae")
    (17 . "Rock")
    (18 . "Techno")
    (19 . "Industrial")
    (20 . "Alternative")
    (21 . "Ska")
    (22 . "Death Metal")
    (23 . "Pranks")
    (24 . "Soundtrack")
    (25 . "Euro-Techno")
    (26 . "Ambient")
    (27 . "Trip-Hop")
    (28 . "Vocal")
    (29 . "Jazz+Funk")
    (30 . "Fusion")
    (31 . "Trance")
    (32 . "Classical")
    (33 . "Instrumental")
    (34 . "Acid")
    (35 . "House")
    (36 . "Game")
    (37 . "Sound Clip")
    (38 . "Gospel")
    (39 . "Noise")
    (40 . "AlternRock")
    (41 . "Bass")
    (42 . "Soul")
    (43 . "Punk")
    (44 . "Space")
    (45 . "Meditative")
    (46 . "Instrumental Pop")
    (47 . "Instrumental Rock")
    (48 . "Ethnic")
    (49 . "Gothic")
    (50 . "Darkwave")
    (51 . "Techno-Industrial")
    (52 . "Electronic")
    (53 . "Pop-Folk")
    (54 . "Eurodance")
    (55 . "Dream")
    (56 . "Southern Rock")
    (57 . "Comedy")
    (58 . "Cult")
    (59 . "Gangsta")
    (60 . "Top 40")
    (61 . "Christian Rap")
    (62 . "Pop/Funk")
    (63 . "Jungle")
    (64 . "Native American")
    (65 . "Cabaret")
    (66 . "New Wave")
    (67 . "Psychadelic")
    (68 . "Rave")
    (69 . "Showtunes")
    (70 . "Trailer")
    (71 . "Lo-Fi")
    (72 . "Tribal")
    (73 . "Acid Punk")
    (74 . "Acid Jazz")
    (75 . "Polka")
    (76 . "Retro")
    (77 . "Musical")
    (78 . "Rock & Roll")
    (79 . "Hard Rock")
    (80 . "Folk")
    (81 . "Folk-Rock")
    (82 . "National Folk")
    (83 . "Swing")
    (84 . "Fast Fusion")
    (85 . "Bebob")
    (86 . "Latin")
    (87 . "Revival")
    (88 . "Celtic")
    (89 . "Bluegrass")
    (90 . "Avantgarde")
    (91 . "Gothic Rock")
    (92 . "Progressive Rock")
    (93 . "Psychedelic Rock")
    (94 . "Symphonic Rock")
    (95 . "Slow Rock")
    (96 . "Big Band")
    (97 . "Chorus")
    (98 . "Easy Listening")
    (99 . "Acoustic")
    (100 . "Humour")
    (101 . "Speech")
    (102 . "Chanson")
    (103 . "Opera")
    (104 . "Chamber Music")
    (105 . "Sonata")
    (106 . "Symphony")
    (107 . "Booty Bass")
    (108 . "Primus")
    (109 . "Porn Groove")
    (110 . "Satire")
    (111 . "Slow Jam")
    (112 . "Club")
    (113 . "Tango")
    (114 . "Samba")
    (115 . "Folklore")
    (116 . "Ballad")
    (117 . "Power Ballad")
    (118 . "Rhythmic Soul")
    (119 . "Freestyle")
    (120 . "Duet")
    (121 . "Punk Rock")
    (122 . "Drum Solo")
    (123 . "A capella")
    (124 . "Euro-House")
    (125 . "Dance Hall")))

(define (process-genre g)
  (if (and (char=? (string-ref g 0) #\()
           (char=? (string-ref g
                               (- (string-length g) 1)) #\)))
      (let* ((genre-number (string->number
                            (substring g 1 (- (string-length g) 1))))
             (genre-lookup (assoc genre-number genres)))
        (if genre-lookup
            (cdr genre-lookup)
            g))
      g))

(define (remove-trailing-term-if-present str)
  (if (zero? (string-length str))
      str
      (if (char=? (string-ref str
                              (- (string-length str) 1))
                  #\x00)
          (substring str 0 (- (string-length str) 1))
          str)))

(define (get-mp3-metadata path)
  ;; FIXME http://id3.org/id3v2-00 / http://id3.org/id3v2.3.0
  (with-input-from-file path
    (lambda ()
      (let/cc
       abort*
       (let* ((metadata
               '((creator . #f)
                 (creation-date . #f)
                 (featuring . #f)
                 (collection-name . #f)
                 (collection-volume . #f)
                 (collection-volumes . #f)
                 (volume-index . #f)
                 (volume-size . #f)))
              (abort
               (lambda ()
                 (abort* metadata)))
              (read-id3v22
               (lambda ()
                 (debug "Reading v2.2")
                 (let* ((flags (read-byte))
                        (unsync (not (zero? (bitwise-and flags 128))))
                        (compressed (not (zero? (bitwise-and flags 64))))
                        (size4 (read-byte))
                        (size3 (read-byte))
                        (size2 (read-byte))
                        (size1 (read-byte))
                        (size (+
                               size1
                               (* 128 size2)
                               (* 16384 size3)
                               (* 2097152 size4)))
                        (*bytes-left* size)
                        (rb ;; read-byte replacement
                         (if unsync
                             ;; Unsynchronised mode
                             (let ((last-byte #f))
                               (lambda ()
                                 (set! *bytes-left*
                                       (- *bytes-left* 1))
                                 (let ((b (read-byte)))
                                   (when (and
                                          (= last-byte #xff)
                                          (= b #x00))
                                         ;; Skip #x00 unsync byte
                                         (set! *bytes-left*
                                               (- *bytes-left* 1))
                                         (set! b (read-byte)))
                                   (set! last-byte b)
                                   b)))
                             ;; Normal mode
                             (lambda ()
                               (set! *bytes-left*
                                     (- *bytes-left* 1))
                               (read-byte)))))
                   (debug "Read header, size = ~S, unsync = ~S\n" size unsync)

                   (let loop ((md (alist-copy metadata)))
                     (debug "Bytes left: ~S\n" *bytes-left*)
                     (when (< *bytes-left* 10)
                           (abort* md))

                     (let* ((fid1 (rb))
                            (fid2 (rb))
                            (fid3 (rb))
                            (fid (list->string
                                  (list
                                   (integer->char fid1)
                                   (integer->char fid2)
                                   (integer->char fid3))))
                            (fsize3 (rb))
                            (fsize2 (rb))
                            (fsize1 (rb))
                            (fsize (+
                                    fsize1
                                    (* 256 fsize2)
                                    (* 65536 fsize3)))
                            (encoding (rb))
                            (data
                             (remove-trailing-term-if-present
                              (list->string
                               (reverse
                                (list-tabulate
                                 (- fsize 1) ;; Remove encoding byte
                                 (lambda (i) (integer->char (rb)))))))))
                       (debug "FID: ~S size: ~S [~S]\n" fid fsize data)
                       (cond
                        ((string=? fid "\x00\x00\x00")
                         (abort* md))
                        ((string=? fid "TT2")
                         (loop (alist-update! md 'name data)))
                        ((string=? fid "TP1")
                         (loop (alist-update! md 'creator data)))
                        ((string=? fid "TCM")
                         (loop (alist-update! md 'creator data)))
                        ((string=? fid "TYE")
                         (loop (alist-update! md 'creation-date data)))
                        ((string=? fid "TCO")
                         (loop (alist-update! md 'keyword (process-genre data))))
                        ((string=? fid "TAL")
                         (loop (alist-update! md 'collection-name data)))
                        ((string=? fid "TRK")
                         (and-let* ((parts (string-split data "/"))
                                    ((= (length parts) 2))
                                    (track (string->number (first parts)))
                                    (tracks (string->number (second parts))))
                                   (loop (alist-update!
                                          (alist-update! md 'volume-index track)
                                          'volume-size tracks)))
                         (loop md))
                        ((string=? fid "TPA")
                         (and-let* ((parts (string-split data "/"))
                                    ((= (length parts) 2))
                                    (volume (string->number (first parts)))
                                    (volumes (string->number (second parts))))
                                   (loop (alist-update!
                                          (alist-update! md 'collection-volume volume)
                                          'collection-volumes volumes)))
                         (loop md))
                        (else
                         (printf "   ;; Unknown ID3 tag ~S=~S\n"
                                 fid
                                 (string-append
                                  (make-string 1 (integer->char encoding))
                                  data))
                         (loop md))))))))
              (read-id3v23
               (lambda ()
                 (debug "Reading v2.3")
                 (let* ((flags (read-byte))
                        (unsync (not (zero? (bitwise-and flags 128))))
                        (extended (not (zero? (bitwise-and flags 64))))
                        (experimental (not (zero? (bitwise-and flags 32))))
                        (size4 (read-byte))
                        (size3 (read-byte))
                        (size2 (read-byte))
                        (size1 (read-byte))
                        (size (+
                               size1
                               (* 128 size2)
                               (* 16384 size3)
                               (* 2097152 size4)))
                        (*bytes-left* size)
                        (rb ;; read-byte replacement
                         (if unsync
                             ;; Unsynchronised mode
                             (let ((last-byte #f))
                               (lambda ()
                                 (set! *bytes-left*
                                       (- *bytes-left* 1))
                                 (let ((b (read-byte)))
                                   (when (and
                                          (= last-byte #xff)
                                          (= b #x00))
                                         ;; Skip #x00 unsync byte
                                         (set! *bytes-left*
                                               (- *bytes-left* 1))
                                         (set! b (read-byte)))
                                   (set! last-byte b)
                                   b)))
                             ;; Normal mode
                             (lambda ()
                               (set! *bytes-left*
                                     (- *bytes-left* 1))
                               (read-byte)))))
                   (debug "Read header, size = ~S, unsync = ~S\n" size unsync)
                   (when extended
                         (debug "Reading extended header\n")
                         (let* ((size4 (rb))
                                (size3 (rb))
                                (size2 (rb))
                                (size1 (rb))
                                (size (+
                                       size1
                                       (* 256 size2)
                                       (* 65536 size3)
                                       (* 16777216 size4)))
                                (eflags1 (rb))
                                (eflags2 (rb)))
                           (when (not (zero?
                                       (bitwise-and eflags1 128)))
                                 ;; Skip CRC
                                 (rb)
                                 (rb)
                                 (rb)
                                 (rb))))
                   (let loop ((md (alist-copy metadata)))
                     (debug "Bytes left: ~S\n" *bytes-left*)
                     (when (< *bytes-left* 10)
                           (abort* md))

                     (let* ((fid1 (rb))
                            (fid2 (rb))
                            (fid3 (rb))
                            (fid4 (rb))
                            (fid (list->string
                                  (list
                                   (integer->char fid1)
                                   (integer->char fid2)
                                   (integer->char fid3)
                                   (integer->char fid4))))
                            (fsize4 (rb))
                            (fsize3 (rb))
                            (fsize2 (rb))
                            (fsize1 (rb))
                            (fsize (+
                                    fsize1
                                    (* 256 fsize2)
                                    (* 65536 fsize3)
                                    (* 16777216 fsize4)))
                            (flags1 (rb))
                            (flags2 (rb))
                            (encoding (rb))
                            (data
                             (remove-trailing-term-if-present
                              (list->string
                               (reverse
                                (list-tabulate
                                 (- fsize 1) ;; Remove encoding byte
                                 (lambda (i) (integer->char (rb)))))))))
                       (debug "FID: ~S size: ~S [~S]\n" fid fsize data)
                       (cond
                        ((string=? fid "\x00\x00\x00\x00")
                         (abort* md))
                        ((string=? fid "TIT2")
                         (loop (alist-update! md 'name data)))
                        ((string=? fid "TPE1")
                         (loop (alist-update! md 'creator data)))
                        ((string=? fid "TCOM")
                         (loop (alist-update! md 'creator data)))
                        ((string=? fid "TPE2")
                         (loop (alist-update! md 'featuring data)))
                        ((string=? fid "TPE3")
                         (loop (alist-update! md 'featuring data)))
                        ((string=? fid "TPE4")
                         (loop (alist-update! md 'editor data)))
                        ((string=? fid "TPUB")
                         (loop (alist-update! md 'publisher data)))
                        ((string=? fid "TYER")
                         (loop (alist-update! md 'creation-date data)))
                        ((string=? fid "TCON")
                         (loop (alist-update! md 'keyword (process-genre data))))
                        ((string=? fid "TALB")
                         (loop (alist-update! md 'collection-name data)))
                        ((string=? fid "TRCK")
                         (and-let* ((parts (string-split data "/"))
                                    ((= (length parts) 2))
                                    (track (string->number (first parts)))
                                    (tracks (string->number (second parts))))
                                   (loop (alist-update!
                                          (alist-update! md 'volume-index track)
                                          'volume-size tracks)))
                         (loop md))
                        ((string=? fid "TPOS")
                         (and-let* ((parts (string-split data "/"))
                                    ((= (length parts) 2))
                                    (volume (string->number (first parts)))
                                    (volumes (string->number (second parts))))
                                   (loop (alist-update!
                                          (alist-update! md 'collection-volume volume)
                                          'collection-volumes volumes)))
                         (loop md))
                        (else
                         (printf "   ;; Unknown ID3 tag ~S=~S\n"
                                 fid
                                 (if (< (string-length data) 160)
                                     data
                                     (string-append
                                      (substring data 0 100)
                                      "...")))
                         (loop md)))))))))
        (when (not (= (read-byte) #x49))
              (abort))
        (when (not (= (read-byte) #x44))
              (abort))
        (when (not (= (read-byte) #x33))
              (abort))
        (let ((version1 (read-byte))
              (version2 (read-byte)))
          (case version1
            ((2) (read-id3v22))
            ((3) (read-id3v23))
            (else (abort)))))))))

(define (get-ogg-metadata path)
  ;; FIXME http://xiph.org/vorbis/doc/v-comment.html
  '((creator . #f)
    (creation-date . #f)
    (featuring . #f)
    (collection-name . #f)
    (collection-volume . #f)
    (collection-volumes . #f)
    (volume-index . #f)
    (volume-size . #f)))

(define (get-pdf-metadata path)
  '((creator . #f)
    (topic . #f)
    (creation-date . #f)
    (keyword . #f)))

(define (get-ps-metadata path)
  '((creator . #f)
    (topic . #f)
    (creation-date . #f)
    (keyword . #f)))

(define (generate-manifest path)
  (if (directory? path)
      ;; Recurse therein
      (for-each (lambda (relname)
                  (generate-manifest (make-pathname path relname)))
                (directory path))

      ;; Not a directory
      (when (included-file? path)
       (let* ((name (pathname-file path))
              (*got-name* #f)
              (print-item
               (lambda (item)
                 (if (and (eq? (car item) 'name)
                          (cdr item))
                     (set! *got-name* #f))
                 (if (cdr item)
                     (printf "  (~S = ~S)\n" (car item) (cdr item))
                     (printf "  #;(~S = \"\")\n" (car item)))))
              (mime-type (extension->mimetype (string-append "."
                                                             (or
                                                              (pathname-extension path)
                                                              "")))))
         (printf "(object ~S\n" path)
         (printf "  (filename = ~S)\n" path)
         (printf "  (mime-type = ~S)\n" mime-type)

         (newline)

         (cond
          ((string=? mime-type "image/jpeg")
           (for-each print-item
                     (get-jpeg-metadata path)))

          ((string=? mime-type "audio/mpeg")
           (for-each print-item
                     (get-mp3-metadata path)))

          ((string=? mime-type "audio/ogg")
           (for-each print-item
                     (get-ogg-metadata path)))

          ((string=? mime-type "application/pdf")
           (for-each print-item
                     (get-pdf-metadata path)))

          ((string=? mime-type "application/postscript")
           (for-each print-item
                     (get-ps-metadata path)))

          (else
           (unless *got-name*
                   (printf "  (name = ~S)\n" name))
           (printf "  #;(description = \"\")\n")))

         (newline)

         (let ((stat (file-stat path)))
           (printf "  (mtime = ~S)\n" (vector-ref stat 8))
           (printf "  (ctime = ~S)\n" (vector-ref stat 7)))

         (let ((size (path-size path)))
           (printf "  (size = ~S))\n" size)
           (set! *total-bytes*
                 (+ *total-bytes* size))
           (set! *total-objects*
                 (+ *total-objects* 1)))

         (newline)))))

(define (usage)
  (with-output-to-port (current-error-port)
    (lambda ()
      (printf "Usage: ugarit-manifest-maker [options...] [files...]\n")
      (printf "  -e <pattern>   Exclude files matching pattern\n")))
  (exit 1))

(define things-to-import
  (args-fold
   (command-line-arguments)
   (list
    (option
     '(#\h "help") #f #f
     (lambda _
       (usage)))
    (option
     '(#\e "exclude") #t #f
     (lambda (o n x vals)
       (push! x *excludes*)
       vals)))
   (lambda (o n x vals)
     (usage))
   cons
   '()))

(for-each generate-manifest
          things-to-import)

(define (format-bytes b)
  (cond
   ((<= (* 1024 1024 1024 1024) b)
    (sprintf "~ATiB" (inexact->exact (round (/ b 1024 1024 1024 1024)))))
   ((<= (* 1024 1024 1024) b)
    (sprintf "~AGiB" (inexact->exact (round (/ b 1024 1024 1024)))))
   ((<= (* 1024 1024) b)
    (sprintf "~AMiB" (inexact->exact (round (/ b 1024 1024)))))
   ((<= (* 1024) b)
    (sprintf "~AKiB" (inexact->exact (round (/ b 1024)))))
   (else
    (sprintf "~AB" b))))

(printf ";;; TOTAL: ~S objects / ~A\n"
        *total-objects*
        (format-bytes *total-bytes*))