+sub fchdir {
+ my ($lei) = @_;
+ my $dh = $lei->{3} // die 'BUG: lei->{3} (CWD) gone';
+ chdir($dh) || $lei->fail("fchdir: $!");
+}
+
+sub wq_eof { # EOF callback for main daemon
+ my ($lei) = @_;
+ my $wq1 = delete $lei->{wq1} // return $lei->fail; # already failed
+ $wq1->wq_wait_old(\&wq_done_wait, $lei);
+}
+
+sub watch_state_ok ($) {
+ my ($state) = $_[-1]; # $_[0] may be $self
+ $state =~ /\Apause|(?:import|index|tag)-(?:ro|rw)\z/;
+}
+
+sub cancel_maildir_watch ($$) {
+ my ($d, $cfg_f) = @_;
+ my $w = delete $MDIR2CFGPATH->{$d}->{$cfg_f};
+ scalar(keys %{$MDIR2CFGPATH->{$d}}) or
+ delete $MDIR2CFGPATH->{$d};
+ for my $x (@{$w // []}) { $x->cancel }
+}
+
+sub refresh_watches {
+ my ($lei) = @_;
+ my $cfg = _lei_cfg($lei) or return;
+ my $old = $cfg->{-watches};
+ my $watches = $cfg->{-watches} //= {};
+ my %seen;
+ my $cfg_f = $cfg->{'-f'};
+ for my $w (grep(/\Awatch\..+\.state\z/, keys %$cfg)) {
+ my $url = substr($w, length('watch.'), -length('.state'));
+ require PublicInbox::LeiWatch;
+ my $lw = $watches->{$url} //= PublicInbox::LeiWatch->new($url);
+ $seen{$url} = undef;
+ my $state = $cfg->get_1("watch.$url", 'state');
+ if (!watch_state_ok($state)) {
+ $lei->err("watch.$url.state=$state not supported");
+ next;
+ }
+ if ($url =~ /\Amaildir:(.+)/i) {
+ my $d = File::Spec->canonpath($1);
+ if ($state eq 'pause') {
+ cancel_maildir_watch($d, $cfg_f);
+ } elsif (!exists($MDIR2CFGPATH->{$d}->{$cfg_f})) {
+ my @w = $dir_idle->add_watches(
+ ["$d/cur", "$d/new"], 1);
+ push @{$MDIR2CFGPATH->{$d}->{$cfg_f}}, @w if @w;
+ }
+ } else { # TODO: imap/nntp/jmap
+ $lei->child_error(1,
+ "E: watch $url not supported, yet");
+ }
+ }
+ if ($old) { # cull old non-existent entries
+ for my $url (keys %$old) {
+ next if exists $seen{$url};
+ delete $old->{$url};
+ if ($url =~ /\Amaildir:(.+)/i) {
+ my $d = File::Spec->canonpath($1);
+ cancel_maildir_watch($d, $cfg_f);
+ } else { # TODO: imap/nntp/jmap
+ $lei->child_error(1, "E: watch $url TODO");
+ }
+ }
+ }
+ if (scalar keys %$watches) {
+ $cfg->{-env} //= { %{$lei->{env}}, PWD => '/' }; # for cfg2lei
+ } else {
+ delete $cfg->{-watches};
+ }
+}
+