booh-gui and distributable
[booh] / bin / booh
1 #!/usr/bin/ruby
2 #
3 #                         *  BOOH  *
4 #
5 # A.k.a `Best web-album Of the world, Or your money back, Humerus'.
6 #
7 # The acronyn sucks, however this is a tribute to Dragon Ball by
8 # Akira Toriyama, where the last enemy beaten by heroes of Dragon
9 # Ball is named "Boo". But there was already a free software project
10 # called Boo, so this one will be it "Booh". Or whatever.
11 #
12 #
13 # Copyright (c) 2004 Guillaume Cottenceau <gc3 at bluewin.ch>
14 #
15 # This software may be freely redistributed under the terms of the GNU
16 # public license version 2.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 require 'getoptlong'
23 require 'gettext'
24 include GetText
25 require 'rexml/document'
26 include REXML
27 require 'timeout'
28
29 require 'booh/booh-lib'
30 require 'booh/html-merges'
31
32 #- bind text domain as soon as possible because some _() functions are called early to build data structures
33 bindtextdomain("booh")
34
35 #- options
36 $options = [
37     [ '--help',          '-h', GetoptLong::NO_ARGUMENT,       _("Get help message") ],
38
39     [ '--no-check',      '-n', GetoptLong::NO_ARGUMENT,       _("Don't check for needed external programs at startup") ],
40
41     [ '--source',        '-s', GetoptLong::REQUIRED_ARGUMENT, _("Directory which contains original images/videos as files or subdirs") ],
42     [ '--destination',   '-d', GetoptLong::REQUIRED_ARGUMENT, _("Directory which will contain the web-album") ],
43     [ '--clean',         '-c', GetoptLong::NO_ARGUMENT,       _("Clean destination directory") ],
44
45     [ '--theme',         '-t', GetoptLong::REQUIRED_ARGUMENT, _("Select HTML theme to use") ],
46     [ '--config',        '-C', GetoptLong::REQUIRED_ARGUMENT, _("File containing config listing images and videos within directories with captions") ],
47     [ '--config-skel',   '-k', GetoptLong::REQUIRED_ARGUMENT, _("Filename where the script will output a config skeleton") ],
48     [ '--merge-config',  '-M', GetoptLong::REQUIRED_ARGUMENT, _("File containing config listing, where to merge new images/videos from --source") ],
49
50     [ '--mproc',         '-m', GetoptLong::REQUIRED_ARGUMENT, _("Specify the number of processors for multi-processors machines") ],
51
52     [ '--verbose-level', '-v', GetoptLong::REQUIRED_ARGUMENT, _("Set max verbosity level (0: errors, 1: warnings, 2: important messages, 3: other messages)") ],
53 ]
54
55 #- default values for some globals 
56 $convert = 'convert -interlace line +profile "*"'
57 $switches = []
58 $stdout.sync = true
59
60 def usage
61     puts __("Usage: %s [OPTION]...", File.basename($0))
62     $options.each { |ary|
63         printf " %3s, %-15s %s\n", ary[1], ary[0], ary[3]
64     }
65 end
66
67 def handle_options
68     parser = GetoptLong.new
69     parser.set_options(*$options.collect { |ary| ary[0..2] })
70     begin
71         parser.each_option do |name, arg|
72             case name
73             when '--help'
74                 usage
75                 exit(0)
76
77             when '--no-check'
78                 $no_check = true
79
80             when '--source'
81                 $source = arg.sub(%r|/$|, '')
82                 if !File.directory?($source)
83                     die __("Argument to --source must be a directory")
84                 end
85             when '--destination'
86                 $dest = arg.sub(%r|/$|, '')
87                 if File.exists?($dest) && !File.directory?($dest)
88                     die __("If --destination exists, it must be a directory")
89                 end
90                 if $dest != make_dest_filename($dest)
91                     die __("Sorry, destination directory can't contain non simple alphanumeric characters.")
92                 end
93             when '--clean'
94                 system("rm -rf #{$dest}")
95
96             when '--theme'
97                 select_theme(arg)
98             when '--config'
99                 if File.readable?(arg)
100                     $xmldoc = REXML::Document.new File.new(arg)
101                     $mode = 'use_config'
102                 else
103                     die __('Config file does not exist or is unreadable.')
104                 end
105             when '--config-skel'
106                 if File.exists?(arg)
107                     msg 1, __("Config skeleton file already exists, backuping to #{arg}.backup")
108                     File.rename(arg, "#{arg}.backup")
109                 end
110                 $config_writeto = arg
111                 $mode = 'gen_config'
112             when '--merge-config'
113                 if File.readable?(arg)
114                     msg 2, __("Merge config notice: backuping current config file to #{arg}.backup")
115                     $xmldoc = REXML::Document.new File.new(arg)
116                     File.rename(arg, "#{arg}.backup")
117                     $config_writeto = arg
118                     $mode = 'merge_config'
119                 else
120                     die __('Config file does not exist or is unreadable.')
121                 end
122
123             when '--mproc'
124                 $mproc = arg.to_i
125                 $pids = []
126
127             when '--verbose-level'
128                 $verbose_level = arg.to_i
129
130             end
131         end
132     rescue
133         puts $!
134         usage
135         exit(1)
136     end
137
138     if !$source && $xmldoc
139         $source = $xmldoc.root.attributes['source']
140         $dest = $xmldoc.root.attributes['destination']
141     end
142
143     if !$source
144         die __("Missing --source parameter.")
145     end
146     if !$dest
147         die __("Missing --destination parameter.")
148     end
149     if !$xmldoc
150         $xmldoc = Document.new "<booh version='#{$VERSION}' source='#{utf8($source)}' destination='#{utf8($dest)}'/>"
151         $xmldoc << XMLDecl.new( XMLDecl::DEFAULT_VERSION, $CURRENT_CHARSET )
152         $mode = 'gen_config'
153     end
154
155     if !$theme
156         select_theme('simple')
157     end
158
159 end
160
161 def check_installation
162     if $no_check
163         return
164     end
165     %w(convert identify exif transcode mencoder).each { |prg|
166         if !system("which #{prg} >/dev/null")
167             die __("The `%s' program is typically needed. Re-run with --no-check if you're sure you're fine without it.", prg)
168         end
169     }
170 end
171
172 def replace_line(surround, keyword, line)
173     begin
174         contents = eval "$#{keyword}"
175         line.sub!(/#{surround}#{keyword}#{surround}/, contents)
176     rescue NameError
177         die __("No `%s' found for substitution", keyword)
178     end
179 end
180
181 def build_html_skeletons
182     $html_images     = File.open("#{$FPATH}/themes/#{$theme}/skeleton_image.html").readlines
183     $html_thumbnails = File.open("#{$FPATH}/themes/#{$theme}/skeleton_thumbnails.html").readlines
184     $html_index      = File.open("#{$FPATH}/themes/#{$theme}/skeleton_index.html").readlines
185     for line in $html_images + $html_thumbnails + $html_index
186         while line =~ /~~~(\w+)~~~/
187             replace_line('~~~', $1, line)
188         end
189     end
190 end
191
192 def sys(cmd)
193     msg 2, cmd
194     system(cmd)
195 end
196
197 #- parallelizable sys
198 def psys(cmd)
199     if $mproc
200         if pid = fork
201             $pids << pid
202         else
203             msg 2, cmd + ' &'
204             system(cmd)
205             exit 0
206         end
207         if $pids.length == $mproc
208             finished = Process.wait2
209             $pids.delete(finished[0])
210             $pids = $pids.find_all { |pid| Process.waitpid(pid, Process::WNOHANG) == nil }
211         end
212     else
213         sys(cmd)
214     end
215 end
216
217 def find_caption_value(xmldir, filename)
218     xmldir.elements["[@filename='#{utf8(filename)}']"].attributes['caption']
219 end
220
221 def find_captions(xmldir, images)
222     return images.collect { |img| find_caption_value(xmldir, img) }
223 end
224
225 def entry2type(entry)
226     if entry =~ /\.(jpg|jpeg|jpe|gif|bmp|png)$/i
227         return 'image'
228     elsif entry =~ /\.(mov|avi|mpg|mpeg|mpe|wmv|asx)$/i
229         #- might consider using file magic later..
230         return 'video'
231     else
232         return nil
233     end
234 end
235
236 #- grab the results of a command but don't sleep forever on a runaway process
237 def subproc_runaway_aware(command)
238     begin
239         timeout(5) {
240             return `#{command}`
241         }
242     rescue Timeout::Error    
243         msg 1, _("forgetting runaway process (transcode sucks again?)")
244         #- todo should slay transcode but dunno how to do that
245         return nil
246     end
247 end
248
249 def gen_thumbnails(orig, xmldir, dests)
250     if !dests.detect { |dest| !File.exists?(dest['filename']) } 
251         return true
252     end
253
254     felem = xmldir.elements["[@filename='#{utf8(File.basename(orig))}']"]
255
256     if entry2type(orig) == 'image'
257         convert_options = ''
258         if felem
259             rotate = felem.attributes['rotate']
260             if !rotate
261                 orientation = `exif '#{orig}'`.detect { |line| line =~ /^Orientation/ }
262                 if orientation =~ /right - top/
263                     rotate = '90'
264                 end
265                 if orientation =~ /left - bottom/
266                     rotate = '-90'
267                 end
268                 rotate ||= '0'
269                 felem.add_attribute('rotate', rotate)
270             end
271             convert_options += "-rotate #{rotate} "
272         end
273         for dest in dests
274             if !File.exists?(dest['filename'])
275                 psys("#{$convert} #{convert_options}-geometry #{dest['size']} '#{orig}' '#{dest['filename']}'")
276             end
277         end
278         return true
279
280     elsif entry2type(orig) == 'video'
281         dest_dir = make_dest_filename(File.dirname(dests[0]['filename']))
282         #- frame-offset is an attribute that allows to specify which frame to use for the thumbnail;
283         #- first try in dir to allow for: <dir .. subdirs-captionfile='/tmp/src2/people/pict0008.mov' pict0008.mov-frame-offset='100' ..>
284         frame_offset = xmldir.attributes["#{utf8(File.basename(orig))}-frame-offset"]
285         if !frame_offset
286             #- then try in elements to allow for: <video filename='bourrique.mov' frame-offset='200'/>
287             felem and frame_offset = felem.attributes['frame-offset']
288         end
289         frame_offset = (frame_offset || 5).to_i
290         for dest in dests
291             if !File.exists?("#{dest_dir}/screenshot.jpg000004.jpg")
292                 cmd = "transcode -a 0 -c #{frame_offset-5}-#{frame_offset} -i '#{orig}' -y jpg -o '#{dest_dir}/screenshot.jpg' 2>&1"
293                 msg 2, cmd
294                 if subproc_runaway_aware(cmd) =~ /V: import format.*unknown/ || !File.exists?("#{dest_dir}/screenshot.jpg000004.jpg")
295                     msg 2, __("* could not extract first image of video %s with transcode, will try first converting with mencoder", orig)
296                     cmd = "mencoder '#{orig}' -nosound -ovc lavc -lavcopts vcodec=mjpeg -o '#{dest_dir}/foo.avi' -frames #{frame_offset} -fps 25 >/dev/null 2>/dev/null"
297                     msg 2, cmd
298                     system cmd
299                     if File.exists?("#{dest_dir}/foo.avi")
300                         cmd = "transcode -a 0 -c #{frame_offset-5}-#{frame_offset} -i '#{dest_dir}/foo.avi' -y jpg -o '#{dest_dir}/screenshot.jpg' 2>&1"
301                         msg 2, cmd
302                         results = subproc_runaway_aware(cmd)
303                         system("rm -f '#{dest_dir}/foo.avi'")
304                         if results =~ /V: import format.*unknown/ || !File.exists?("#{dest_dir}/screenshot.jpg000004.jpg")
305                             msg 0, __("could not extract first image of video %s encoded by mencoder", "#{dest_dir}/foo.avi")
306                             return false
307                         end
308                     else
309                         msg 0, __("could not make mencoder to encode %s to mpeg4", "#{orig}")
310                         return false
311                     end
312                 end
313
314             end
315             sys("#{$convert} -geometry #{dest['size']} #{dest_dir}/screenshot.jpg000004.jpg '#{dest['filename']}'")
316         end
317         return true
318     end
319 end
320
321 #- stolen from CVSspam
322 def urlencode(text)
323   text.sub(/[^a-zA-Z0-9\-,.*_\/]/) do
324     "%#{sprintf('%2X', $&[0])}"
325   end
326 end
327
328 def html_refresh(target)
329     return "<html><head><META http-equiv='refresh' content='0;URL=#{target}'></head><body></body><html>"
330 end
331
332 def discover_iterations(iterations, line)
333     if line =~ /~~iterate(\d)_open(_max(\d+))?~~/
334         for iter in iterations.values
335             if iter['open']
336                 iter['open'] = false
337                 iter['close_wait'] = $1.to_i
338             end
339         end
340         iterations[$1.to_i] = { 'open' => true, 'max' => $3, 'opening' => '', 'closing' => '' }
341         if $1.to_i == 1
342             line.sub!(/.*/, '~~thumbnails~~')
343         else
344             line.sub!(/.*/, '')
345         end
346     elsif line =~ /~~iterate(\d)_close~~/
347         iterations[$1.to_i]['open']  = false;
348         iterations[$1.to_i]['close'] = true;
349         line.sub!(/.*/, '')
350     else
351         for iter in iterations.values
352             if iter['open']
353                 iter['opening'] += line
354                 line.sub!(/.*/, '')
355             end
356             if !iter['close'] && iter['close_wait'] && iterations[iter['close_wait']]['close']
357                 iter['closing'] += line
358                 line.sub!(/.*/, '')
359             end
360         end
361     end
362 end
363
364 def reset_iterations(iterations)
365     for iter in iterations.values
366         iter['value'] = 1
367     end
368 end
369
370 def run_iterations(iterations)
371     html = ''
372     for level in iterations.keys.sort
373         if iterations[level]['value'] == 1 || level == iterations.keys.max
374             html += iterations[level]['opening']
375         end
376         iterations[level]['value'] += 1
377         if iterations[level]['max'] && iterations[level]['value'] > iterations[level]['max'].to_i
378             iterations[level]['value'] = 1
379             iterations[level-1]['value'] = 1
380             html += iterations[level-1]['closing']
381         end
382     end
383     return html
384 end
385
386 def close_iterations(iterations)
387     html = ''
388     for level in iterations.keys.sort.reverse
389         html += iterations[level]['closing']
390     end
391     return html
392 end
393
394 def img_element(fullpath)
395     sizespec = `identify '#{fullpath}'` =~ / JPEG (\d+)x(\d+) / ? 'width="' + $1 + '" height="' + $2 + '"' : ''
396     return '<img src="' + File.basename(fullpath) + '" ' + sizespec + ' border="0"/>'
397 end
398
399 def walk_source_dir
400
401     `find #{$source} -type d`.sort.each { |dir|
402         dir.chomp!
403
404         #- place xml document on proper node if exists, else create
405         xmldir = $xmldoc.elements["//dir[@path='#{utf8(dir)}']"]
406         if $mode == 'use_config'
407             if !xmldir
408                 next
409             end
410         else
411             if $mode == 'gen_config' || ($mode == 'merge_config' && !xmldir)
412                 #- add the <dir..> element if necessary
413                 parent = File.dirname(dir)
414                 xmldir = $xmldoc.elements["//dir[@path='#{utf8(parent)}']"]
415                 if !xmldir
416                     xmldir = $xmldoc.root
417                 end
418                 xmldir = xmldir.add_element 'dir', { 'path' => utf8(dir), 'new' => 1 }
419             end
420         end
421
422         #- read images/videos entries from config or from directories depending on mode
423         entries = []
424         if $mode == 'use_config'
425             msg 2, __("Handling %s from config list...", dir)
426             xmldir.elements.each { |element|
427                 if %w(image video).include?(element.name)
428                     entries << from_utf8(element.attributes['filename'])
429                 end
430             }
431         else
432             msg 2, __("Examining %s...", dir)
433             entries = Dir.entries(dir).sort
434             #- populate config in case of gen_config, add new files in case of merge_config
435             for file in entries
436                 type = entry2type(file)
437                 if type && !xmldir.elements["#{type}[@filename='#{utf8(file)}']"]
438                     xmldir.add_element type, { "filename" => utf8(file), "caption" => utf8(file.sub(/\.[^\.]+$/, '')[0..17]) }
439                 end
440             end
441             if $mode == 'merge_config'
442                 #- cleanup removed files from config and reread entries from config to get proper ordering
443                 entries = []
444                 xmldir.elements.each { |element|
445                     fullpath = "#{dir}/#{from_utf8(element.attributes['filename'])}"
446                     if %w(image video).include?(element.name)
447                         if !File.readable?(fullpath)
448                             msg 1, __("Config merge: removing #{fullpath} from config; use the backup file to retrieve caption info if this was a mistake")
449                             xmldir.delete(element)
450                         else
451                             entries << from_utf8(element.attributes['filename'])
452                         end
453                     end
454                 }
455             end
456         end
457         images = entries.find_all { |e| entry2type(e) == 'image' }
458         msg 3, __("\t%s images", images.length)
459         videos = entries.find_all { |e| entry2type(e) == 'video' }
460         msg 3, __("\t%s videos", videos.length)
461
462         dest_dir = make_dest_filename(dir.sub(/^#{Regexp.quote($source)}/, $dest))
463         system("mkdir -p '#{dest_dir}'")
464
465         #- copy any resource file that goes with the theme (css, images..)
466         for entry in Dir.entries("#{$FPATH}/themes/#{$theme}")
467             if !%w(. .. skeleton_image.html skeleton_thumbnails.html skeleton_index.html parameters.rb CVS).include?(entry)
468                 if !File.exists?("#{dest_dir}/#{entry}")
469                     psys("cp '#{$FPATH}/themes/#{$theme}/#{entry}' '#{dest_dir}'")
470                 end
471             end
472         end
473
474         #- pass through if there are no images and videos
475         if images.size == 0 && videos.size == 0
476             next
477         end
478
479         msg 2, __("Outputting in %s...", dest_dir)
480
481         #- populate data structure with sizes from theme
482         for sizeobj in $images_size
483             fullscreen_images ||= {}
484             fullscreen_images[sizeobj['name']] = []
485             thumbnail_images ||= {}
486             thumbnail_images[sizeobj['name']] = []
487             thumbnail_videos ||= {}
488             thumbnail_videos[sizeobj['name']] = []
489         end
490
491         images.size >= 1 and msg 3, __("\tcreating images thumbnails...")
492
493         #- create thumbnails for images
494         images.each { |img|
495             base_dest_img = dest_dir + '/' + make_dest_filename(img.sub(/\.[^\.]+$/, ''))
496             for sizeobj in $images_size
497                 size_fullscreen = sizeobj['fullscreen']
498                 size_thumbnails = sizeobj['thumbnails']
499                 fullscreen_dest_img = base_dest_img + "-#{size_fullscreen}.jpg"
500                 thumbnail_dest_img  = base_dest_img + "-#{size_thumbnails}.jpg"
501                 fullscreen_images[sizeobj['name']] << File.basename(fullscreen_dest_img)
502                 thumbnail_images[sizeobj['name']]  << File.basename(thumbnail_dest_img)
503                 gen_thumbnails("#{dir}/#{img}", xmldir, [ { 'filename' => fullscreen_dest_img, 'size' => size_fullscreen },
504                                                           { 'filename' => thumbnail_dest_img, 'size' => size_thumbnails } ])
505             end
506         }
507
508         videos.size >= 1 and msg 3, __("\tcreating videos thumbnails...")
509
510         #- create thumbnails for videos
511         videos.each { |video|
512             thumbnail_ok = true
513             for sizeobj in $images_size
514                 size_thumbnails = sizeobj['thumbnails']
515                 thumbnail_dest_img = dest_dir + '/' + make_dest_filename(video.sub(/\.[^\.]+$/, '')) + "-#{size_thumbnails}.jpg"
516                 thumbnail_videos[sizeobj['name']] << File.basename(thumbnail_dest_img)
517                 thumbnail_ok &&= gen_thumbnails("#{dir}/#{video}", xmldir, [ { 'filename' => thumbnail_dest_img, 'size' => size_thumbnails } ])
518             end
519             destvideo = "#{dest_dir}/#{video}"
520             if !File.exists?(destvideo)
521                 psys("cp '#{dir}/#{video}' '#{destvideo}'")
522             end
523             #- cleanup temp
524             system("rm -f #{dest_dir}/screenshot.jpg00000*")
525         }
526
527         #- fake for gettext to find these; if themes need more sizes, english name for them should be added here
528         sizenames = { 'small' => utf8(_("small")), 'medium' => utf8(_("medium")), 'large' => utf8(_("large")) }
529
530         msg 3, __("\tgenerating HTML pages...")
531         
532         #- generate thumbnails.html (page with thumbnails)
533         for sizeobj in $images_size
534             html = $html_thumbnails.collect { |l| l.clone }
535             iterations = {}
536             for i in html
537                 i.sub!(/~~run_slideshow~~/, images.size <= 1 ? '' : '<a href="image-' + sizeobj['name'] + '.html?run_slideshow">' + utf8(_('Run slideshow!')) + '</a>')
538                 i.sub!(/~~title~~/, xmldir.attributes['thumbnails-caption'] || utf8(File.basename(dir)))
539                 for sizeobj2 in $images_size
540                     if sizeobj != sizeobj2
541                         i.sub!(/~~size_#{sizeobj2['name']}~~/, '<a href="thumbnails-' + sizeobj2['name'] + '.html">' + sizenames[sizeobj2['name']] + '</a>')
542                     else
543                         i.sub!(/~~size_#{sizeobj2['name']}~~/, sizenames[sizeobj2['name']])
544                     end
545                 end
546                 discover_iterations(iterations, i)
547             end
548             html_thumbnails = ''
549             reset_iterations(iterations)
550             for file in entries
551                 type = images.include?(file) ? 'image' : videos.include?(file) ? 'video' : nil
552                 if type
553                     html_thumbnails += run_iterations(iterations)
554                     if type == 'image'
555                         index = images.index(file)
556                         html_thumbnails.gsub!(/~~image_iteration~~/,
557                                               '<a href="image-' + sizeobj['name'] + '.html?current=' + fullscreen_images[sizeobj['name']][index] + '">' +
558                                                   img_element("#{dest_dir}/#{thumbnail_images[sizeobj['name']][index]}") + '</a>')
559                         html_thumbnails.gsub!(/~~caption_iteration~~/,
560                                               find_caption_value(xmldir, images[index]) || utf8(images[index]))
561                         html_thumbnails.gsub!(/~~ifimage\?~~(.+?)~~fi~~/) { $1 }
562                         html_thumbnails.gsub!(/~~ifvideo\?~~(.+?)~~fi~~/, '')
563                     elsif type == 'video'
564                         index = videos.index(file)
565                         if File.exists?("#{dest_dir}/#{thumbnail_videos[sizeobj['name']][index]}")
566                             html_thumbnails.gsub!(/~~image_iteration~~/,
567                                                   '<a href="' + videos[index] + '">' + img_element("#{dest_dir}/#{thumbnail_videos[sizeobj['name']][index]}") + '</a>')
568                         else
569                             html_thumbnails.gsub!(/~~image_iteration~~/,
570                                                   '<a href="' + videos[index] + '">' + utf8(_("(no preview)")) + '</a>')
571                         end
572                         html_thumbnails.gsub!(/~~caption_iteration~~/,
573                                               find_caption_value(xmldir, videos[index]) || utf8(videos[index]))
574                         html_thumbnails.gsub!(/~~ifimage\?~~(.+?)~~fi~~/, '')
575                         html_thumbnails.gsub!(/~~ifvideo\?~~(.+?)~~fi~~/) { $1 }
576                     end
577                 end
578             end
579             html_thumbnails += close_iterations(iterations)
580             for i in html
581                 i.sub!(/~~thumbnails~~/, html_thumbnails)
582             end
583             ios = File.open("#{dest_dir}/thumbnails-#{sizeobj['name']}.html", "w")
584             ios.write(html)
585             ios.close
586         end
587
588         #- generate image.html (page with fullscreen images)
589         if images.size > 0
590             #- don't ask me why I need so many backslashes... the aim is to print \\\" for each " in the javascript source
591             captions4js = find_captions(xmldir, images).collect { |e| e ? '"' + e.gsub('"', '\\\\\\\\\\\\\\\\\"' ) + '"' : '""' }.join(', ')
592             for sizeobj in $images_size
593                 html = $html_images.collect { |l| l.clone }
594                 images4js = fullscreen_images[sizeobj['name']].collect { |e| "\"#{e}\"" }.join(', ')
595                 otherimages4js = ''
596                 othersizes = []
597                 for sizeobj2 in $images_size
598                     if sizeobj != sizeobj2
599                         otherimages4js += "var images_#{sizeobj2['name']} = new Array(" + fullscreen_images[sizeobj2['name']].collect { |e| "\"#{e}\"" }.join(', ') + ")\n"
600                         othersizes << "\"#{sizeobj2['name']}\""
601                     end
602                 end
603                 for i in html
604                     i.sub!(/~~images~~/, images4js)
605                     i.sub!(/~~other_images~~/, otherimages4js)
606                     i.sub!(/~~other_sizes~~/, othersizes.join(', '))
607                     i.sub!(/~~captions~~/, captions4js)
608                     i.sub!(/~~title~~/, xmldir.attributes['thumbnails-caption'] || utf8(File.basename(dir)))
609                     i.sub!(/~~thumbnails~~/, '<a href="thumbnails-' + sizeobj['name'] + '.html">' + utf8(_('Return to thumbnails')) + '</a>')
610                     for sizeobj2 in $images_size
611                         if sizeobj != sizeobj2
612                             i.sub!(/~~size_#{sizeobj2['name']}~~/,
613                                    '<a href="image-' + sizeobj2['name'] + '.html" id="link' + sizeobj2['name'] + '">' + sizenames[sizeobj2['name']] + '</a>')
614                         else
615                             i.sub!(/~~size_#{sizeobj2['name']}~~/,
616                                    sizenames[sizeobj2['name']])
617                         end
618                     end
619                 end
620                 ios = File.open("#{dest_dir}/image-#{sizeobj['name']}.html", "w")
621                 ios.write(html)
622                 ios.close
623             end
624         end
625     }
626
627     msg 3, ''
628
629     #- add attributes to <dir..> elements needing so
630     if $mode != 'use_config'
631         msg 3, __("\tfixating configuration file...")
632         $xmldoc.elements.each('//dir[@new]') { |element|
633             path = captionpath = element.attributes['path']
634             child = element
635             captionfile = nil
636             while true
637                 child = child.elements[1]
638                 if !child
639                     element.remove  #- means we have a directory with nothing interesting in it
640                     break
641                 elsif child.name == 'dir'
642                     captionpath = child.attributes['path']
643                 else
644                     captionfile = "#{captionpath}/#{child.attributes['filename']}"
645                     break
646                 end
647             end
648             basename = File.basename(path)
649             if element.elements['dir']
650                 element.add_attribute('subdirs-caption', basename)
651                 element.add_attribute('subdirs-captionfile', captionfile)
652             end
653             if element.elements['image'] || element.elements['video']
654                 element.add_attribute('thumbnails-caption', basename)
655                 element.add_attribute('thumbnails-captionfile', captionfile)
656             end
657             element.delete_attribute('new')
658         }
659     end
660
661     #- write down to disk config if necessary
662     if $config_writeto
663         ios = File.open($config_writeto, "w")
664         $xmldoc.write(ios, 0)
665         ios.close
666     end
667
668     #- second pass to create index.html files
669     default_thumbnails = $images_size.detect { |sizeobj| sizeobj['default'] }
670     if !default_thumbnails
671         die __("Theme `%s' has no default size.", $theme)
672     else
673         default_thumbnails = default_thumbnails['name']
674     end
675
676     msg 3, __("\trescanning directories to generate all `index.html' files...")
677
678     `find #{$source} -type d`.each { |dir|
679         dir.chomp!
680         xmldir = $xmldoc.elements["//dir[@path='#{utf8(dir)}']"]
681         if !xmldir
682             next
683         end
684         dest_dir = make_dest_filename(dir.sub(/^#{Regexp.quote($source)}/, $dest))
685
686         html = $html_index.collect { |l| l.clone }
687         iterations = {}
688         for i in html
689             caption = xmldir.attributes['subdirs-caption'] || xmldir.attributes['thumbnails-caption'] || utf8(File.basename(dir))
690             i.gsub!(/~~title~~/, caption)
691             if xmldir.parent.name == 'dir'
692                 nav = ''
693                 path = '..'
694                 parent = xmldir.parent
695                 while parent.name == 'dir'
696                     parentcaption = parent.attributes['subdirs-caption'] || parent.attributes['thumbnails-caption'] || File.basename(parent.attributes['path'])
697                     nav = "<a href='#{path}/index.html'>#{parentcaption}</a> #{utf8(_(" > "))} #{nav}"
698                     path += '/..'
699                     parent = parent.parent
700                 end
701                 i.gsub!(/~~ifnavigation\?~~(.+?)~~fi~~/) { $1 }
702                 i.gsub!(/~~navigation~~/, nav + caption)
703             else
704                 i.gsub!(/~~ifnavigation\?~~(.+?)~~fi~~/, '')
705             end
706             discover_iterations(iterations, i)
707         end
708
709         html_index = ''
710         reset_iterations(iterations)
711
712         if xmldir.elements['dir']
713             #- deal with "current" album (directs to "thumbnails" page)
714             if xmldir.attributes['thumbnails-caption']
715                 thumbnail = "#{dest_dir}/thumbnails-thumbnail.jpg"
716                 gen_thumbnails(from_utf8(xmldir.attributes['thumbnails-captionfile']), xmldir, [ { 'filename' => thumbnail, 'size' => $albums_thumbnail_size } ])
717                 html_index += run_iterations(iterations)
718                 html_index.gsub!(/~~image_iteration~~/, "<a href='thumbnails-#{default_thumbnails}.html'>" + img_element(thumbnail) + '</a>')
719                 html_index.gsub!(/~~caption_iteration~~/, xmldir.attributes['thumbnails-caption'])
720             end
721             #- cleanup temp for videos
722             system("rm -f #{dest_dir}/screenshot.jpg00000*")
723
724             #- deal with sub-albums (direct to subdirs/index.html pages)
725             xmldir.elements.each('dir') { |child|
726                 subdir = make_dest_filename(from_utf8(File.basename(child.attributes['path'])))
727                 thumbnail = "#{dest_dir}/thumbnails-#{subdir}.jpg"
728                 html_index += run_iterations(iterations)
729                 #- first look for subdirs info; if not, means there is no subdir
730                 caption = child.attributes['subdirs-caption']
731                 if caption
732                     gen_thumbnails(from_utf8(child.attributes['subdirs-captionfile']), child, [ { 'filename' => thumbnail, 'size' => $albums_thumbnail_size } ])
733                     html_index.gsub!(/~~caption_iteration~~/, caption)
734                 else
735                     gen_thumbnails(from_utf8(child.attributes['thumbnails-captionfile']), child, [ { 'filename' => thumbnail, 'size' => $albums_thumbnail_size } ])
736                     html_index.gsub!(/~~caption_iteration~~/, child.attributes['thumbnails-caption'])
737                 end
738                 html_index.gsub!(/~~image_iteration~~/, "<a href='#{make_dest_filename(subdir)}/index.html'>" + img_element(thumbnail) + '</a>')
739                 #- cleanup temp for videos
740                 system("rm -f #{dest_dir}/screenshot.jpg00000*")
741             }
742
743         else
744             html = html_refresh("thumbnails-#{default_thumbnails}.html")
745         end
746
747         html_index += close_iterations(iterations)
748         for i in html
749             i.gsub!(/~~thumbnails~~/, html_index)
750         end
751
752         ios = File.open("#{dest_dir}/index.html", "w")
753         ios.write(html)
754         ios.close
755
756         #- substitute "return to albums" correctly
757         `find '#{dest_dir}' -maxdepth 1 -name "thumbnails*.html"`.each { |thumbnails|
758             thumbnails.chomp!
759             contents = File.open(thumbnails.chomp).readlines
760             for i in contents
761                 if xmldir.elements['dir']
762                     i.sub!(/~~return_to_albums~~/, '<a href="index.html">' + utf8(_('Return to albums')) + '</a>')
763                 else
764                     if xmldir.parent.name == 'dir'
765                         i.sub!(/~~return_to_albums~~/, '<a href="../index.html">' + utf8(_('Return to albums')) + '</a>')
766                     else
767                         i.sub!(/~~return_to_albums~~/, '')
768                     end
769                 end
770             end
771             ios = File.open(thumbnails, "w")
772             ios.write(contents)
773             ios.close
774         }
775     }
776
777     msg 3, _(" all done.")
778 end
779
780 handle_options
781 check_installation
782
783 build_html_skeletons
784
785 walk_source_dir