]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-init
lazy load Xapian and make it optional for v2
[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 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/;
15 use Cwd qw/abs_path/;
16
17 sub x { system(@_) and die join(' ', @_). " failed: $?\n" }
18 sub usage { print STDERR "Usage: $usage\n"; exit 1 }
19 my $version = undef;
20 my $indexlevel = undef;
21 my $skip;
22 my %opts = ( 'V|version=i' => \$version,
23              'L|indexlevel=s' => \$indexlevel,
24              'S|skip=i' => \$skip,
25 );
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;
31 @address or usage();
32 my %seen;
33
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);
38 if (-e $pi_config) {
39         open(my $oh, '<', $pi_config) or die "unable to read $pi_config: $!\n";
40         my @st = stat($oh);
41         my $perm = $st[2];
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";
45         my $old;
46         {
47                 local $/;
48                 $old = <$oh>;
49         }
50         print $fh $old or die "failed to write: $!\n";
51         close $oh or die "failed to close $pi_config: $!\n";
52
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;
56         my $conflict;
57         foreach my $addr (@address) {
58                 my $found = $cfg->lookup($addr);
59                 if ($found) {
60                         if ($found->{name} ne $name) {
61                                 print STDERR
62                                         "`$addr' already defined for ",
63                                         "`$found->{name}',\n",
64                                         "does not match intend `$name'\n";
65                                 $conflict = 1;
66                         } else {
67                                 $seen{lc($addr)} = 1;
68                         }
69                 }
70         }
71
72         exit(1) if $conflict;
73
74         my $ibx = $cfg->lookup_name($name);
75         if ($ibx) {
76                 if (!defined($indexlevel) && $ibx->{indexlevel}) {
77                         $indexlevel = $ibx->{indexlevel};
78                 }
79         }
80 }
81 close $fh or die "failed to close $pi_config_tmp: $!\n";
82
83 my $pfx = "publicinbox.$name";
84 my @x = (qw/git config/, "--file=$pi_config_tmp");
85
86 $mainrepo = abs_path($mainrepo);
87 if (-f "$mainrepo/inbox.lock") {
88         if (!defined $version) {
89                 $version = 2;
90         } elsif ($version != 2) {
91                 die "$mainrepo is a -V2 repo, -V$version specified\n"
92         }
93 } elsif (-d "$mainrepo/objects") {
94         if (!defined $version) {
95                 $version = 1;
96         } elsif ($version != 1) {
97                 die "$mainrepo is a -V1 repo, -V$version specified\n"
98         }
99 }
100
101 $version = 1 unless defined $version;
102
103 if ($version == 1 && defined $skip) {
104         die "--skip is only supported for -V2 repos\n";
105 }
106
107 my $ibx = PublicInbox::Inbox->new({
108         mainrepo => $mainrepo,
109         name => $name,
110         version => $version,
111         -primary_address => $address[0],
112         indexlevel => $indexlevel,
113 });
114
115 if ($version >= 2) {
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);
121 } else {
122         die "Unsupported -V/--version: $version\n";
123 }
124
125 foreach my $addr (@address) {
126         next if $seen{lc($addr)};
127         x(@x, "--add", "$pfx.address", $addr);
128 }
129 x(@x, "$pfx.url", $http_url);
130 x(@x, "$pfx.mainrepo", $mainrepo);
131
132 if (defined($indexlevel)) {
133         x(@x, "$pfx.indexlevel", $indexlevel);
134 }
135
136 rename $pi_config_tmp, $pi_config or
137         die "failed to rename `$pi_config_tmp' to `$pi_config': $!\n";