]> mj.ucw.cz Git - gallery.git/blob - gal2/bin/gal-mj-upgrade
Gallery2: gal-scan --update supported
[gallery.git] / gal2 / bin / gal-mj-upgrade
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use UCW::Gallery;
7 use File::Spec;
8
9 my $photos_root = $ENV{HOME} . '/photos';
10
11 my $album = $ARGV[0] // die "Usage: gal mj-upgrade <album>\n";
12 print "### $album ###\n";
13
14 ### Scan photos.map ###
15
16 my $year = $album;
17 $year =~ s{/.*}{};
18 if ($album eq '2009/Fireworks') { $year = 2008; }
19 print "Loading map for $year\n";
20 open M, "$photos_root/photos.map" or die "No map file found\n";
21 my %map = ();
22 while (<M>) {
23         chomp;
24         m{^$year} or next;
25         my ($path, $hash) = split /\t/;
26         my $name = $path;
27         $name =~ s{.*/}{};
28         if (defined $map{$name}) {
29                 my $prev = $map{$name};
30                 if ($prev->{hash} ne $hash) {
31                         print STDERR "Collision for $name: ", $prev->{path}, " vs. ", $path, "\n";
32                 } else {
33                         # print STDERR "Harmless collision for $name: ", $prev->{path}, " vs. ", $path, "\n";
34                 }
35         } else {
36                 $map{$name} = { path => $path, hash => $hash };
37         }
38 }
39 close M;
40
41 ### Scan static list (if any) ###
42
43 my @src = ();
44 if (open S, "../static/photos/$album/x") {
45         print "Found static list\n";
46         while (<S>) {
47                 chomp;
48                 my @fields = split /\t/;
49                 if (@fields == 4) {
50                         push @src, { name => $fields[0], rotate => $fields[2], xform => $fields[3] };
51                 } elsif (@fields == 2) {
52                         push @src, { name => $fields[0], rotate => $fields[1], xform => '.' };
53                 } else {
54                         die "Error parsing gallery list: $_\n";
55                 }
56         }
57         close S;
58 }
59
60 ### Parse index.cgi and produce gallery.new ###
61
62 open I, "$album/index.cgi" or die "Cannot find $album/index.cgi\n";
63 open W, ">$album/gallery.new" or die "Cannot create $album/gallery.new\n";
64 my %opt = ();
65 my %found_dirs = ();
66 my @items = ();
67 while (<I>) {
68         chomp;
69         if (/^\s+"(\w+)" => "(.*)",?$/) {
70                 $opt{$1} = $2;
71                 print "Option: $1 = $2\n";
72         } elsif (/^img\("([^"]+)\.jpe?g", "([^"]*)"\);\s*# (\S+)/) {
73                 my $nr = $1;
74                 my $title = $2;
75                 my $file = $3;
76                 $file =~ s{^.*/}{}g;
77
78                 my $map = $map{$file};
79                 if (!$map) {
80                         print STDERR "$album: No match for $file\n";
81                         next;
82                 }
83                 my $path = $map->{path};
84                 my ($vv, $dd, $ff) = File::Spec->splitpath($path);
85                 $found_dirs{$dd} = 1;
86
87                 print "Image: $nr $path [$title]\n";
88                 my $src;
89                 if (@src) {
90                         if ($nr =~ m{^\d+$} && $nr <= @src) {
91                                 $src = $src[$nr-1];
92                         } else {
93                                 print STDERR "$album: Crooked refs ($nr)\n";
94                         }
95                 }
96
97                 print W join("\t",
98                         "$photos_root/$path",
99                         $map->{hash},
100                         ($src ? $src->{rotate} : '-'),
101                         ($src ? $src->{xform} : '.'),
102                         ($title ne "" ? $title : '-'),
103                         ), "\n";
104         } elsif (/^($|#|require|SetOptions|\)|Start|Finish)/) {
105                 # Nothing important
106         } else {
107                 print STDERR "$album/index.cgi: Parse error at line $.: $_\n";
108         }
109 }
110 close W;
111 close I;
112
113 if (scalar keys %found_dirs != 1) {
114         print STDERR "$album: Photos in multiple directories\n";
115 }
116
117 ### Create gallery.cf ###
118
119 open CF, ">$album/gallery.cf" or die "Cannot create $album/gallery.cf";
120 print CF <<'AMEN' ;
121 use strict;
122 use warnings;
123 use utf8;
124
125 my $gal = require '../../default.cf';
126 $gal->set(
127 AMEN
128
129 for my $cf (reverse sort keys %opt) {
130         print CF "\t$cf => \"", $opt{$cf}, "\",\n";
131 }
132
133 print CF <<'AMEN' ;
134 );
135
136 return $gal;
137 AMEN
138
139 close CF;