]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-init
init: s/GIT_DIR/REPO_DIR/ in usage
[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 File::Temp qw/tempfile/;
12 use File::Basename qw/dirname/;
13 use File::Path qw/mkpath/;
14 use Cwd qw/abs_path/;
15
16 sub x { system(@_) and die join(' ', @_). " failed: $?\n" }
17 sub usage { print STDERR "Usage: $usage\n"; exit 1 }
18 my $version = undef;
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;
25 @address or usage();
26 my %seen;
27
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);
32 if (-e $pi_config) {
33         open(my $oh, '<', $pi_config) or die "unable to read $pi_config: $!\n";
34         my @st = stat($oh);
35         my $perm = $st[2];
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";
39         my $old;
40         {
41                 local $/;
42                 $old = <$oh>;
43         }
44         print $fh $old or die "failed to write: $!\n";
45         close $oh or die "failed to close $pi_config: $!\n";
46
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;
50         my $conflict;
51         foreach my $addr (@address) {
52                 my $found = $cfg->lookup($addr);
53                 if ($found) {
54                         if ($found->{name} ne $name) {
55                                 print STDERR
56                                         "`$addr' already defined for ",
57                                         "`$found->{name}',\n",
58                                         "does not match intend `$name'\n";
59                                 $conflict = 1;
60                         } else {
61                                 $seen{lc($addr)} = 1;
62                         }
63                 }
64         }
65
66         exit(1) if $conflict;
67 }
68 close $fh or die "failed to close $pi_config_tmp: $!\n";
69
70 my $pfx = "publicinbox.$name";
71 my @x = (qw/git config/, "--file=$pi_config_tmp");
72
73 $mainrepo = abs_path($mainrepo);
74 if (-f "$mainrepo/inbox.lock") {
75         if (!defined $version) {
76                 $version = 2;
77         } elsif ($version != 2) {
78                 die "$mainrepo is a -V2 repo, -V$version specified\n"
79         }
80 } elsif (-d "$mainrepo/objects") {
81         if (!defined $version) {
82                 $version = 1;
83         } elsif ($version != 1) {
84                 die "$mainrepo is a -V1 repo, -V$version specified\n"
85         }
86 }
87
88 $version = 1 unless defined $version;
89
90 if ($version >= 2) {
91         require PublicInbox::V2Writable;
92         require PublicInbox::Inbox;
93         my $ibx = {
94                 mainrepo => $mainrepo,
95                 name => $name,
96                 version => $version,
97                 -primary_address => $address[0],
98         };
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);
103
104         # set a reasonable default:
105         x(qw/git config/, "--file=$mainrepo/config",
106                 'repack.writeBitmaps', 'true');
107 } else {
108         die "Unsupported -V/--version: $version\n";
109 }
110
111 foreach my $addr (@address) {
112         next if $seen{lc($addr)};
113         x(@x, "--add", "$pfx.address", $addr);
114 }
115 x(@x, "$pfx.url", $http_url);
116 x(@x, "$pfx.mainrepo", $mainrepo);
117
118 rename $pi_config_tmp, $pi_config or
119         die "failed to rename `$pi_config_tmp' to `$pi_config': $!\n";