4eaf5c9b1e4199fd0c4cb96e25ea0ea73e552dbb
[xscreensaver] / hacks / bubbles-tools / bubblestofile
1 #!/usr/bin/perl
2 #
3 # $Id: bubblestofile,v 1.1 1996/09/08 01:35:52 jwz Exp $
4 #
5 #----------------------------------------------------------------------------
6 #  Copyright (C) 1995-1996 James Macnicol
7 #
8 #   This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by the
10 # Free Software Foundation; either version 2, or (at your option) any later
11 # version.
12 #
13 #   This program is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY
15 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 # for more details.
17 #-----------------------------------------------------------------------------
18 #
19 # Contact me (J.Macnicol@student.anu.edu.au) if you have problems.
20 #
21 # [ The moral of this story is use a version of rm which safely backs up
22 # files when you delete them in case you do something stupid like
23 # "rm * xpm" which trashed all the scripts in this directory so I had
24 # to write them again.  Grrrrrr..... ]
25 #
26 #-----------------------------------------------------------------------------
27 #
28 # This script takes a set of XPM files (from povbubbles, for example)
29 # whose names are listed in file with extension .names (of same format
30 # as output by povbubbles) and puts them together into a file which can
31 # loaded with the -file option or place in a directory suitable for
32 # use with the -directory option to bubbles.  Note that neither of these
33 # options are available if you have just compiled bubbles as provided.
34 # You must edit bubbles.h to enable these.  Files generated by this script
35 # have by default the extension ".bub".
36 #
37 # To use it, provide as an argument the base-name of the .names file,
38 # i.e. if you ran povbubbles on the file foo.pov by typing "povbubbles foo"
39 # then this created a file "foo.names" so you can now make the loadable file
40 # "foo.bub" by typing "bubblestofile foo".
41 #
42
43 sub die_help {
44     print STDERR "Usage: $0 [-help] base-name\n";
45     print STDERR "  -help\n";
46     print STDERR "    gives this message.\n";
47     print STDERR "  base-name is the name of the file used to generate\n";
48     print STDERR "    the XPM files, e.g. if you invoked povbubbles with\n";
49     print STDERR "              \"povbubbles foo\"\n";
50     print STDERR "    then you should invoke $0 with\n";
51     die("              \"$0 foo\"\n");
52 }
53
54 sub die_usage {
55     die "Usage: $0 [-help] base-name\n";
56 }
57
58 $infile = undef;
59
60 # Process command line arguments
61 while ($op = shift) {
62     if ($op eq "-help") {
63         &die_help;
64     } else {
65         $infile = $op;
66         # Ignore further arguments
67         break;
68     }
69 }
70 if ($infile eq undef) {
71     &die_usage;
72 }
73
74 $namesfile = $infile . ".names";
75 $outfile = $infile . ".bub";
76
77 if (! -f $namesfile) {
78     die("File list $namesfile doesn't exist\n");
79 }
80
81 if (-f $outfile) {
82     print "Backing up $outfile\n";
83     system("mv -f $outfile $outfile.bak");
84 }
85
86 open(OUT, ">$outfile") || die("Couldn't open $outfile\n");
87 open(NAMES, $namesfile) || die ("Couldn't open $namesfile\n");
88 $numbubbles = 0;
89 while (<NAMES>) {
90     if (/\s*(\S+)\:(\S+)\s*/) {
91         $filename = $1;
92         $xpmname = $2;
93         open(CAT, $filename) || die("Couldn't open file $filename listed in\
94 $namesfile\n");
95         while (<CAT>) {
96             print OUT;
97         }
98         close(CAT);
99     } else {
100         print STDERR "Can't understand the line \"$_\"\n";
101         print STDERR "  in $namesfile.  Ignoring...\n";
102     }
103 }
104 close(NAMES);
105 close(OUT);
106
107