+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 $ex = $self->canonicalize_excludes($opt->{exclude});
+ $self->externals_each(\&prep_ext, $lxs, $ex);
+ $opt->{remote} //= !($lxs->locals - $opt->{'local'});
+ $lxs->{locals} = \@ilocals if !$opt->{'local'};
+ $lxs->{remotes} = \@iremotes if !$opt->{remote};
+ }
+ }
+ ($lxs->locals || $lxs->remotes) ? ($self->{lxs} = $lxs) :
+ die("no local or remote inboxes to search\n");
+}
+
+# the main "lei q SEARCH_TERMS" method
+sub lei_q {
+ my ($self, @argv) = @_;
+ PublicInbox::Config->json; # preload before forking
+ my $lxs = lxs_prepare($self) or return;
+ $self->ale->refresh_externals($lxs, $self);
+ my $opt = $self->{opt};
+ my %mset_opt;