#!/usr/bin/ruby1.8

require "cgi"


# ---- form class
class EntryForm
  def initialize(params)
    # - page
    if (params['Page'].empty?) then
      @page = 0
    else
      @page = params['Page'][0].to_i
    end
    # - mail address
    if (params['MADR'].empty?) then
      @mail_addrss = ''
    else
      @mail_address = params['MADR'][0]
    end
    # - name
    if (params['NAME'].empty?) then
      @name = ''
    else
      @name = params['NAME'][0]
    end
    # - build service
    if (params['BUILD'].empty?) then
      @build = ''
    else
      @build = params['BUILD'][0]
    end
    # - select package
    if (params['PACK'].empty?) then
      @pack = ''
    else
      @pack = params['PACK'][0]
    end
    # - send area
    if (params['AREA'].empty?) then
      @area = ''
    else
      @area = params['AREA'][0]
    end
    # - payment
    if (params['PAYMENT'].empty?) then
      @payment = ''
    else
      @payment = params['PAYMENT'][0]
    end
  end

  # -- page
  def GetPage
    @page
  end

  # -- send mail
  def SendMail
    message = ""
    message << "Mail Address:\n"
    message << @mail_address
    message << "\n\n"
    message << "Name:\n"
    message << @name
    message << "\n\n"
    message << "Build:\n"
    message << @build
    message << "\n\n"
    message << "Pack:\n"
    message << @pack
    message << "\n\n"
    message << "Area:\n"
    message << @area
    message << "\n\n"
    message << "Payment:\n"
    message << @payment
    message << "\n\n"
    #
    open("| /usr/sbin/sendmail rero2@yuumu.org", "w") do |mail|
      mail.print("From: kanade_entry@yuumu.org\n")
      mail.print("To: rero2@yuumu.org\n")
      mail.print("Subject: To Kanade Entry\n")
      mail.print("\n")
      mail.print message
    end
  end

  # -- invisible form
  def InvisibleForm
    printf("<input name=\"MADR\" type=\"hidden\" id=\"MADR\" value=\"%s\">\n", @mail_address)
    printf("<input name=\"NAME\" type=\"hidden\" id=\"NAME\" value=\"%s\">\n", @name)
    printf("<input name=\"BUILD\" type=\"hidden\" id=\"BUILD\" value=\"%s\">\n", @build)
    printf("<input name=\"PACK\" type=\"hidden\" id=\"PACK\" value=\"%s\">\n", @pack)
    printf("<input name=\"AREA\" type=\"hidden\" id=\"AREA\" value=\"%s\">\n", @area)
    printf("<input name=\"PAYMENT\" type=\"hidden\" id=\"PAYMENT\" value=\"%s\">\n", @payment)
  end

  # -- confirm
  def Confirm
    printf("<p>連絡先メールアドレス : %s</p>\n", @mail_address)
    printf("<p>お名前 : %s</p>\n", @name)
    if (@build =~ /NO/) then
      disp_build = "希望しません(キットで送る)"
    else
      disp_build = "希望します(+1,000円)"
    end
    printf("<p>組み立て希望 : %s</p>\n", disp_build)
    if (@pack =~ /OK/) then
      disp_pack = "良いです(送料 500円)"
    else
      disp_pack = "箱にしてください(送り先で送料が決まります)"
    end
    printf("<p>EXPACK500で良いか : %s</p>\n", disp_pack)
    printf("<p>宛先都道府県 : %s</p>\n", @area)
    printf("<p>支払い方法 : </br>%s</p>\n", @payment)
  end
end


# ---- stock class
class Stock
  def initialize(stock_file)
    @stock = 0
    fp = open("stock.dat", "r")
    fp.each do |line|
      if line =~ /[0-9]+/ then
        @stock = line.to_i
      elsif line =~ /\*/
        @stock -= 1
      end
    end
    fp.close
  end

  def GetStock
    @stock
  end

  def PutStock
    fp = open("stock.dat", "a")
    fp << "*\n"
    fp.close
  end
end



# ---- page subroutine
def goto_next
  print'<form id="form1" name="form1" method="post" action="kanade_entry.cgi">'
  print'<input name="Page" type="hidden" id="Page" value="1" />'
  print'<p><input type="submit" name="DONE" id="DONE" value="申し込みフォームへ" /></p>'
  print'</form>'
end

def soldout
  print '<div class="red"><p>申込数が予定数量に達したため受付を終了します。<br />ありがとうございました。</p></div>'
end

def output_body(form, rest)
  pagefile = ''
  # - page select
  case form.GetPage
  when 1
    pagefile = 'form.html'
  when 2
    pagefile = 'confirm.html'
  when 3
    pagefile = 'form_sent.html'
    form.SendMail
    rest.PutStock
  else
    pagefile = 'information.html'
  end
  # - output
  fp = open(pagefile, "r")
  fp.each do |line|
    if line =~ /\%\%rest\%\%/ then
      printf("<p> %d 個</p>\n", rest.GetStock);
    elsif line =~ /\%\%confirm\%\%/ then
      form.InvisibleForm
      form.Confirm
    elsif line =~ /\%\%next\%\%/ then
      if rest.GetStock > 0 then
        goto_next
      else
        soldout
      end
    else
      print line
    end
  end
  fp.close
end




# ---- main routine
cgi = CGI.new
params = cgi.params

get_form = EntryForm::new(params)
rest = Stock::new('stock.dat')

# ---- generate html
print "Content-type: text/html\n\n"

fp = open("base.html", "r")
fp.each do |line|
  if line =~ /\%\%body\%\%/ then
    output_body(get_form, rest)
  else
    print line
  end
end
fp.close

