#!/usr/bin/env ruby
#coding: euc-jp

require 'cgi'

cgi = CGI.new(:accept_charset => "EUC-JP")
print("Content-type: text/html; charset=EUC-JP\n\n")

bus = Hash.new

direc = cgi["direc"]
dep = cgi["dep"]
arr = cgi["arr"]
hour = cgi["hour"]
min = cgi["min"]
which = cgi["which"]

if direc == "e"
  open("zikoku1.txt","r:euc-jp") do |zikoku|
    while line = zikoku.gets 
      if /(\S*),(\S*),(\S*),(\S*),(\S*),(\S*),(\S*),(\S*),(\S*),(\S*)/ =~ line
        bus[$10.to_s] = [$1.to_s,$2.to_s,$3.to_s,$4.to_s,$5.to_s,
                         $6.to_s,$7.to_s,$8.to_s,$9.to_s]
      end      
    end
  end
  
elsif direc == "k"
  open("zikoku2.txt","r:euc-jp") do |zikoku|
    while line = zikoku.gets
      if /(\S*),(\S*),(\S*),(\S*),(\S*),(\S*),(\S*),(\S*),(\S*),(\S*)/ =~ line
        bus[$10.to_s] = [$1.to_s,$2.to_s,$3.to_s,$4.to_s,$5.to_s,
                         $6.to_s,$7.to_s,$8.to_s,$9.to_s]
      end      
    end
  end
end

time = hour + min

# p time

print("<html>\n")
print("<head>\n")
print("<title>瑛絆</title>\n")
print("</head>\n")
print("<body>\n")
print("<link rel=\"stylesheet\" href=\"bus.css\" type=\"text/css\">\n")
print("<h1>腹綢瑛絆</h1><br>\n")
print("<img src=\"bus.png\" align=\"right\">")
a = 0
if which == "t"
  while true
    if time.to_i < bus[arr][0].to_i
      if /(\S\S)(\S\S)/ =~ bus[dep][8]
        dhour = $1
        dmin = $2
      end
      if /(\S\S)(\S\S)/ =~ bus[arr][8]
        ahour = $1
        amin = $2
      end
      print("<h2>曇樂</h2>")
      printf("<p> %s %s:%s\n</p>\n",dep ,dhour, dmin)
      print("<h2>嫦樂</h2>")
      printf("<p> %s %s:%s\n</p>\n",arr ,ahour, amin)
      break
    elsif a >= 8 || time.to_i > bus[arr][a].to_i && time.to_i < bus[arr][a+1].to_i
      if /(\S\S)(\S\S)/ =~ bus[dep][a]
        dhour = $1
        dmin = $2
      end
      if /(\S\S)(\S\S)/ =~ bus[arr][a]
        ahour = $1
        amin = $2
      end
      print("<h2>曇樂</h2>")
      printf("<p> %s %s:%s\n</p>\n",dep ,dhour, dmin)
      print("<h2>嫦樂</h2>")
      printf("<p> %s %s:%s\n</p>\n",arr ,ahour, amin)
      break
    else 
      a += 1 
    end
  end
elsif which == "s"
  while true
    if time.to_i < bus[dep][0].to_i || time.to_i > bus[dep][8].to_i
      if /(\S\S)(\S\S)/ =~ bus[dep][0]
        dhour = $1
        dmin = $2
      end
      if /(\S\S)(\S\S)/ =~ bus[arr][0]
        ahour = $1
        amin = $2
      end
      print("<h2>曇樂</h2>")
      printf("<p> %s %s:%s\n</p>\n",dep ,dhour, dmin)
      print("<h2>嫦樂</h2>")
      printf("<p> %s %s:%s\n</p>\n",arr ,ahour, amin)
      break
    elsif time.to_i > bus[dep][a].to_i && time.to_i < bus[dep][a+1].to_i
      if /(\S\S)(\S\S)/ =~ bus[dep][a+1]
        dhour = $1
        dmin = $2
      end
      if /(\S\S)(\S\S)/ =~ bus[arr][a+1]
        ahour = $1
        amin = $2
      end
      print("<h2>曇樂</h2>")
      printf("<p> %s %s:%s\n</p>\n",dep ,dhour, dmin)
      print("<h2>嫦樂</h2>")
      printf("<p> %s %s:%s\n</p>\n",arr ,ahour, amin) 
      break
    else 
      a += 1 
    end
  end
end
print("<a href=\"http://skip.koeki-prj.org/bus_cgi/home.html\">枑月</a>
<a href=\"http://skip.koeki-prj.org/\">SKIP</a>")
print("</body>\n")
print("</html>\n")
