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>
5 # Initializes a public-inbox, basically a wrapper for git-init(1)
8 my $usage = "public-inbox-init NAME GIT_DIR HTTP_URL ADDRESS [ADDRESS..]";
9 use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
10 use PublicInbox::Config;
11 use File::Temp qw/tempfile/;
12 use File::Basename qw/dirname/;
13 use File::Path qw/mkpath/;
16 sub x { system(@_) and die join(' ', @_). " failed: $?\n" }
17 sub usage { print STDERR "Usage: $usage\n"; exit 1 }
19 my %opts = ( 'V|version=i' => \$version );
20 GetOptions(%opts) or usage();
21 my $name = shift @ARGV or usage();
22 my $mainrepo = shift @ARGV or usage();
23 my $http_url = shift @ARGV or usage();
24 my (@address) = @ARGV;
28 my $pi_config = PublicInbox::Config->default_file;
29 my $dir = dirname($pi_config);
30 mkpath($dir); # will croak on fatal errors
31 my ($fh, $pi_config_tmp) = tempfile('pi-init-XXXXXXXX', DIR => $dir);
33 open(my $oh, '<', $pi_config) or die "unable to read $pi_config: $!\n";
36 defined $perm or die "(f)stat failed on $pi_config: $!\n";
37 chmod($perm & 07777, $fh) or
38 die "(f)chmod failed on future $pi_config: $!\n";
44 print $fh $old or die "failed to write: $!\n";
45 close $oh or die "failed to close $pi_config: $!\n";
47 # yes, this conflict checking is racy if multiple instances of this
48 # script are run by the same $PI_DIR
49 my $cfg = PublicInbox::Config->new;
51 foreach my $addr (@address) {
52 my $found = $cfg->lookup($addr);
54 if ($found->{name} ne $name) {
56 "`$addr' already defined for ",
57 "`$found->{name}',\n",
58 "does not match intend `$name'\n";
68 close $fh or die "failed to close $pi_config_tmp: $!\n";
70 my $pfx = "publicinbox.$name";
71 my @x = (qw/git config/, "--file=$pi_config_tmp");
73 $mainrepo = abs_path($mainrepo);
76 require PublicInbox::V2Writable;
77 require PublicInbox::Inbox;
79 mainrepo => $mainrepo,
82 -primary_address => $address[0],
84 $ibx = PublicInbox::Inbox->new($ibx);
85 PublicInbox::V2Writable->new($ibx, 1)->init_inbox(0);
86 } elsif ($version == 1) {
87 x(qw(git init -q --bare), $mainrepo);
89 # set a reasonable default:
90 x(qw/git config/, "--file=$mainrepo/config",
91 'repack.writeBitmaps', 'true');
93 die "Unsupported -V/--version: $version\n";
96 foreach my $addr (@address) {
97 next if $seen{lc($addr)};
98 x(@x, "--add", "$pfx.address", $addr);
100 x(@x, "$pfx.url", $http_url);
101 x(@x, "$pfx.mainrepo", $mainrepo);
103 rename $pi_config_tmp, $pi_config or
104 die "failed to rename `$pi_config_tmp' to `$pi_config': $!\n";