aboutsummaryrefslogtreecommitdiff
path: root/bootstrap
blob: d9a130e6412740d817d5e5c14a035bb83a03eabb (plain)
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
eval '(exit $?0)' && eval 'exec perl -wS "$0" "$@"'
  & eval 'exec perl -wS "$0" $argv:q'
    if 0;

use strict;
use warnings;
use File::Basename;
use File::Path qw(make_path);
use Getopt::Long qw(:config gnu_getopt no_ignore_case);
use Sys::Hostname;
use POSIX qw(strftime);
use Pod::Usage;
use Pod::Man;
use Cwd 'abs_path';

=head1 NAME

bootstrap - creates a framework for writing a Varnish module

=head1 SYNOPSIS

B<bootstrap>
[B<-f>]
[B<-C> I<DIR>]
[B<--license=>I<FILE>]
[B<--description=>I<TEXT>]    
[B<--directory=>I<DIR>]
[B<--email=>I<EMAIL>]
[B<--force>]
[B<--foreign>]
[B<--git>]
[B<--gnits>]
[B<--real-name=>I<NAME>]
[B<--url=>I<URL>]    
[B<--version=>I<V>]
I<MODULE>

=head1 DESCRIPTION

The B<bootstrap> tool populates the current working directory (or the
directory specified with the B<-C> option) with the files necessary
for building Varnish module B<vmod_I<MODULE>> using GNU autotools.
At the end of the run, it invokes B<autoreconf>(1) to actually bootstrap
the suite.    
    
=head1 OPTIONS

=over 4

=item B<-f>, B<--force>

Force overwriting existing files.

=item B<-C>, B<--directory=>I<DIR>

Change to I<DIR> prior to startup.

=item B<--git>

Set Automake strictness to B<gnu>.    
    
=item B<--gnits>

Set Automake strictness to B<gnits>.    

=item B<--foreign>

Set Automake strictness to B<foreign>. This is the default.

=item B<--real-name=>I<NAME>

Sets your real name.

=item B<--email=>I<EMAIL>

Sets your email address.

=item B<--description=>I<TEXT>

Sets a one-line vmod description for use in the vcc file header.

=item B<--version=>I<V>

Sets the vmod version. Default is B<0.1>.

=item B<--url=>I<URL>

Sets the ULR of the vmod web page.    

=item B<--license=>I<FILE>

Reads the text of the license from I<FILE>. Unless I<FILE> begins with B</> or
B<./>, the file will first be looked in the B<license> subdirectory and then
in the current working directory.

The text read froom I<FILE> will be expanded (see B<VARIABLE EXPANSION>)
and reproduced in the initial header of each created file.
    
=item B<-?>

Display short help.

=item B<--help>

Display detailed help.    

=item B<--usage>

Display short usage summary.    
    
=back    
    
=head1 SEE ALSO

The code.    
    
=cut    

