2 # Copyright (C) 2014-2015 all contributors <meta@public-inbox.org>
3 # License: AGPLv3 or later (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 PublicInbox::Config;
10 use File::Temp qw/tempfile/;
11 use File::Basename qw/dirname/;
12 use File::Path qw/mkpath/;
15 sub x { system(@_) and die join(' ', @_). " failed: $?\n" }
16 sub usage { print STDERR "Usage: $usage\n"; exit 1 }
18 my $name = shift @ARGV or usage();
19 my $git_dir = shift @ARGV or usage();
20 my $http_url = shift @ARGV or usage();
21 my (@address) = @ARGV;
25 my $pi_config = PublicInbox::Config->default_file;
26 my $dir = dirname($pi_config);
27 mkpath($dir); # will croak on fatal errors
28 my ($fh, $filename) = tempfile('pi-init-XXXXXXXX', DIR => $dir);
30 open(my $oh, '<', $pi_config) or die "unable to read $pi_config: $!\n";
33 defined $perm or die "(f)stat failed on $pi_config: $!\n";
34 chmod($perm & 07777, $fh) or
35 die "(f)chmod failed on future $pi_config: $!\n";
41 print $fh $old or die "failed to write: $!\n";
42 close $oh or die "failed to close $pi_config: $!\n";
44 # yes, this conflict checking is racy if multiple instances of this
45 # script are run by the same $PI_DIR
46 my $cfg = PublicInbox::Config->new;
48 foreach my $addr (@address) {
49 my $found = $cfg->lookup($addr);
51 if ($found->{name} ne $name) {
53 "`$addr' already defined for ",
54 "`$found->{name}',\n",
55 "does not match intend `$name'\n";
65 close $fh or die "failed to close $filename: $!\n";
67 my $pfx = "publicinbox.$name";
68 my @x = (qw/git config/, "--file=$filename");
69 $git_dir = abs_path($git_dir);
70 x(qw(git init -q --bare), $git_dir);
72 # set a reasonable default:
73 x(qw/git config/, "--file=$git_dir/config", 'repack.writeBitmaps', 'true');
75 foreach my $addr (@address) {
76 next if $seen{lc($addr)};
77 x(@x, "--add", "$pfx.address", $addr);
79 x(@x, "$pfx.url", $http_url);
80 x(@x, "$pfx.mainrepo", $git_dir);
82 rename $filename, $pi_config or
83 die "failed to rename `$filename' to `$pi_config': $!\n";