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 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 $indexlevel = undef;
21 my %opts = ( 'V|version=i' => \$version,
22 'L|indexlevel=s' => \$indexlevel,
25 GetOptions(%opts) or usage();
26 my $name = shift @ARGV or usage();
27 my $mainrepo = shift @ARGV or usage();
28 my $http_url = shift @ARGV or usage();
29 my (@address) = @ARGV;
33 my $pi_config = PublicInbox::Config->default_file;
34 my $dir = dirname($pi_config);
35 mkpath($dir); # will croak on fatal errors
36 my ($fh, $pi_config_tmp) = tempfile('pi-init-XXXXXXXX', DIR => $dir);
38 open(my $oh, '<', $pi_config) or die "unable to read $pi_config: $!\n";
41 defined $perm or die "(f)stat failed on $pi_config: $!\n";
42 chmod($perm & 07777, $fh) or
43 die "(f)chmod failed on future $pi_config: $!\n";
49 print $fh $old or die "failed to write: $!\n";
50 close $oh or die "failed to close $pi_config: $!\n";
52 # yes, this conflict checking is racy if multiple instances of this
53 # script are run by the same $PI_DIR
54 my $cfg = PublicInbox::Config->new;
56 foreach my $addr (@address) {
57 my $found = $cfg->lookup($addr);
59 if ($found->{name} ne $name) {
61 "`$addr' already defined for ",
62 "`$found->{name}',\n",
63 "does not match intend `$name'\n";
73 my $ibx = $cfg->lookup_name($name);
75 if (!defined($indexlevel) && $ibx->{indexlevel}) {
76 $indexlevel = $ibx->{indexlevel};
80 close $fh or die "failed to close $pi_config_tmp: $!\n";
82 my $pfx = "publicinbox.$name";
83 my @x = (qw/git config/, "--file=$pi_config_tmp");
85 $mainrepo = abs_path($mainrepo);
86 if (-f "$mainrepo/inbox.lock") {
87 if (!defined $version) {
89 } elsif ($version != 2) {
90 die "$mainrepo is a -V2 repo, -V$version specified\n"
92 } elsif (-d "$mainrepo/objects") {
93 if (!defined $version) {
95 } elsif ($version != 1) {
96 die "$mainrepo is a -V1 repo, -V$version specified\n"
100 $version = 1 unless defined $version;
102 if ($version == 1 && defined $skip) {
103 die "--skip is only supported for -V2 repos\n";
107 require PublicInbox::V2Writable;
108 require PublicInbox::Inbox;
110 mainrepo => $mainrepo,
113 -primary_address => $address[0],
115 $ibx = PublicInbox::Inbox->new($ibx);
116 PublicInbox::V2Writable->new($ibx, 1)->init_inbox(0, $skip);
117 } elsif ($version == 1) {
118 x(qw(git init -q --bare), $mainrepo);
120 # set a reasonable default:
121 x(qw/git config/, "--file=$mainrepo/config",
122 'repack.writeBitmaps', 'true');
124 die "Unsupported -V/--version: $version\n";
127 foreach my $addr (@address) {
128 next if $seen{lc($addr)};
129 x(@x, "--add", "$pfx.address", $addr);
131 x(@x, "$pfx.url", $http_url);
132 x(@x, "$pfx.mainrepo", $mainrepo);
134 if (defined($indexlevel)) {
135 x(@x, "$pfx.indexlevel", $indexlevel);
138 rename $pi_config_tmp, $pi_config or
139 die "failed to rename `$pi_config_tmp' to `$pi_config': $!\n";