]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiInit.pm
lei init: split out into separate file
[public-inbox.git] / lib / PublicInbox / LeiInit.pm
1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # for the "lei init" command, not sure if it's even needed...
5 package PublicInbox::LeiInit;
6 use v5.10.1;
7 use File::Spec;
8
9 sub lei_init {
10         my ($self, $dir) = @_;
11         my $cfg = $self->_lei_cfg(1);
12         my $cur = $cfg->{'leistore.dir'};
13         $dir //= $self->store_path;
14         $dir = $self->rel2abs($dir);
15         my @cur = stat($cur) if defined($cur);
16         $cur = File::Spec->canonpath($cur // $dir);
17         my @dir = stat($dir);
18         my $exists = "# leistore.dir=$cur already initialized" if @dir;
19         if (@cur) {
20                 if ($cur eq $dir) {
21                         $self->_lei_store(1)->done;
22                         return $self->qerr($exists);
23                 }
24
25                 # some folks like symlinks and bind mounts :P
26                 if (@dir && "@cur[1,0]" eq "@dir[1,0]") {
27                         $self->lei_config('leistore.dir', $dir);
28                         $self->_lei_store(1)->done;
29                         return $self->qerr("$exists (as $cur)");
30                 }
31                 return $self->fail(<<"");
32 E: leistore.dir=$cur already initialized and it is not $dir
33
34         }
35         $self->lei_config('leistore.dir', $dir);
36         $self->_lei_store(1)->done;
37         $exists //= "# leistore.dir=$dir newly initialized";
38         $self->qerr($exists);
39 }
40
41 1;