File: cdlabelgen

package info (click to toggle)
cdlabelgen 1.5.0-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 384 kB
  • ctags: 34
  • sloc: perl: 244; makefile: 82
file content (643 lines) | stat: -rwxr-xr-x 20,063 bytes parent folder | download
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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
#!/usr/bin/perl -w
#
# cdlabelgen - a program for making cd jewel box covers and traycards
#
# Copyright (C) 1998, 1999 B. W. Fitzpatrick <fitz@red-bean.com>
#
# Author: B. W. Fitzpatrick <fitz@red-bean.com>
# Maintainer: B. W. Fitzpatrick <fitz@red-bean.com>
# Created: October, 1998
# Keywords: postscript, labels, cd-rom
#
# $Id: cdlabelgen,v 1.48 1999/06/23 17:48:15 bwf Exp $
#
# This program 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 program 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 program; if not, you can either send email to this
# program's maintainer or write to: The Free Software Foundation,
# Inc.; 59 Temple Place, Suite 330; Boston, MA 02111-1307, USA.

use Getopt::Std;
use strict;

# Prepare to slurp up the file
my $template = 'template.ps';
my $directory;

# Modify this if you want to store your template somewhere else
# Perhaps we can make this part of a conf file in the future?
my @where_is_the_template = ('/usr/share/cdlabelgen/',
									  '/usr/local/share/cdlabelgen/',
									  '/usr/local/lib/cdlabelgen/',
									  '/etc/cdlabelgen/',
									  './postscript/');

my $found_template = '';
foreach $directory (@where_is_the_template) {
	if (-e ("$directory$template")) {
		$template = "$directory$template";
		$found_template = 1;
		#Put the template dir at the front of the list for later
		push @where_is_the_template,$directory;
		last;
	}
}

my %flag;
my @items;
my $eps_cover_file;
my $eps_tray_card_file;
my $eps_cover_bounds;
my $eps_tray_card_bounds;
my $eps_cover_scale;
my $eps_tray_card_scale;
my $date;
my $no_items = 0;
my	$print_to_file;
my $default_eol = $/;
my $show_plaque = 1;
my $default_bounds = "\n/bound1x 10 def\n/bound1y 10 def\n/bound2x 10 def\n/bound2y 10 def\n/scaleratio 1 def\n";
######################################################################
# Argument processing
getopts('c:s:f:i:d:S:t:T:e:E:o:Dbwh', \%flag);    

if($flag{h}) {
	&show_help();
}

# Do we have enough flags to go on? Bail here if not.
&show_help unless $flag{c} && $flag{'s'};

if($flag{t} && -f $flag{t})
{
    $template = $flag{t};
    $found_template = 1; 
}
unless ($found_template) {	&error("Postscript template file not found") }

# Category/title
my $category = &scrub($flag{c});

# Subcategory/subtitle
my $subcategory = &scrub($flag{'s'});

# Items | directories | songs
if($flag{f}) {
   my $infile = $flag{'f'};
   @items = split(/\n/, &scrub(get_file_contents($infile,@where_is_the_template)));
}
elsif ($flag{i}){
	my $clean_items = &scrub($flag{i});
   @items = (split (/%/, $clean_items));
}
else {
   $no_items = 1;
	@items = (' ',' '); # Need to at least have an empty array.
}

# Word wrapping
if($flag{w}) {
   @items = &word_wrap(@items);
}

# Format for a postscript array of strings now that we've wrapped (or not)
@items = map {"($_)"} @items;

