source: projects/md2mb/devel/md2mb/bin/md2mb.pl @ 1390

Revision 1390, 3.5 KB checked in by bruno, 17 months ago (diff)
  • Fix the RE which wasn't capturing all subdirectories .directories correctly for md2mb.pl
  • MOve the inbox file into Inbox in case it doesn't exist
Line 
1#!/usr/bin/perl -w
2#
3# Program to import a maildir kmail environement into a thunderbird one.
4
5use strict;
6use File::Find;
7use File::Copy;
8use File::Basename;
9use File::Path;
10use File::Glob ':glob';
11
12my $cmd="formail";
13# CHANGE AS YOU WISH
14my $oldroot = "/users/segolene/.Mail";
15my $newroot = "/users/segolene/.thunderbird/qk2f4dl6.default/Mail/pop.home.musique-ancienne.org/";
16# Is the newroot a file (1) or a dir (0)
17my $nrisfile = 0;
18my $debug = 0;
19# END CHANGE
20
21$debug++ if ((defined $ARGV[0]) && ($ARGV[0] eq "-v"));
22print "DEBUG MODE, not doing anything, just printing\n" if ($debug);
23if ($debug) { 
24        print "TARGET DIR: mkdir -p $newroot\n" if ((not -d "$newroot") && (not $nrisfile));
25        print "CMD: mkdir -p $newroot\n" if ((not -d "$newroot") && (not $nrisfile));
26} else {
27        mkpath("$newroot",0, 0755) if ((not -d "$newroot") && (not $nrisfile));
28}
29system("$cmd </dev/null >/dev/null 2>/dev/null") == 0 or die "cannot find formail on your \$PATH!\nAborting";
30
31find(\&md2mb,($oldroot));
32
33if ((-z "$newroot/Inbox") || (! -e "$newroot/Inbox")) {
34        print "Renaming inbox into Inbox\n";
35        move("$newroot/inbox","$newroot/Inbox") if (-e "$newroot/inbox");
36}
37
38sub md2mb {
39
40if (-f $File::Find::name) {
41        if (($File::Find::name =~ /\.ids$/) ||
42                ($File::Find::name =~ /\.sorted$/) ||
43                ($File::Find::name =~ /\.index$/)) {
44                print "SKIP FILE: $File::Find::name\n" if ($debug);
45                return;
46        }
47}
48if (-d $File::Find::name) {
49        if (($File::Find::name =~ /\/cur$/) ||
50                ($File::Find::name =~ /\/new$/) ||
51                ($File::Find::name =~ /\/tmp$/)) {
52                print "SKIP DIR: $File::Find::name\n" if ($debug);
53                return;
54        }
55}
56my $destname = $File::Find::name;
57# Target name is under a different root dir
58$destname =~ s|^$oldroot||;
59# Target name is not under a .directory dir but under a .sdb one
60$destname =~ s|\.([^/]+)\.directory/|$1.sbd/|g;
61# Here we create the target dir and target name
62my $cdir = dirname("$newroot/$destname");
63my $outputfile="$newroot/$destname";
64# Handle case where target file name is empty
65$outputfile="$newroot" if ($destname =~ /^\s*$/);
66# When we treat a dir, we will have to handle what it has below
67if (-d $File::Find::name) {
68        if ($debug) { 
69                print "DIR SRC: $File::Find::name\n";
70        }
71        my @files = (bsd_glob("$File::Find::name/cur/*"),bsd_glob("$File::Find::name/new/*"));
72        if (@files) {
73                if ($debug) { 
74                        print "DIR ($File::Find::name) DIR TARGET: mkdir -p $cdir\n" if (not -d "$cdir");
75                } else {
76                        mkpath("$cdir",0, 0755) if (not -d "$cdir");
77                }
78        }
79        foreach my $file (@files) {
80                next unless -f $file; # skip non-regular files
81                next unless -s $file; # skip empty files
82                next unless -r $file; # skip unreadable files
83                $file =~ s/'/'"'"'/g;  # escape ' (single quote)
84                # NOTE! The output file must not contain single quotes (')!
85                my $run = "cat '$file' | $cmd >> '$outputfile'";
86                if ($debug) { 
87                        print "COPYING CONTENT maildir($file) to $outputfile\n";
88                        print "CMD: $run\n";
89                } else {
90                        print "Copying maildir content from $file to $outputfile\n";
91                        system($run) == 0 or warn "cannot run \"$run\".";
92                }
93        }
94}
95if (-f $File::Find::name) {
96        if (($File::Find::name =~ /\/cur\//) ||
97                ($File::Find::name =~ /\/new\//) ||
98                ($File::Find::name =~ /\/tmp\//)) {
99                print "SKIP FILE: $File::Find::name\n" if ($debug);
100                return;
101        }
102        if ($debug) { 
103                print "FILE ($File::Find::name) TARGET DIR: mkdir -p $cdir\n" if (not -d "$cdir");
104                print "CMD: cp $File::Find::name $cdir\n";
105        } else {
106                print "Copying mailbox content from $File::Find::name to $cdir\n";
107                mkpath("$cdir",0, 0755) if (not -d "$cdir");
108                copy($File::Find::name,$cdir);
109        }
110}
111}
Note: See TracBrowser for help on using the repository browser.