#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require 'sqlite3'
require 'cgi'
require 'kconv'

db = "../viewerdb/db/viewer_prc.sq3"
c = CGI.new(:accept_charset=>'utf-8')
print "Content-type: text/html; charset=utf-8\n\n"

@mapdb = SQLite3::Database.new(db)
@mapdb.results_as_hash = true

def content(file)
  open(file){|f| f.read.toutf8}
end
def cat(file)
  print(content(file))
end
def db2options(selection)
  sql = sprintf("SELECT DISTINCT %s", selection)
  opts = ""
  @mapdb.execute(sql) do |row|
    opts += sprintf("<option>%s</option>\n", row[0])
  end
  opts
end
ef = content("ent-form.html")
c.params['name']=['山居倉庫']
for v in %w(ename name addr latlon)
  ef.gsub!("__"+v.upcase+"__", c.params[v][0]||"")
end
cat("ent-header.html")
print ef.gsub(/__{(.*)}__/) {
  db2options($1)
}
cat("footer.html")