my $program = basename($0);
my $progdescr = q{creates a framework for writing a Varnish module};
my $basedir = abs_path(dirname($0));
my $tmpldir = $basedir . '/tmpl';
my $force;
my $git = 0;
my %dict = ( STRICTNESS => 'foreign',
	     VERSION => '0.1',
	     DATE => strftime('%Y-%m-%d', localtime),
	     YEAR => strftime('%Y', localtime),
	     DESCR => 'No description yet',
	     URL => q{<please enter the project's URL here>},
    );

sub error {
    print STDERR "$program: @_\n";
}
    
sub usage {
    pod2usage(-exitstatus => 0, -verbose => 0);
}

sub create_dir {
    my $dir = shift;
    make_path($dir, {error => \my $err});
    if (@$err) {
	for my $diag (@$err) {
	    my ($file, $message) = %$diag;
	    if ($file eq '') {
		print STDERR $message . ", while creating \"$file\"\n";
	    } else {
		print STDERR $message . ", while creating \"$file\"\n";
	    }
	}
	exit(1);
    }
}

sub expvar {
    my ($varname, $args) = @_;
    my $exp;
    $args = {} unless $args;
    if (exists($dict{$varname})) {
	if (ref($dict{$varname}) eq 'SCALAR') {
	    $exp = ${$dict{$varname}};
	} elsif (ref($dict{$varname}) eq 'CODE') {
	    $exp = &{$dict{$varname}}($varname, $args);
	} else {
	    $exp = $dict{$varname};
	}
    } elsif (exists($args->{$varname})) {
	$exp = $args->{$varname};
    } else {
	return "";
    }
    if ($exp ne '' && exists($args->{prefix})) {
	my @lines = split /\n/, $exp;
	if ($args->{suffix}) {
	    my $leng = 0;
	    foreach my $l (@lines) {
		$leng = length($l) if length($l) > $leng;
	    }
	    $exp = join("\n",
			map {
			    $args->{prefix} . $_ . ' ' x ($leng - length($_)) . $args->{suffix}
			} @lines);
	} else {
	    $exp = join("\n", map { $args->{prefix} . $_ } @lines);
	}
    }
    return $exp;
}   

sub make_file {
    my ($name, $args) = @_;
    my $template = $tmpldir . '/' . $args->{template};
    open(my $ofd, '>', $name)
	or die "can't open $name for writing: $!";
    open(my $ifd, '<', $template)
	or die "can't open $template: $!";
    while (<$ifd>) {
	s/^(.*)%\[([\w_]+)\](.*)$
         /expvar($2, { %$args, prefix => $1, suffix => $3 })
         /ex;
	s/\%\{([\w_]+)\}/expvar($1, $args)/gex;
	print $ofd $_;
    }
    close $ifd;
    close $ofd;
}

sub mkcl {
    my ($name) = @_;
    open(my $fd, '>', $name)
	or die "can't open $name for writing: $!";
    print $fd <<'EOT'
This file is a placeholder. It will be replaced with the actual ChangeLog
by make dist.  Run make ChangeLog if you wish to create it earlier.
EOT
;
    close $fd;
}

sub mkCOPYING {
    my ($name, $args) = @_;
    my $template = $basedir . '/COPYING';
    return if -d '.git';
    open(my $ofd, '>', $name)
	or die "can't open $name for writing: $!";
    open(my $ifd, '<', $template)
	or die "can't open $template: $!";
    while (<$ifd>) {
	s/\%\{([\w_]+)\}/expvar($1, $args)/gex;
	print $ofd $_;
    }
    close $ifd;
    close $ofd;
}

sub strictness_gnu {
    return $dict{STRICTNESS} eq 'gnu' || $dict{STRICTNESS} eq 'gnits';
}

sub strictness_gnits {
    return $dict{STRICTNESS} eq 'gnits';
}

my @files = (
    { name => 'Makefile.am', template => 'Makefile.am' },
    { name => 'configure.ac', template => 'configure.ac' },
    { name => '.gitignore', template => 'dot.gitignore',
      cond => \$git },
    { name => 'src/Makefile.am', template => 'src/Makefile.am' },
    { name => 'src/.gitignore', template => 'src/dot.gitignore',
      cond => \$git },
    { name => 'src/vmod_%{MODULE}.vcc', template => 'src/vmod.vcc' },
    { name => 'src/vmod_%{MODULE}.c', template => 'src/vmod.c' },
    { name => 'tests/testsuite.at', template => 'tests/testsuite.at' },
    { name => 'tests/atlocal.in', template => 'tests/atlocal.in' },
    { name => 'tests/.gitignore', template => 'tests/dot.gitignore',
      cond => \$git },
    { name => 'tests/Makefile.am', template => 'tests/Makefile.am' },
    { name => 'COPYING', func => \&mkCOPYING },
    { name => 'AUTHORS', template => 'AUTHORS',
      cond => \&strictness_gnu },
    { name => 'NEWS', template => 'NEWS',
      cond => \&strictness_gnu },
    { name => 'README', template => 'README',
      cond => \&strictness_gnu },
    { name => 'THANKS', template => 'THANKS',
      cond => \&strictness_gnits },
    { name => 'ChangeLog', func => \&mkcl,
      cond => \&strictness_gnu, gitignore => 1 },
    { name => 'm4', func => \&create_dir }
);


my $wd;
my $license_file;
GetOptions(
    'h|?' => sub {
	pod2usage(-message => "$program: $progdescr",
		  -exitstatus => 0)
    },
    'help' => sub {
	pod2usage(-exitstatus => 0,
		  -verbose => 2);
    },
    'usage' => sub {
	pod2usage(-exitstatus => 0,
                  -verbose => 0);
    },
    
    'force|f' => \$force,
    'firectory|C=s' => \$wd,
    'git' => \$git,
    'gnu' => sub { $dict{STRICTNESS} = 'gnu' },
    'gnits' => sub { $dict{STRICTNESS} = 'gnits' },
    'foreign' => sub { $dict{STRICTNESS} = 'foreign' },
    'real-name=s' => \$dict{REALNAME},
    'email=s' => \$dict{EMAIL},
    'description=s' => \$dict{DESCR},
    'version=s' => \$dict{VERSION},
    'url=s' => \$dict{URL},
    'license=s' => \$license_file
    ) or exit(1);

$dict{MODULE} = shift @ARGV or usage;

usage if @ARGV;

$dict{EMAIL} = $ENV{USER} . '@' . hostname()
    unless defined($dict{EMAIL});
unless (defined($dict{REALNAME})) {
    $dict{REALNAME} = (getpwnam($ENV{USER}))[6];
    $dict{REALNAME} =~ s/,.*//;
    $dict{REALNAME} = "User $ENV{USER}" if $dict{REALNAME} eq '';
}

if ($license_file) {
    unless ($license_file =~ m{^(\.{1,2})?/}) {
	my $n = "$basedir/license/$license_file";
	$license_file = $n if -f $n;
    }

    local $/ = undef;
    open(my $fd, '<', $license_file)
	or die "cannot open $license_file: $!";
    $dict{LICENSE} = <$fd>;
    close($fd);
    $dict{LICENSE} =~ s/\%\{([\w_]+)\}/expvar($1)/gex;
}

if ($wd) {
    create_dir($wd) unless -d $wd;
    system("cp -r $basedir $wd");
    chdir $wd or die "Can't change to $wd: $!";
}

$git = 1 if (!$git && -d '.git');

my $skip = 0;
my @created;
foreach my $f (@files) {
    my $func;
    my $name;

    if (exists($f->{cond})) {
	my $cond;
	if (ref($f->{cond}) eq 'CODE') {
	    $cond = &{$f->{cond}}($f);
	} elsif (ref($f->{cond}) eq 'SCALAR') {
	    $cond = ${$f->{cond}};
	} else {
	    die "$f->{name}: invalid condition";
	}
	next unless $cond;
    }
    
    $name = $f->{name};
    $name =~ s/\%\{([\w_]+)\}/expvar($1)/gex;

    if (-e $name) {
	unless ($force) {
	    error "$name already exists, skipping";
	    $skip++;
	}
    }
    
    my $dir = dirname($name);
    create_dir($dir) unless -d $dir;
    
    if (exists($f->{func})) {
	$func = $f->{func};
    } elsif (exists($f->{template})) {
	$func = \&make_file;
    } else {
	die "neither func nor template set for $f->{name}";
    }
    &{$func}($name, $f);

    push @created, $name if -f $name && !$f->{gitignore};
}

if ($skip) {
    error "use '$program --force' to overwrite existing files";
}

system('autoreconf -f -i -s');

if ($git) {
    system("git init") unless -d '.git';
    system("git submodule add git://git.gnu.org.ua/acvmod.git");
    system('git', 'add', @created);
}

# Local variables:
# mode: perl
# End:

Return to:

Send suggestions and report system problems to the System administrator.