2 # Copyright (C) 2014, all contributors (git clone git://80x24.org/public-inbox)
3 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
6 my $usage = "public-inbox-init NAME GIT_DIR HTTP_URL ADDRESS [ADDRESS..]";
7 use PublicInbox::Config;
8 use File::Temp qw/tempfile/;
9 use File::Basename qw/dirname/;
10 use File::Path qw/mkpath/;
11 use File::Path::Expand qw/expand_filename/;
13 sub x { system(@_) and die join(' ', @_). " failed: $?\n" }
14 sub usage { print STDERR "Usage: $usage\n"; exit 1 }
16 my $name = shift @ARGV or usage();
17 my $git_dir = shift @ARGV or usage();
18 my $http_url = shift @ARGV or usage();
19 my (@address) = @ARGV;
23 my $pi_config = PublicInbox::Config->default_file;
24 my $dir = dirname($pi_config);
25 mkpath($dir); # will croak on fatal errors
26 my ($fh, $filename) = tempfile('pi-init-XXXXXXXX', DIR => $dir);
28 open(my $oh, '<', $pi_config) or die "unable to read $pi_config: $!\n";
34 print $fh $old or die "failed to write: $!\n";
35 close $oh or die "failed to close $pi_config: $!\n";
37 # yes, this conflict checking is racy if multiple instances of this
38 # script are run by the same $PI_DIR
39 my $cfg = PublicInbox::Config->new;
41 foreach my $addr (@address) {
42 my $found = $cfg->lookup($addr);
44 if ($found->{listname} ne $name) {
46 "`$addr' already defined for ",
47 "`$found->{listname}',\n",
48 "does not match intend `$name'\n";
58 close $fh or die "failed to close $filename: $!\n";
60 my $pfx = "publicinbox.$name";
61 my @x = (qw/git config/, "--file=$filename");
62 $git_dir = expand_filename($git_dir);
63 x(qw(git init -q --bare), $git_dir);
64 foreach my $addr (@address) {
65 next if $seen{lc($addr)};
66 x(@x, "--add", "$pfx.address", $addr);
68 x(@x, "$pfx.url", $http_url);
69 x(@x, "$pfx.mainrepo", $git_dir);
71 rename $filename, $pi_config or
72 die "failed to rename `$filename' to `$pi_config': $!\n";