#!/usr/bin/env ruby

require 'commit'
require 'nkf'

SITE = 'ht'

@com = Commit.new

def add_html(html)
  path = html.sub(/^www12.plala.or.jp\/ksp\//, '')
  title = ''
  File.open(html) do |ifile|
    ifile.each do |line|
      if (line =~ /<title>([^<]+)<\/title>/)
        title = NKF.nkf('-w', $1)
      end

      while (line.sub!(/<img alt="([^"]+)" class="([^"]+)" src="([^"]+)"/, ''))
        f = $1.gsub(/&lt;/, '<').gsub(/&gt;/, '>').gsub(/&amp;/, '&')
        @com.add(f, SITE, path, title)
      end
    end

    print "#{html} #{NKF.nkf('-e', title)}\n"
  end

end

def add_dir(d)
  Dir.open(d) do |dir|
    dir.each do |e|
      next if e =~ /^\./
      e = "#{d}/#{e}"
      if (e =~ /\.html$/)
        add_html(e)
      elsif (File.directory?(e))
        add_dir(e)
      end
    end
  end
end

t = ARGV[0]
if (t =~ /\.html$/)
  add_html(t)
elsif (File.directory?(t))
  add_dir(t)
else
  print "#{t}: ???\n"
end

@com.close
