File: update-bbstyles

package info (click to toggle)
blackbox 0.51.3-13
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,168 kB
  • ctags: 1,307
  • sloc: cpp: 10,982; ansic: 476; sh: 370; makefile: 291; perl: 128
file content (191 lines) | stat: -rw-r--r-- 5,465 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
#!/usr/bin/perl 
#
#  update-bbstyles -- stylemenu update script
#  Copyright (c) 1999 by Brent A. Fulgham <bfulgham@debian.org>
#  
#  This program is free software, under the terms of the
#  GNU General Public License as published by the Free Software
#  Foundation; either version 2 of the license, or (at your
#  option) any later version.
#
#  This routine is designed to be used in conjunction with 
#  Brad Hughes' Blackbox window manager.
#
#  It can be run in three modes:
#
#    --update  [filename] [title]
#
#        This updates the stylemenu in /usr/share/blackbox/styles with
#        the style file "filename" of name "Title".  Note that Title
#        should be quoted.  This routine places the new style in the
#        menu in alphabetical order.
#
#    --remove  [title]
#
#        This removes the style called "title" from the menu file.
#        Note that title should be quoted. 
#
#    --build
#
#        This creates a brand-new style menu in /usr/share/blackbox/styles
#        based on the files present in the /usr/share/blackbox/style
#        directory.  Note that because the blackbox style format does
#        not contain meta-data for the title, this information is lost
#        and the file name is used for the title.  The stylemenu may
#        be hand-edited to provide the desired title.

#
#  Globals
#
$stylesmenu = "/usr/share/blackbox/styles/stylesmenu";
$stylesdir  = "/usr/share/blackbox/styles";
$tempstyle = "/tmp/styles.temp";

#
#  Initialization
#
sub initialize 
{
    if (($ARGV[0] eq "--update") || ($ARGV[0] eq "-u"))
    {
    	$filename = $ARGV[1]  || die "No filename argument given\n";
	$title = $ARGV[2]     || die "No title argument given\n";
    	return 1;
    }
    elsif (($ARGV[0] eq "--build") || ($ARGV[0] eq "-b"))
    {
    	return 2;
    }
    elsif (($ARGV[0] eq "--remove") || ($ARGV[0] eq "-r"))
    {
        $title = $ARGV[1]     || die "No title argument given\n";
        return 3;
    }
    else 	# Bad input
    {
        print "\n";
    	print "Usage:  update-bbstyles [--update] [filename] [title]\n";
	print "        update-bbstyles [--build]\n\n";
	print "        update-bbstyles [--remove] [title]\n";
	print "  -u, --update          Add an entry to the styles menu\n";
	print "  -b, --build           Build the style menu from the\n";
	print "                        files in the style directory.\n";
	print "  -r, --remove          Remove an entry from the menu.\n\n";
	print "Note:  The build option will destroy any title information\n";
	print "in the menu, because the style files do not contain this\n";
	print "information.  The filename will be used as the style title\n";
	print "in this case.\n";
	exit 1;
    }
}

#
#  Update the style menu
#
sub update
{
    print "Updating the style menu\n";
    opendir(STYLESDIR, $stylesdir) || die "Sorry.  No $stylesdir directory.\n";
    if (-f "$stylesdir/$filename")
    {
    	# Update the style menu
	open(INSTYLE, "$stylesmenu") || die "Couldn't open $stylesmenu\n";
	open(TEMPFILE, ">$tempstyle") || die "Sorry.  Can't write to $tempstyle.\n";
	$used = 0;
	while (<INSTYLE>)
	{
            chomp($_);
	    ($foo, $name, $bar) = split(/[()]/, $_);
            if ($filename gt $name || $used == 1)
            {
		print TEMPFILE "$_\n";
            }
	    else
	    {
	        print TEMPFILE "[style]  ($title)   {$stylesdir/$filename}\n";
		print TEMPFILE "$_\n";
		$used = 1;
	    }
	}
    }
    else
    {
        die "$filename is not present in the $stylesdir directory.\n";
    }
    close(TEMPFILE);
    close(INSTYLE);
    unlink($stylesmenu) || die "Can't remove old style menu: $stylesmenu\n";
    system("mv $tempstyle $stylesmenu") && die "Couldn't move $tempstyle to $stylesmenu\n";
    closedir(STYLESDIR);
}


#
#  Build the style menu from the directory entries
#
sub build
{
    print "Building the style menu\n";
    opendir(STYLESDIR, $stylesdir) || die "Sorry.  No $stylesdir directory.\n";
    open(STYLEFILE, ">$tempstyle") || die "Sorry.  Can't write to $tempstyle.\n";

    @names = readdir(STYLESDIR);
    foreach $name (sort(@names))
    {
        if (-f "$stylesdir/$name" && $name ne "stylesmenu" && $name ne "styles.temp")
	{
            print STYLEFILE "[style] ($name)    {$stylesdir/$name}\n"
	}
    }
    close(STYLEFILE);
    close(TEMPFILE);
    if (-f "$stylesmenu")
    {
    	unlink($stylesmenu) || die "Can't remove old style menu: $stylesmenu\n";
    }
    system("mv $tempstyle $stylesmenu") && die "Couldn't move $tempstyle to $stylesmenu\n";
    closedir(STYLESDIR);
} 

#
#  Remove a Style from the style menu
#
sub remove
{
    print "Removing $title from the style menu\n";
    opendir(STYLESDIR, $stylesdir) || die "Sorry.  No $stylesdir directory.\n";
    open(INSTYLE, "$stylesmenu") || die "Couldn't open $stylesmenu\n";
    open(TEMPFILE, ">$tempstyle") || die "Sorry.  Can't write to $tempstyle.\n";
    while (<INSTYLE>)
    {
        chomp($_);
        @fields = split(/[()]/, $_);
        if ($title ne $fields[1])
        {
	    print TEMPFILE "$_\n";
	}
    }
    close(STYLEFILE);
    close(TEMPFILE);
    unlink($stylesmenu) || die "Can't remove old style menu: $stylesmenu\n";
    system("mv $tempstyle $stylesmenu") && die "Couldn't move $tempstyle to $stylesmenu\n";
    closedir(STYLESDIR);
}

#
#  Main routine
#
$mode = initialize();

if ($mode == 1)		# Update
{
    update();
}
elsif ($mode == 2)	# Build
{
    build();
}
else			# Remove
{
    remove();
}