+sub lxs_prepare {
+ my ($self) = @_;
+ require PublicInbox::LeiXSearch;
+ # prepare any number of LeiXSearch || LeiSearch || Inbox || URL
+ my $lxs = $self->{lxs} = PublicInbox::LeiXSearch->new;
+ my $opt = $self->{opt};
+ my @only = @{$opt->{only} // []};
+ # --local is enabled by default unless --only is used
+ # we'll allow "--only $LOCATION --local"
+ my $sto = $self->_lei_store(1);
+ $self->{lse} = $sto->search;
+ if ($opt->{'local'} //= scalar(@only) ? 0 : 1) {
+ $lxs->prepare_external($self->{lse});
+ }
+ if (@only) {
+ for my $loc (@only) {
+ my @loc = $self->get_externals($loc) or return;
+ $lxs->prepare_external($_) for @loc;
+ }
+ } else {
+ my (@ilocals, @iremotes);
+ for my $loc (@{$opt->{include} // []}) {
+ my @loc = $self->get_externals($loc) or return;
+ $lxs->prepare_external($_) for @loc;
+ @ilocals = @{$lxs->{locals} // []};
+ @iremotes = @{$lxs->{remotes} // []};
+ }
+ # --external is enabled by default, but allow --no-external
+ if ($opt->{external} //= 1) {
+ my %x;
+ for my $loc (@{$opt->{exclude} // []}) {
+ my @l = $self->get_externals($loc, 1) or return;
+ $x{$_} = 1 for @l;
+ }
+ my $ne = $self->externals_each(\&prep_ext, $lxs, \%x);
+ $opt->{remote} //= !($lxs->locals - $opt->{'local'});
+ if ($opt->{'local'}) {
+ $lxs->{remotes} = \@iremotes if !$opt->{remote};
+ } else {
+ $lxs->{locals} = \@ilocals;
+ }
+ }
+ }
+ ($lxs->locals || $lxs->remotes) ? ($self->{lxs} = $lxs) :
+ $self->fail('no local or remote inboxes to search');
+}