#!/usr/local/bin/ruby

require 'rubygems'
require 'erb'
require 'RMagick'

class EasyAlbum

  def initialize
    @erb = ERB.new(IO.readlines('tpl/index.html').join);
  end

  def execute
    @imgs = Array.new
    Dir.glob('images/*.jpg') do |imgfile|
      thumbfile = 'thumb/' + File.basename(imgfile)
      if ! File.exist?(thumbfile)
        tmpimg = Magick::Image.read(imgfile).first
        asp    = tmpimg.columns.to_f / tmpimg.rows.to_f
        thumb  = tmpimg.scale(100 * asp, 100)
        thumb.write thumbfile
      end
      img_rec = Array[thumbfile, imgfile]
      @imgs  << img_rec
    end
    print "Content-Type: text/html; charset=EUC-JP\n\n"
    @erb.run(binding)
  end

end

EasyAlbum.new.execute
