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);
74 if (-f "$mainrepo/inbox.lock") {
75 if (!defined $version) {
77 } elsif ($version != 2) {
78 die "$mainrepo is a -V2 repo, -V$version specified\n"
80 } elsif (-d "$mainrepo/objects") {
81 if (!defined $version) {
83 } elsif ($version != 1) {
84 die "$mainrepo is a -V1 repo, -V$version specified\n"
88 $version = 1 unless defined $version;
91 require PublicInbox::V2Writable;
92 require PublicInbox::Inbox;
94 mainrepo => $mainrepo,
97 -primary_address => $address[0],
99 $ibx = PublicInbox::Inbox->new($ibx);
100 PublicInbox::V2Writable->new($ibx, 1)->init_inbox(0);
101 } elsif ($version == 1) {
102 x(qw(git init -q --bare), $mainrepo);
104 # set a reasonable default:
105 x(qw/git config/, "--file=$mainrepo/config",
106 'repack.writeBitmaps', 'true');
108 die "Unsupported -V/--version: $version\n";
111 foreach my $addr (@address) {
112 next if $seen{lc($addr)};
113 x(@x, "--add", "$pfx.address", $addr);
115 x(@x, "$pfx.url", $http_url);
116 x(@x, "$pfx.mainrepo", $mainrepo);
118 rename $pi_config_tmp, $pi_config or
119 die "failed to rename `$pi_config_tmp' to `$pi_config': $!\n";