# Date stuff
if ($flag{d}) {
	$date = &scrub($flag{d});
}
else {
	$date = &get_date();
}
if ($flag{D}) {
	$date = '';
}
# cover espfile scaling
if ($flag{S}) {
	unless (($flag{S} =~ /^\d+(\.\d+)?$/) || ($flag{S} =~ /^\.\d+$/)) {
		&error("-S Cover scale ratio must be a number");
}
	$eps_cover_scale = $flag{S};
}
else { # Else, no scaling
	$eps_cover_scale = 1;
}
# traycard espfile scaling
if ($flag{T}) {
	unless (($flag{T} =~ /^\d+(\.\d+)?$/) || ($flag{T} =~ /^\.\d+$/)) {
		&error("-T: Traycard scale ratio must be a number");
}
	$eps_tray_card_scale = $flag{T};
}
else { # Else, no scaling
	$eps_tray_card_scale = 1;
}
# cover epsfile
if ($flag{e}) {
	$eps_cover_file = get_file_contents($flag{e},@where_is_the_template);
	$eps_cover_bounds = &get_bounding_box($flag{e}, $eps_cover_scale);
}
else {
	$eps_cover_bounds = $default_bounds;
	$eps_cover_file = '';
}
# traycard epsfile
if ($flag{E}) {
	$eps_tray_card_file = get_file_contents($flag{E},@where_is_the_template);
	$eps_tray_card_bounds = &get_bounding_box($flag{E}, $eps_tray_card_scale);
}
else {
	$eps_tray_card_bounds = $default_bounds;
	$eps_tray_card_file = '';
}
# output file
if ($flag{o}) {
	open (OUT, ">$flag{o}") or &error("Cannot open $flag{o} for writing: $!");
	$print_to_file = 1;
}

# If this flag is set, don't print the plaque on the back (give more room for items).
if ($flag{b}) {
   $show_plaque = 0;
}

######################################################################
# Grab the template

my $psout = get_file_contents($template);

$psout =~ s/TOKEN_BAN_STRING/$category/;
$psout =~ s/TOKEN_BAN_STRING/$category/;
$psout =~ s/TOKEN_SUBBAN_STRING/$subcategory/g;
$psout =~ s/TOKEN_DATE/$date/g;
$psout =~ s/TOKEN_ITEMS/@items/g;
$psout =~ s/TOKEN_EPS_BOUNDS/$eps_cover_bounds/g;
$psout =~ s/TOKEN_COVER_EPS/$eps_cover_file/g;
$psout =~ s/TOKEN_TRAY_CARD_BOUNDS/$eps_tray_card_bounds/g;
$psout =~ s/TOKEN_TRAY_CARD_EPS/$eps_tray_card_file/g;
$psout =~ s/TOKEN_PLAQUE_P/$show_plaque/;

if ($print_to_file) { print OUT $psout; }
else { print $psout; }
##################################################################################
# Subroutines

sub get_date {
   my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
   $mon++;
   if ($mon  < 10) { $mon = "0$mon"; }
   if ($mday < 10) { $mday = "0$mday"; }
	$year += 1900; 
	return "$year-$mon-$mday";     # ISO 8601:1988 

}   


sub get_full_filename {
	my $fname = shift @_;
	my ($directory,$realname,$found);
	$found=0;
	$realname=$fname;
	foreach $directory ("",@_) {
		if ( -f "$directory$fname") {
			$realname = "$directory$fname";
			$found = 1;
			last;
		}
	}
	return $realname;
}


sub get_file_contents {
	my $fname = shift @_;
	local $/ = undef;
	my ($directory,$realname,$found);
	$found=0;
	foreach $directory ("",@_) {
		if (open (GET_FILE, "$directory$fname")) {
			$realname = "$directory$fname";
			$found = 1;
			last;
		}
	}
	unless ($found) {	&error("Cannot open $fname") }
	$_ = <GET_FILE>;

   # Check for zero length file
   if (length($_) == 0) {
      &error("file $fname is zero length!");
   }

	close GET_FILE;
	return $_;
}


sub scrub {
	my $string = shift @_;
	$string =~ s?\(?\\050?g;
	$string =~ s?\)?\\051?g;
	return $string;
}


sub get_bounding_box {
	my $file = shift @_;
	my $eps_scale = shift @_;
	my $bounds;
	my $got_bounding_box = 0;
	$/ = $default_eol;
	open (EPS, get_full_filename("$file",@where_is_the_template)) or &error("Cannot open epsfile $file");
	while (<EPS>) {
		chomp;
		if (s/\%\%BoundingBox: //) {
			my ($llx, $lly, $urx, $ury) = split (/ /, $_);
			$bounds ="\n/bound1x $llx def\n/bound1y $lly def\n/bound2x $urx def\n/bound2y $ury def\n/scaleratio $eps_scale def\n";
			$got_bounding_box = 1;
			last;
		}
	}
	unless ($got_bounding_box) {
		&error("Cannot get BoundingBox from $file. Are you sure it's an EPS file?");
	}
undef $/;
	return $bounds;
}

