#!/usr/bin/env ruby
# coding: utf-8
require 'cgi'
require 'kconv'
c = CGI.new(:accept_charset => "UTF-8")

def zikoku(req, sel, fir, time)
  hoge = req + sel
  if  i = fir[hoge].find { |n| time <= n.to_i }
    printf("%4d発です。<br>", i)
    i = i.to_i
    y = fir[hoge].find { |m| i < m.to_i }
    if y == nil
      print "それ以降は翌日です。"
    elsif y > ""
      printf("その次は%4d発になります。<br>", y)
    end
    print "ご利用感謝致します。\n"
  else 
    print "翌日以降です。\n"
  end
end

q = Hash.new()
open("zikokuhyo.txt", "r") do |stst|
  while line = stst.gets
    line = line.toutf8
    if /^(\S+)\s(.*)/ =~ line
      q[$1] = $2.split
    end
  end
end

rosen = Hash.new()
open("rosen.txt", "r") do |ro|
  while line = ro.gets
    line = line.toutf8
    if /^(\S+)\s(.*)/ =~ line
      rosen[$1] = $2.split
    end
  end
end

ekime = {}
n = 0
open("ekime.txt", "r") do |yomi|
  while line = yomi.gets
    if /^(\S+)/ =~ line.toutf8
      ekime[n] = $1
      ekime[n] = ekime[n].toutf8 
      n += 1
    end
  end
end
n1 = n
n = 0
sel = ARGV[0]
req = c["start"].toutf8
sel = c["goal"].toutf8

puts"Content-type: text/html; charset=utf-8\n
<!DOCTYPE html>
<html lang = 'ja'>
<head><title>runrun bus</title></head>"
#puts"<body><h1><a href=runrunbus.rb>るんるんばすをけんさく</a></h1>
puts"<a href=\"runrunbus.rb\"><img src=\"bus.png\" alt=\"bus\"  width=100%></a>
<form method=\"GET\" action=\"./runrunbus.rb\">
<link rel=\"stylesheet\" type=\"text/css\" href=\"prac.css\">"

puts"<p>乗りたい停留所<select name=\"start\">\n"
while n < n1
  printf("<option>%s", ekime[n])
  n += 1
end
puts("</select></p>")
if sel == ""
  n = 0
  puts"<p>目的の停留所<select name=\"goal\">\n"
  while n < n1
    if sel == ekime[n]
      printf("<option selected>%s", ekime[n])
      n += 1
    else
      printf("<option>%s", ekime[n])
      n += 1
    end
  end
end

print"</select>\n<!--乗りたい時間 <input name=\"hour\">:<input name=\"minute\">-->"
print"</p>\n<p>乗りたい時間<select name = \"hour\">\n"
for h in 0..23 do
  print("<option>", h)
end
print"</select>:<select name = \"minute\">\n"
for m in 0..59 do
  if m < 10
    m = m.to_s
    m += "0"
    m = m.reverse
  end
  print("<option>", m)
end
print"</select></p>"
printf("<input type=\"hidden\" name=\"goal\" value=\"%s\">\n</p>", sel)
print"<p>
<input type=\"submit\" value=\"OK\">
<input type=\"reset\" value=\"reset\">"
h = c["hour"].to_i
m1 = c["minute"]
m = c["minute"].to_i
a = h * 100 + m
if req > ""
    print "<div class='bus'></div>\n"
    print "<h1>るんるんバスの検索結果</h1>\n"
    print "<p>#{req}の#{sel}方面行きで</p>\n"
    print "<p>#{h}:#{m1}時点から一番近いのは</p>\n"
    print "<p>"
    printf("%s</p>\n", zikoku(req, sel, q, a))
end
print "<br><a href=runrunbus.rb>TOPへ</a><br>"
print "<a href=http://roy/~c115163/mycgi/runbakensaku.html>観光地検索へ</a>"
puts"<iframe width=\"100%\" height=\"600px\" frameBorder=\"0\" src=\"https://umap.openstreetmap.fr/ja/map/map_116946?scaleControl=false&miniMap=false&scrollWheelZoom=false&zoomControl=true&allowEdit=false&moreControl=true&searchControl=null&tilelayersControl=null&embedControl=null&datalayersControl=true&onLoadPanel=undefined&captionBar=false\"></iframe><p><a href=\"https://umap.openstreetmap.fr/ja/map/map_116946\">フルスクリーン表示</a></p>"
print"</form>\n</body>\n</html>"
