#!/usr/bin/ruby -w

require 'rexml/document'
require "~/projects/ruby/picasa-gen/picasa_album.rb"

# Modify the file below to change the default thumbnail size
# .picasa/drive_c/Program\ Files/Picasa2/web/templates/xml/index.tpl

#------------------------------------------------------------
#-----------------------------------------------------------
class Gallery
  include PicasaAlbumHelper

  def initialize
    @album = PicasaAlbum.create_album
  end


# ----------------------------------------------------------
  def index
    index_file = IO.readlines("index.tpl")
    html = String.new
    root = @album
    album_keys = root.album_keys

	shtml = File.new( "index.html" , "w")
	$oldout = $stdout
	$stdout = shtml

    index_file.each{ |line|

        album_keys.each{|key|
            if (line =~ /__#{key}__/) then
                line.sub!(/__#{key}__/, root.hash[key])
            end
        }
        html = line

            if line.include? "<!--THUMBNAILS-->"
                html = line.sub!("<!--THUMBNAILS-->","")
                html = index_thumbs
            end
        puts html
    }
	$stdout = $oldout
	shtml.flush
	shtml.close

  end

# ----------------------------------------------------------
  def index_thumbs
    thumb_file = IO.readlines("thumbs.tpl")
    target = 1
    html = String.new

    item_keys = @album.item_keys

    @album.entries.each do |e|
        thumb_file.each{ |line_base|
            item = e
            line = String.new(line_base)
            item_keys.each{|key|
                if (line =~ /__#{key}__/) then
                    line.sub!(/__#{key}__/, item.hash[key])
                end
                line.sub!(/__target__/, "target#{target.to_s}.html")
            }
            html += line
        }
        target = target + 1
	end
    return html
  end

# ----------------------------------------------------------
  def pages
    image_file = IO.readlines("image.tpl")
    root = @album
    album_keys = root.album_keys
    item_keys = root.item_keys

    html = String.new
    target = 1

    root.entries.each do |e|

	shtml = File.new( "target#{target}.html" , "w")
	$oldout = $stdout
	$stdout = shtml

        image_file.each{ |line_base|
            line = String.new(line_base)
            item = e

            album_keys.each{|key|
                if (line =~ /__#{key}__/) then
                    line.sub!(/__#{key}__/, root.hash[key])
                end
            }
            item_keys.each{|key|
                if (line =~ /__#{key}__/) then
                    unless item.hash[key].nil? then
                        line.sub!(/__#{key}__/, item.hash[key])
                    else
                        line.sub!(/__#{key}__/, "")
                    end
                end
                line.sub!(/__target__/, "target#{target.to_s}.html")
            }

            html = line

            puts html
        }
        target += 1

	$stdout = $oldout
	shtml.flush
	shtml.close

    end

  end

end



    g = Gallery.new
    g.index
    g.pages

