summaryrefslogtreecommitdiffstats
path: root/lib/store/fix_storage.pl
blob: ba60561623efc1a58405708d9a02075986c2ffac (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/perl -w

use strict;

# A small script to convert current-style KOffice tar storages to storages
# compatible with KOffice 1.0 and KOffice 1.1(.1)

# Note to developers:
#  Add the PID (in Perl: $$ ) to all fixed temporary directory/file names,
#  so that this script can be run multiple times at once.

# Holds the directory tree
my @rootdir;
my $tmpdir = "/tmp/kofficeconverter$$" ;
print "Using temporary directory... $tmpdir\n";
# Holds the source/dest of the files to fix
my @needFixing;

# Walk the whole archive and collect information about the files
# This creates one array, containing another array for every directory
# we found (recursively). Additionally this array holding a directory
# holds the name of the directory and the path.
sub explore {
  my($path) = @_;
  my(@dir);

  print "   Exploring: $path\n";
  chdir($path);
  opendir(DIR, $path) || die "Couldn't open the directory: $!";
  my @contents = readdir(DIR);
  my $i = 0;
  foreach(@contents) {
    if($_ eq "." || $_ eq "..") {
      next;      # we're not intersted in . and ..
    }
    if(-d $_) {
      $dir[$i] = [ $_, $path, [ explore($path . '/' . $_) ] ];
      chdir($path);    # back to the directory where we come from
    }
    else {
      $dir[$i] = $_;
    }
    $i = $i + 1;
  }
  closedir(DIR);
  return @dir;
}

# Dumps the scary datastructure we built
sub dumpTree {
  my(@dir) = @_;
  foreach(@dir) {
    if(ref($_) eq 'ARRAY') {
      print $_->[0], "   (", $_->[1], ")\n";
      dumpTree(@{$_->[2]});
    }
    else {
      print $_ . "\n";
    }
  }
}

# Finds the files where we have to fix part references (->maindoc.xml)
sub findCandidates {
  my($dref, $currentdir, $tqparentdir) = @_;
  my @dir = @{$dref};
  #print "current: $currentdir, tqparentdir: $tqparentdir\n";
  foreach(@dir) {
    if(ref($_) eq 'ARRAY') {
      #print $_->[0], "   (", $_->[1], ")\n";
      findCandidates(\@{$_->[2]}, $_->[0], $_->[1]);
    }
    else {
      if($_ =~ m/maindoc\.xml/) {
	my $source = $tqparentdir . '/' . $currentdir . "/maindoc.xml";
	my $dest = $tqparentdir . '/' . $currentdir . ".xml";
	push(@needFixing, [ $source, $dest ]);
      }
    }
  }
}

# No need to move around elements of the root directory, these are handled
# separately anyway. Therefore we call findCandidates only on subdirs
sub findMainDocuments {
  foreach(@rootdir) {
    if(ref($_) eq 'ARRAY') {
      findCandidates(\@{$_->[2]}, $_->[0], $_->[1]);
    }
  }
}

# Factorizes the common regexp code between maindoc.xml and parts
sub fixLine {
  my($line, $prefix) = @_;

  if($line =~ m/(\s*\<object\s+mime=\"[^\"]*\"\s+url=\")([^\"]*)(\".*)/) {
    return $1 . $prefix . $2 . $3 . "\n";
  }
  elsif($line =~ m/(\s*\<OBJECT\s+mime=\"[^\"]*\"\s+url=\")([^\"]*)(\".*)/) {
    return $1 . $prefix . $2 . $3 . "\n";
  }
  elsif($line =~ m/(\s*\<KEY\s+.*\s+)filename(=\"[^\"]*\".*)/) {
    my($tmp) = $1 . "key" . $2 . "\n";
    if($tmp =~ m/(\s*\<KEY\s+.*\s+name=\")([^\"]*)(\".*)/) {
      return $1 . $prefix . $2 . $3 . "\n";
    }
    return $tmp;
  }
# Replace pictures by images, as cliparts will never work with only this script.
  elsif($line =~ m%\s*\<PICTURE%) {
      $line =~ s%\<PICTURES%\<PIXMAPS% ;
      $line =~ s%\<PICTURE%\<IMAGE% ;
  }
  elsif($line =~ m%\s*\</PICTURE%) {
      $line =~ s%\</PICTURES%\</PIXMAPS% ;
      $line =~ s%\</PICTURE%\</IMAGE% ;
  }
  elsif($line =~ m%\s*\<BACKPICTUREKEY%) {
      $line =~ s%\<BACKPICTUREKEY%\<BACKPIXKEY% ;
  }
  return $line;
}

# Walks through all the documents and fixes links. "Fixes" all the
# candidates we found
sub fixLinks {
  for my $item (@needFixing) {
    my $prefix = substr $item->[0], length($tmpdir)+1;
    $prefix =~ m,^(.*?)(maindoc\.xml),;
    $prefix = "tar:/" . $1;
    open(SOURCE, "<$item->[0]") || die "Couldn't open the source file: $!\n";
    open(DEST, ">$item->[1]") || die "Couldn't open the destination file: $!\n";
    while(<SOURCE>) {
      print DEST fixLine($_, $prefix);
    }
    close(SOURCE);
    close(DEST);
  }
}

# Get rid of the moved files
sub removeOldFiles {
  foreach(@needFixing) {
    system("rm -rf $_->[0]");
  }
}

# Special case for the main document as we have to use a temporary
# file and stuff like that. We only have to fix part references here.
sub fixMainDocument {
  open(SOURCE, "<$tmpdir/maindoc.xml");
  open(DEST, ">$tmpdir/tmp.xml");
  while(<SOURCE>) {
    print DEST fixLine($_, "tar:/");
  }
  close(SOURCE);
  close(DEST);
  system("mv $tmpdir/tmp.xml $tmpdir/maindoc.xml");
}

##################################################
# The execution starts here
##################################################
if($#ARGV != 1) {
    print "Script to convert current storages to KOffice 1.0/1.1.x compatible ones.\n";
    print "Usage: perl fix_storage.pl <inputfile> <outputfile>\n";
    exit(1);
}

# remember where we came from
chomp(my $cwd = `pwd`);

# clean up properly
system("rm -rf $tmpdir");
mkdir $tmpdir || die "Couldn't create tmp directory: $!\n";


print "Trying to detect the type of archive... ";
my($mime) = `file -i -z $ARGV[0]`;

if($mime =~ m,application/x-tar,) {
  print "tar.gz\n";
  print "Uncompressing the archive...\n";
  system("tar -C $tmpdir -xzf $ARGV[0]");
}
elsif($mime =~ m,application/x-zip,) {
  print "zip\n";
  print "Uncompressing the archive...\n";
  system("unzip -qq -d $tmpdir $ARGV[0]");
}

print "Browsing the directory structure...\n";
@rootdir = explore($tmpdir);

# debugging
#dumpTree(@rootdir);

print "Find candidates for moving...\n";
findMainDocuments();

print "Moving and fixing relative links...\n";
fixLinks();
removeOldFiles();
fixMainDocument();

print "Creating the archive...\n";
chdir($tmpdir);
system("tar czf tmp$$.tgz *");
chdir ($cwd);
system("mv $tmpdir/tmp$$.tgz $ARGV[1]");

print "Cleaning up...\n";
# clean up properly
system("rm -rf $tmpdir");

print "Done.\n";