source: ProjectBuilder/projects/md2mb/devel/md2mb/bin/md2mb.pl@ 1389

Last change on this file since 1389 was 1389, checked in by Bruno Cornec, 12 years ago
  • Fix problems with md2mb by using bsd_glob to avoid space issues.
File size: 3.3 KB
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/60q6hqwp.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
33sub md2mb {
34
35if (-f $File::Find::name) {
36 if (($File::Find::name =~ /\.ids$/) ||
37 ($File::Find::name =~ /\.sorted$/) ||
38 ($File::Find::name =~ /\.index$/)) {
39 print "SKIP FILE: $File::Find::name\n" if ($debug);
40 return;
41 }
42}
43if (-d $File::Find::name) {
44 if (($File::Find::name =~ /\/cur$/) ||
45 ($File::Find::name =~ /\/new$/) ||
46 ($File::Find::name =~ /\/tmp$/)) {
47 print "SKIP DIR: $File::Find::name\n" if ($debug);
48 return;
49 }
50}
51my $destname = $File::Find::name;
52# Target name is under a different root dir
53$destname =~ s|^$oldroot||;
54# Target name is not under a .directory dir but under a .sdb one
55$destname =~ s|\.([[:alnum:]éèçàù\s]*)\.directory|$1.sbd|g;
56# Here we create the target dir and target name
57my $cdir = dirname("$newroot/$destname");
58my $outputfile="$newroot/$destname";
59# Handle case where target file name is empty
60$outputfile="$newroot" if ($destname =~ /^\s*$/);
61# When we treat a dir, we will have to handle what it has below
62if (-d $File::Find::name) {
63 if ($debug) {
64 print "DIR SRC: $File::Find::name\n";
65 }
66 my @files = (bsd_glob("$File::Find::name/cur/*"),bsd_glob("$File::Find::name/new/*"));
67 if (@files) {
68 if ($debug) {
69 print "DIR ($File::Find::name) DIR TARGET: mkdir -p $cdir\n" if (not -d "$cdir");
70 } else {
71 mkpath("$cdir",0, 0755) if (not -d "$cdir");
72 }
73 }
74 foreach my $file (@files) {
75 next unless -f $file; # skip non-regular files
76 next unless -s $file; # skip empty files
77 next unless -r $file; # skip unreadable files
78 $file =~ s/'/'"'"'/g; # escape ' (single quote)
79 # NOTE! The output file must not contain single quotes (')!
80 my $run = "cat '$file' | $cmd >> '$outputfile'";
81 if ($debug) {
82 print "COPYING CONTENT maildir($file) to $outputfile\n";
83 print "CMD: $run\n";
84 } else {
85 print "Copying maildir content from $file to $outputfile\n";
86 system($run) == 0 or warn "cannot run \"$run\".";
87 }
88 }
89}
90if (-f $File::Find::name) {
91 if (($File::Find::name =~ /\/cur\//) ||
92 ($File::Find::name =~ /\/new\//) ||
93 ($File::Find::name =~ /\/tmp\//)) {
94 print "SKIP FILE: $File::Find::name\n" if ($debug);
95 return;
96 }
97 if ($debug) {
98 print "FILE ($File::Find::name) TARGET DIR: mkdir -p $cdir\n" if (not -d "$cdir");
99 print "CMD: cp $File::Find::name $cdir\n";
100 } else {
101 print "Copying mailbox content from $File::Find::name to $cdir\n";
102 mkpath("$cdir",0, 0755) if (not -d "$cdir");
103 copy($File::Find::name,$cdir);
104 }
105}
106}
Note: See TracBrowser for help on using the repository browser.