summaryrefslogtreecommitdiff
path: root/notmuch-import-patch
blob: 5a8e5896d6829749156d6a88cfa232cbf1404561 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/perl

# notmuch-import-patch -- import a git patch series into notmuch

# Copyright (C) 2019 Sean Whitton
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

use strict;
use warnings;

use Config::Tiny;
use File::Spec::Functions qw(catfile);
use File::Which;
use IPC::System::Simple qw(systemx);

my $Config = Config::Tiny->new;

die "notmuch-import-patch: this script requires git to be installed"
  unless defined which "git";
die "notmuch-import-patch: this script requires notmuch to be installed"
  unless defined which "notmuch";

my $maildir;

my $mailscripts_conf_dir = defined $ENV{'XDG_CONFIG_HOME'}
  ? catfile $ENV{'XDG_CONFIG_HOME'}, "/mailscripts"
  : catfile $ENV{'HOME'}, "/.config/mailscripts";

my $notmuch_import_patch_conf = "$mailscripts_conf_dir/notmuch-import-patch";
if (-f $notmuch_import_patch_conf) {
    $Config = Config::Tiny->read($notmuch_import_patch_conf);
    $maildir = $Config->{_}->{maildir};
} else {
    # user probably doesn't want our generated patches, which are not
    # real e-mails, to go into their inbox
    my $database_path = `notmuch config get database.path`;
    chomp $database_path;
    $maildir = catfile $database_path, "patches";
}

die "notmuch-import-patch: $maildir does not look to be a maildir"
  unless (-d catfile($maildir, "cur")
          && -d catfile($maildir, "new")
          && -d catfile($maildir, "tmp"));

systemx("maildir-import-patch", $maildir, @ARGV);

systemx(qw(notmuch new));