# Ugh. This is nasty and hackish, but it sort of does the trick. If
# anyone can improve the word wrapping capability (either in this
# routine(yuck) or in the postscript (yay!), *please* do and send me
# the diffs.  I'd love to do this in postscript, but I just don't have
# the time right now.
sub word_wrap {
   my @long_items = @_;
   my $long_item;
   my $max_cols; 
   my @wrapped_items;

   # Account for the super long possibilities of the single column display
   # Note that by wrapping, it may add a column (or 2). 
 SWITCH: {
      ($#long_items < 22)  && do { $max_cols = 63; last SWITCH;}; # 1 col
      ($#long_items < 54)  && do { $max_cols = 27; last SWITCH;}; # 2 cols
      ($#long_items < 110) && do { $max_cols = 23; last SWITCH;}; # 3 cols
      $max_cols = 21;                                             # 4 cols
   }

   foreach $long_item (@long_items) {
      my $untouched = 1;
      while (length($long_item) > $max_cols){
         $untouched = 0;
         # chop the first bit off and find the word boundary
         my $tmp = substr($long_item, 0, $max_cols);
         while ($tmp =~ /\S$/ && (length($tmp) > 0)) {
            $tmp = substr($tmp, 0, (length($tmp) -1));
         }

         # If $tmp gets to 0, then there is no whitespace, so we can't wrap
         if (length($tmp) == 0) { 
            $tmp = chop $tmp;
            push (@wrapped_items, $long_item);
            # Ack, hack, ook, barf
            $long_item = '';
         }         
         else {
            my $wrappee = substr($long_item, 0, length($tmp));
            push (@wrapped_items, $wrappee);
            # add a few spaces (to simulate tab) to the 
            # beginning of the rest of the string
            $long_item = "   " . substr($long_item, length($tmp));
         }
      }
      # Everyone will eventually fall through to here, so we need to account for 0 length
      if ((length($long_item) > 0) || $untouched == 1) {
         push (@wrapped_items, $long_item);
      }
   }
   return @wrapped_items;
}


sub show_help {
	print <<EOT;
usage: $0 -c <category> -s <subcategory> [-i <item1%item2%etc> -f<itemsfile> 
       -e <cover_epsfile> -S <cover_eps_scaleratio> -E <tray_epsfile> 
       -T <tray_eps_scaleratio> -d <date> -D -o <outputfile>
       -t <template> -b -w -h]
EOT
exit 255;
}


sub error {
	my $err = shift @_;
	warn "$0: $err\n\n";
	&show_help;
}

##################################################################################
# Documentation

=head1 NAME

cdlabelgen - generates frontcards and traycards for CDs

=head1 SYNOPSIS

cdlabelgen B<-c> <category> B<-s> <subcategory> [B<-i> <item1%item2%etc> B<-f><itemsfile> 
B<-e> <cover_epsfile> B<-S> <cover_eps_scaleratio> B<-E> <tray_epsfile> 
B<-T> <tray_eps_scaleratio> B<-d> <date> B<-D> B<-o> <outputfile>
B<-t> <template> B<-b> B<-w> B<-h>]


=head1 VERSION

=over

=item Version 1.5.0

=back

=head1 DESCRIPTION

cdlabelgen's purpose in life is twofold:

=over

=item * To be run automatically and swiftly from a shell script and
automatically generate a frontcard and a traycard for a cd--usually
data archive cd's. The traycard (which goes behind the CD itself) is
U-shaped and the ends of the CD case bear the label of what the CD is.

=item * To have a minimum of dependencies--cdlabelgen only requires perl.

=back

cdlabelgen was designed to simplify the process of generating labels
for CD's. It originated as a program to allow auto generation of
frontcards and traycards for CD's burned via an automated mechanism
(specifically for archiving data), but has now become popular for
labelling CD compilations of mp3's, and copies of CDs. Note that
cdlabelgen does not actually print anything--it just spits out
postscript, which you can then do with as you please.

The latest version of cdlabelgen as well as this document can be
found at http://www.red-bean.com/~bwf/software/cdlabelgen/. Please
make certain that you are using the latest version before submitting
any bug reports or patches.

Thanks to some really hairy postscript from GNUPlot, cdlabelgen now
supports arbitrary ISOLatin font characters. This means that you can
print out characters like ������ (ASCII \230, \248, \229, \198, \216
and \197)

cdlabelgen comes with several eps images for you to use on your
labels. These images can be found in /usr/share/cdlabelgen or
/usr/local/share/cdlabelgen, depending on your installation. Included
are a Recycling icon, an mp3 icon, the Compact Disc icon (with and
without 'Digital' on it), Tux the penguin, and the new Debian 'swirl'
logo. If you have any freely redistributable eps files that you would
like to see distributed with cdlabelgen, send them to me at
E<lt>F<fitz@red-bean.com>E<gt>, and I may be able to include them in
the next distribution of cdlabelgen (size, copyright, and subject
matter permitting).

Note that cdlabelgen now prints a 'tongue' as part of the
traycard. This folds around and is viewable from the front in jewel
boxes that are entirely clear (CD holder piece is not opaque). If you
do not have a clear CD holder in your jewel box, you may find it
easier to just cut the 'tongue' off--it's a bit easier to fold without
it.

cdlabelgen requires Perl Version 5.003 or greater. Ghostscript is not
required, but B<is> recommended so that you can test out your labels
without wasting paper.

=head1 SWITCHES

=over

=item B<-c category>

Set the category (title) for the CD

=item B<-s subcategory>

Set the subcategory (subtitle) for the CD

=item B<-i items>

'items' should be a '%' separated list of items to print on the
traycard of the CD.  Note that if the items list is more than 192 (256
with the B<-b> flag) items long, cdlabelgen will truncate
it. cdlabelgen automatically flows the items into 2, 3, or 4 columns
and scales the fontsize accordingly. Excessively wide items will be
scaled down (into illegibility sometimes--I plan on working on that in
a future release) to fit horizontally. You can insert blank lines by
inserting 2 percent signs in a row into the items list.

=item B<-f filename>

Get item names from file named filename. Each item should be on its
own line separated by carriage returns. Note that if the items list is
more than 192 (256 with the B<-b> flag) items long, cdlabelgen will
truncate it. cdlabelgen automatically flows the items into 2, 3, or 4
columns and scales the fontsize accordingly. Excessively wide items
will be scaled down (into illegibility sometimes--I plan on working on
that in a future release) to fit horizontally. You can insert blank
lines by placing blank lines between items in this file

=item B<-d date>

Set the date to be used as 'date' if not set or not overridden with
the B<-D> flag, today's date will be used (default is today's
date). Use this option if you don't like cdlabelgen's default format of
YYCC-MM-YY, for example.

=item B<-D> 

Do not print B<any> date (overrides B<-d> as well)

=item B<-e cover_epsfile>

Filename of eps file to print on cover. Note that cdlabelgen requires
that the eps file contain a proper '%%BoundingBox LLx LLy URx URy'
declaration according to the PostScript Document Structuring
Conventions. cdlabelgen uses this line to determine the dimensions of
the eps graphic so that it can position it appropriately on the
cover. Note that cdlabelgen first looks for this file in your working
directory. If it doesn't find it there, it will look in the list of
directories where the default eps files are stored (see
@where_is_the_template). This makes it easy to use the images shipped
with cdlabgelgen without typing miles of pathnames.

=item B<-S cover_eps_scaleratio>

The ratio by which you want to scale the epsfile that appears on the
cover. If you omit this flag, cdlabelgen assumes a scaleratio of
1. This flag allows you to squeeze larger graphics into the cover or
expand smaller graphics to fill the cover. Scaleratio must be a number
(int or float).

=item B<-E tray_epsfile>

Filename of eps file to print on traycard. Note that cdlabelgen
requires that the eps file contain a proper '%%BoundingBox LLx LLy URx
URy' declaration according to the PostScript Document Structuring
Conventions. cdlabelgen uses this line to determine the dimensions of
the eps graphic so that it can position it appropriately on the
cover. Note that cdlabelgen first looks for this file in your working
directory. If it doesn't find it there, it will look in the list of
directories where the default eps files are stored (see
@where_is_the_template). This makes it easy to use the images shipped
with cdlabgelgen without typing miles of pathnames.


=item B<-T tray_eps_scaleratio>

The ratio by which you want to scale the epsfile that appears on the
traycard. If you omit this flag, cdlabelgen assumes a scaleratio of
1. This flag allows you to squeeze larger graphics into the traycard or
expand smaller graphics to fill the traycard. Scaleratio must be a number
(int or float).

=item B<-o outputfile>

If the B<-o> flag is used, cdlabelgen prints to outputfile instead of STDOUT. 

=item CB<-t template>

Specify explicitly which template to use. This is useful if you need
to debug a template, use a different template, or if you have created
your own template to use in lieu of the one provided with
cdlabelgen. Remember to send in patches to me at
E<lt>F<fitz@red-bean.com>E<gt> if you hack any interesting fixes or
features into the template.

=item B<-b>

Suppresses printing of the Plaque on the traycard, thus allowing you
to fit even more items on the traycard. Use if you have more than
approximately 192 items. This will allow you to print up to 256 items.

=item B<-h>

print out the usage message

=item B<-w>

Enables word wrapping of the items that print on the traycard. Note
that this is *not* extensively tested and may be buggy! Make sure that
you preview your label before printing it if you use this flag. This
was a frequently requested feature, so I hacked it into the perl side
of cdlabelgen as I don't have the time now to do it properly in
postscript--patches are welcome (hint, hint).

=back

=head1 EXAMPLES

	./cdlabelgen -c "My Filesystem" -s "/usr/local/foo" -e postscript/recycle.epsi > foo.ps

	./cdlabelgen -c "title of cd" -s "subtitle" -i "Item 1%and Item 2%a third item here perhaps" -e postscript/recycle.epsi > bar.ps

	./cdlabelgen -c "Fitz" -s "home directory" -o qux.ps

=head1 AUTHOR

B. W. Fitzpatrick E<lt>F<fitz@red-bean.com>E<gt>

=head1 THANKS

	- Karl Fogel, for general encouragement and that free software vibe
	- Adam Di Carlo, for bug testing, help and making the .deb
	- Greg Gallagher, for bug testing, coding, and tons of suggestions
	- Goran Larsson, for feedback and date fixes
	- Jens Claussen, for the patch to allow arbitrary ISO-Latin1 characters
	- Bernard Quatermass, for contributing several excellent new features


=head1 ERRATA

Perhaps one of the most important features that I wanted in a
CD labelling program was the ability to print Title/Subtitle, and date
information on the endcaps of the CD jewel box to allow me to keep
archive disks in a standard CD rack and find a particular one without
yanking them all out and shuffling through them like a deck of cards.

cdlabelgen was inspired by the need for not only a simple cd labelling
program (there are many available), but by the need for a free
labelling program which could be integrated easily with scheduled CD
archiving routines. I did find a program called cdlabel
http://londo.ncl.ac.uk/~npac/cdlabel/, but that one is designed to work
with CDDB.

I searched the net for a suitable program, but found none, so taking
cues from programs that I found that perform similar tasks (like tape
labellers and DAT labellers), I embarked on this venture. Notable
inspiration came from the incredible audio-tape.ps by Jamie Zawinski
(which is indeed, as Jamie notes, completely out of control). Other
ideas were drawn from casslabel.c, and cdlabel.cc (noted above).

People have already pointed out to me that cdlabelgen could be
extended to perform other functions:

=over

=item * Print out the actual label that goes on the disk (I suppose it
is kinda funny that it is named cdB<label>gen and doesn't really
generate any label per se).

=item * Integrate with cdindex or cddb or something like that.

=back

If you are interesting in doing or contributing to the above features
(or any other features for that matter!), please contact me at the
above address. Please report bugs and submit any patches to the above
address as well--all help is appreciated.

=head1 BUGS

=over

=item * Excessively wide (> 30 chars) traycard items are shrunk into
illegibility.

=item * Word Wrapping is a bit wonky.

=back

=head1 TODO

In order of importance.

=over

=item * Proper word wrapping (on the postscript side).

=item * Add cdindex support

=back

=head1 LAST MODIFIED

$Id: cdlabelgen,v 1.48 1999/06/23 17:48:15 bwf Exp $


=cut