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 REPO_DIR HTTP_URL ADDRESS [ADDRESS..]";
9 use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
10 use PublicInbox::Config;
11 use PublicInbox::Inbox;
12 use File::Temp qw/tempfile/;
13 use File::Basename qw/dirname/;
14 use File::Path qw/mkpath/;
17 sub x { system(@_) and die join(' ', @_). " failed: $?\n" }
18 sub usage { print STDERR "Usage: $usage\n"; exit 1 }
20 my $indexlevel = undef;
22 my %opts = ( 'V|version=i' => \$version,
23 'L|indexlevel=s' => \$indexlevel,
26 GetOptions(%opts) or usage();
27 my $name = shift @ARGV or usage();
28 my $mainrepo = shift @ARGV or usage();
29 my $http_url = shift @ARGV or usage();
30 my (@address) = @ARGV;
34 my $pi_config = PublicInbox::Config->default_file;
35 my $dir = dirname($pi_config);
36 mkpath($dir); # will croak on fatal errors
37 my ($fh, $pi_config_tmp) = tempfile('pi-init-XXXXXXXX', DIR => $dir);
39 open(my $oh, '<', $pi_config) or die "unable to read $pi_config: $!\n";
42 defined $perm or die "(f)stat failed on $pi_config: $!\n";
43 chmod($perm & 07777, $fh) or
44 die "(f)chmod failed on future $pi_config: $!\n";
50 print $fh $old or die "failed to write: $!\n";
51 close $oh or die "failed to close $pi_config: $!\n";
53 # yes, this conflict checking is racy if multiple instances of this
54 # script are run by the same $PI_DIR
55 my $cfg = PublicInbox::Config->new;
57 foreach my $addr (@address) {
58 my $found = $cfg->lookup($addr);
60 if ($found->{name} ne $name) {
62 "`$addr' already defined for ",
63 "`$found->{name}',\n",
64 "does not match intend `$name'\n";
74 my $ibx = $cfg->lookup_name($name);
76 if (!defined($indexlevel) && $ibx->{indexlevel}) {
77 $indexlevel = $ibx->{indexlevel};
81 close $fh or die "failed to close $pi_config_tmp: $!\n";
83 my $pfx = "publicinbox.$name";
84 my @x = (qw/git config/, "--file=$pi_config_tmp");
86 $mainrepo = abs_path($mainrepo);
87 if (-f "$mainrepo/inbox.lock") {
88 if (!defined $version) {
90 } elsif ($version != 2) {
91 die "$mainrepo is a -V2 repo, -V$version specified\n"
93 } elsif (-d "$mainrepo/objects") {
94 if (!defined $version) {
96 } elsif ($version != 1) {
97 die "$mainrepo is a -V1 repo, -V$version specified\n"
101 $version = 1 unless defined $version;
103 if ($version == 1 && defined $skip) {
104 die "--skip is only supported for -V2 repos\n";
107 my $ibx = PublicInbox::Inbox->new({
108 mainrepo => $mainrepo,
111 -primary_address => $address[0],
112 indexlevel => $indexlevel,
116 require PublicInbox::V2Writable;
117 PublicInbox::V2Writable->new($ibx, 1)->init_inbox(0, $skip);
118 } elsif ($version == 1) {
119 require PublicInbox::V1Writable;
120 PublicInbox::V1Writable->new($ibx, 1)->init_inbox(0, $skip);
122 die "Unsupported -V/--version: $version\n";
125 foreach my $addr (@address) {
126 next if $seen{lc($addr)};
127 x(@x, "--add", "$pfx.address", $addr);
129 x(@x, "$pfx.url", $http_url);
130 x(@x, "$pfx.mainrepo", $mainrepo);
132 if (defined($indexlevel)) {
133 x(@x, "$pfx.indexlevel", $indexlevel);
136 rename $pi_config_tmp, $pi_config or
137 die "failed to rename `$pi_config_tmp' to `$pi_config': $!\n";