]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-init
update copyrights for 2018
[public-inbox.git] / script / public-inbox-init
1 #!/usr/bin/perl -w
2 # Copyright (C) 2014-2018 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 #
5 # Initializes a public-inbox, basically a wrapper for git-init(1)
6 use strict;
7 use warnings;
8 my $usage = "public-inbox-init NAME GIT_DIR HTTP_URL ADDRESS [ADDRESS..]";
9 use PublicInbox::Config;
10 use File::Temp qw/tempfile/;
11 use File::Basename qw/dirname/;
12 use File::Path qw/mkpath/;
13 use Cwd qw/abs_path/;
14
15 sub x { system(@_) and die join(' ', @_). " failed: $?\n" }
16 sub usage { print STDERR "Usage: $usage\n"; exit 1 }
17
18 my $name = shift @ARGV or usage();
19 my $git_dir = shift @ARGV or usage();
20 my $http_url = shift @ARGV or usage();
21 my (@address) = @ARGV;
22 @address or usage();
23 my %seen;
24
25 my $pi_config = PublicInbox::Config->default_file;
26 my $dir = dirname($pi_config);
27 mkpath($dir); # will croak on fatal errors
28 my ($fh, $filename) = tempfile('pi-init-XXXXXXXX', DIR => $dir);
29 if (-e $pi_config) {
30         open(my $oh, '<', $pi_config) or die "unable to read $pi_config: $!\n";
31         my @st = stat($oh);
32         my $perm = $st[2];
33         defined $perm or die "(f)stat failed on $pi_config: $!\n";
34         chmod($perm & 07777, $fh) or
35                 die "(f)chmod failed on future $pi_config: $!\n";
36         my $old;
37         {
38                 local $/;
39                 $old = <$oh>;
40         }
41         print $fh $old or die "failed to write: $!\n";
42         close $oh or die "failed to close $pi_config: $!\n";
43
44         # yes, this conflict checking is racy if multiple instances of this
45         # script are run by the same $PI_DIR
46         my $cfg = PublicInbox::Config->new;
47         my $conflict;
48         foreach my $addr (@address) {
49                 my $found = $cfg->lookup($addr);
50                 if ($found) {
51                         if ($found->{name} ne $name) {
52                                 print STDERR
53                                         "`$addr' already defined for ",
54                                         "`$found->{name}',\n",
55                                         "does not match intend `$name'\n";
56                                 $conflict = 1;
57                         } else {
58                                 $seen{lc($addr)} = 1;
59                         }
60                 }
61         }
62
63         exit(1) if $conflict;
64 }
65 close $fh or die "failed to close $filename: $!\n";
66
67 my $pfx = "publicinbox.$name";
68 my @x = (qw/git config/, "--file=$filename");
69 $git_dir = abs_path($git_dir);
70 x(qw(git init -q --bare), $git_dir);
71
72 # set a reasonable default:
73 x(qw/git config/, "--file=$git_dir/config", 'repack.writeBitmaps', 'true');
74
75 foreach my $addr (@address) {
76         next if $seen{lc($addr)};
77         x(@x, "--add", "$pfx.address", $addr);
78 }
79 x(@x, "$pfx.url", $http_url);
80 x(@x, "$pfx.mainrepo", $git_dir);
81
82 rename $filename, $pi_config or
83         die "failed to rename `$filename' to `$pi_config': $!\n";