]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiInit.pm
imap+nntp: share COMPRESS implementation
[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
8 sub lei_init {
9         my ($self, $dir) = @_;
10         my $cfg = $self->_lei_cfg(1);
11         my $cur = $cfg->{'leistore.dir'};
12         $dir //= $self->store_path;
13         $dir = $self->rel2abs($dir);
14         my @cur = stat($cur) if defined($cur);
15         $cur = $self->canonpath_harder($cur // $dir);
16         my @dir = stat($dir);
17         my $exists = "# leistore.dir=$cur already initialized" if @dir;
18         if (@cur) {
19                 if ($cur eq $dir) {
20                         $self->_lei_store(1)->done;
21                         return $self->qerr($exists);
22                 }
23
24                 # some folks like symlinks and bind mounts :P
25                 if (@dir && "@cur[1,0]" eq "@dir[1,0]") {
26                         $self->_config('leistore.dir', $dir);
27                         $self->_lei_store(1)->done;
28                         return $self->qerr("$exists (as $cur)");
29                 }
30                 return $self->fail(<<"");
31 E: leistore.dir=$cur already initialized and it is not $dir
32
33         }
34         $self->_config('leistore.dir', $dir);
35         $self->_lei_store(1)->done;
36         $exists //= "# leistore.dir=$dir newly initialized";
37         $self->qerr($exists);
38 }
39
40 1;