#!/usr/bin/env ruby25
# coding: utf-8
Encoding.default_external = "UTF-8"
require 'kconv'
require 'cgi'

def cat(file)
  print IO.readlines(file).join
end

def cat_message(file)
  IO.readlines(file).join.
    gsub("__NAME__", $name_c).
    gsub("__RUBY__", $ruby_c).
    gsub("__SEI__", $sei_c).
    gsub("__EMAIL__", $email_c).
    gsub("__TEL__", $tel_c).
    gsub("__ADDRESS__", $address_c).
    gsub("__SCHOOL__", $school_c).
    gsub("__GRADE__", $grade_c).
    gsub("__CLASS__", $class_c).
    gsub("__NUMBER__", $number_c).
    gsub("__SCHEDULE__", $schedule_c).
    gsub("__MESSAGE__", $message_c).
    gsub("__REF__", $ref_num)
end

def sendmail(command, file)
  open(command, "w") do |mail|
    mail.print file.tojis
  end
end

print "Content-type: text/html; charset=utf-8\n\n"
cat("../header.html")

begin
  c = CGI.new(:accept_charset => "UTF-8")

  $name_c = c["name"].strip
  $ruby_c = c["ruby"].strip
  $sei_c = c["sei"].strip
  $email_c = c["email"].strip
  $tel_c = c["telephone"].strip
  $address_c = c["address"].strip
  $school_c = c["school"].strip
  $grade_c = c["grade"].strip
  $class_c = c["class"].strip
  $number_c = c["number"].strip
  $schedule_c = c["schedule"].strip
  $message_c = c["message"].strip
  $submit_c = c["send"].strip
  $ref_num = c[""].strip
  check_c = c["check"].strip
  remail_c = c["remail"].strip

  if $submit_c == ""
    cat("../form-initial.html")

  elsif $submit_c == "1"
    if remail_c > ""
      $email_c = remail_c
    end
    sendmail_test = "| Mail -s 'Rubyてらこった テストメール' " + $email_c
    m = cat_message("../send_test.txt")
    ENV["QMAILUSER"]="c116092"
    ENV["QMAILHOST"]="h.koeki-u.ac.jp"
    t=Thread.new do
      sendmail(sendmail_test, m)
    end
    puts"<h2>確認画面</h2>"
    print cat_message("../form-confirm.html")
    t.join

  elsif $submit_c == "2"
    if check_c == ""
      puts"<h2>確認画面</h2>"
      puts"<p><em>テストメールを確認したらチェックボックスにチェックをつけてください。</em></p>"
      print cat_message("../form-confirm.html")
    else

      if $sei_c == "男"
        sei_num = "1"
      else
        sei_num = "2"
      end

      case $school_c
      when /\S*泉\S*/
        school_num = "01"
      when /\S*一條\S*/
        school_num = "02"
      when /\S*亀ヶ崎\S*/
        school_num = "03"
      when /\S*黒森\S*/
        school_num = "04"
      when /\S*琢成\S*/
        school_num = "05"
      when /\S*田沢\S*/
      school_num = "06"
      when /\S*鳥海\S*/
        school_num = "07"
      when /\S*十坂\S*/
        school_num = "08"
      when /\S*西荒瀬\S*/
        school_num = "09"
      when /\S*新掘\S*/
        school_num = "10"
      when /\S*浜田\S*/
        school_num = "11"
      when /\S*浜中\S*/
        school_num = "12"
      when /\S*[^南]平田\S*/
        school_num = "13"
      when /\S*広野\S*/
        school_num = "14"
      when /\S*富士見\S*/
        school_num = "15"
      when /\S*松原\S*/
        school_num = "16"
      when /\S*松山\S*/
        school_num = "17"
      when /\S*松陵\S*/
        school_num = "18"
      when /\S*南平田\S*/
        school_num = "19"
      when /\S*宮野浦\S*/
        school_num = "20"
      when /\S*八幡\S*/
        school_num = "21"
      when /\S*若浜\S*/
        school_num = "22"
      else
        school_num = "23"
      end

      case $grade_c
      when "小学5年生"
        grade_num = "5"
      else
        grade_num = "6"
      end

      case $class_c
      when /\S*(\d{1})\S*/
        class_num = $1
      else
        class_num = "1"
      end

      case $number_c
      when /\S*(\d{2})\S*/
        number_num = $1
      when /\S*(\d{1})\S*/
        number_num = "0" + $1
      else
        number_num = "99"
      end

      case $schedule_c
      when /^A/
        sch = "A"
      else
        sch = "B"
      end

      $ref_num = sei_num + school_num + grade_num + class_num + number_num + sch
      host = "c116092@h.koeki-u.ac.jp"
      sendmail_app = "| Mail -s 'Rubyてらこった お申し込み完了メール' " + $email_c
      sendmail_host = "| Mail -s 'Rubyてらこった 申込みメール' " + host
      m1 = cat_message("../send_app.txt")
      m2 = cat_message("../send_host.txt")
      ENV["QMAILUSER"]="c116092"
      ENV["QMAILHOST"]="h.koeki-u.ac.jp"
      t=Thread.new do
        sendmail(sendmail_app, m1)
        sendmail(sendmail_host, m2)
      end

      print cat_message("../form-submit.html")
      t.join
    end
  else
    cat "../error.html"
  end

rescue
  cat "../error.html"
end
