3ad718b8b65bffe2c8415d6f4c0a2d4fefeeb8e5
[xscreensaver] / hacks / bubbles-tools / bubblestodefault
1 #!/usr/bin/perl
2 #
3 # $Id: bubblestodefault,v 1.1 1996/09/08 01:35:51 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 # be used in place of the source file bubbles_default.c which comes with
32 # bubbles/xscreensaver.
33 #
34 # To use it, provide as an argument the base-name of the .names file,
35 # i.e. if you ran povbubbles on the file foo.pov by typing "povbubbles foo"
36 # then this created a file "foo.names" so you can now make a new
37 # bubbles_default.c by typing "bubblestodefault foo".
38 #
39
40 sub die_help {
41     print STDERR "Usage: $0 [-help] base-name\n";
42     print STDERR "  -help gives this message.\n";
43     print STDERR "  base-name is the name of the file used to generate\n";
44     print STDERR "  the XPM files, e.g. if you invoked povbubbles with\n";
45     print STDERR "            \"povbubbles foo\"\n";
46     print STDERR "  then you should invoke $0 with\n";
47     die("            \"$0 foo\"\n");
48 }
49
50 sub die_usage {
51     die "Usage: $0 [-help] base-name\n";
52 }
53
54 $infile = undef;
55
56 # Process command line arguments
57 while ($op = shift) {
58     if ($op eq "-help") {
59         &die_help;
60     } else {
61         $infile = $op;
62         # Ignore further arguments
63         break;
64     }
65 }
66 if ($infile eq undef) {
67     &die_usage;
68 }
69
70 $namesfile = $infile . ".names";
71
72 if (! -f $namesfile) {
73     die("File list $namesfile doesn't exist\n");
74 }
75
76 if (-f "bubbles_default.c") {
77     print "Backing up bubbles_default.c...\n";
78     system("mv -f bubbles_default.c bubbles_default.c.bak");
79 }
80
81 open(OUT, ">bubbles_default.c") || die("Couldn't open bubbles_default.c\n");
82 print OUT "#include <stdio.h>\n";
83 print OUT "#include \"bubbles.h\"\n";
84 print OUT "\n";
85 print OUT "#ifndef NO_DEFAULT_BUBBLE\n";
86 print OUT "\n";
87
88 open(NAMES, $namesfile) || die ("Couldn't open $namesfile\n");
89 $numbubbles = 0;
90 while (<NAMES>) {
91     if (/\s*(\S+)\:(\S+)\s*/) {
92         $filename = $1;
93         $xpmname = $2;
94         $xpmlist = $xpmlist . $xpmname . ", ";
95         open(CAT, $filename) || die("Couldn't open file $filename listed in\
96 $namesfile\n");
97         while (<CAT>) {
98             print OUT;
99         }
100         close(CAT);
101         $numbubbles++;
102     } else {
103         print STDERR "Can't understand the line \"$_\"\n";
104         print STDERR "  in $namesfile.  Ignoring...\n";
105     }
106 }
107 print OUT "char **default_bubbles[] = {$xpmlist";
108 print OUT "(char **)0};\n";
109 print OUT "\n";
110 print OUT "int num_default_bubbles = $numbubbles;\n";
111 print OUT "\n";
112 print OUT "#endif /* NO_DEFAULT_BUBBLE */\n";
113
114 close(NAMES);
115 close(OUT);