#!/usr/bin/env ruby22
# -*- coding: utf-8 -*-

require 'sqlite3'
require 'kconv'
require 'cgi'
require 'csv'

c = CGI.new(:tag_maker => "html5", :accept_charest => "UTF-8")
db = SQLite3::Database.new("sql/main.sq3")
csv = "list.csv"

sql = "insert into test values(?, ?, ?, ?, ?, ?, ?, ?, ?)"

toi = "insert into toi values (?, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)"
item = "insert into goods values (?, 0, 0, 0, 0, 0, 0)"

name = c["name"]
pswd = c["pswd"]
cle1 = "CREATE TABLE IF NOT EXISTS test(id, name, cal, age, sex, pswd, exp, pt, lv)"
cle2 = "CREATE TABLE IF NOT EXISTS toi(id, q1, q2, q3, q4, q5, q6, q7, q8, q9, q10)"
cle3 = "CREATE TABLE IF NOT EXISTS goods(id, g1, g2, g3, g4, g5, g6)"
cle4 = "CREATE TABLE IF NOT EXISTS time(cd, now, id)"
cle5 = "CREATE TABLE IF NOT EXISTS num(cd, id)"
cle6 = "CREATE TABLE IF NOT EXISTS pass(id, pass, name)"

cle7 = "CREATE TABLE IF NOT EXISTS zukan(no, name, tah, lv2, lv3, lv4, lv5)"
cle8 = "CREATE TABLE IF NOT EXISTS box(id, no1, e1, t1, tm1, no2, e2, t2, tm2, no3, e3, t3, tm3)"
ins8 = "insert into box values(?,?,0,?,?,0,0,0,0,0,0,0,0)"

ins4 = 'insert into time values(?, 61, "n")'
ins5 = 'insert into num values("aa",0)'
ins6 = 'insert into pass values(?, ?, ?)'
num = "select id from num where cd = ?"
numup = "update num set id = ? where cd = ?"

db.execute(cle1)
db.execute(cle2)
#db.execute(cle3)
db.execute(cle4)
db.execute(cle5)
db.execute(cle6)
db.execute(cle7)
db.execute(cle8)
if name == "createman789789789"
  ("a".."j").each{|x|db.execute(ins4,x)}
  db.execute(ins5)
  #system `sqlite3 -separator , /sql/main.sq3 ".import list.csv zukan"` #(ins7)
  CSV.foreach(csv) do |row|
    db.execute("INSERT INTO zukan VALUES(?,?,?,?,?,?,?)",*row)
  end
end

name = c["name"]
namae = "#{name}"
cal = c["cal"]
age = c["age"]
sex = c["sex"]
aria = c["aria"]


id = c.cookies["id"][0]
pass = c.cookies["pass"][0]
no1 = db.execute("select no from zukan where name = ?",cal)[0][0]
t1 = db.execute("select tah from zukan where name = ?",cal)[0][0]


if id == nil
  number = db.execute(num, "aa")
  begin
    id = number[0][0].to_i + 1
  rescue
    ("a".."j").each{|x| db.execute(ins4,x)}
    #  db.execute(ins4, "b")
    #  db.execute(ins4, "c")
    #  db.execute(ins4, "d")
    #  db.execute(ins4, "e")
    #  db.execute(ins4, "f")
    #  db.execute(ins4, "g")
    #  db.execute(ins4, "h")
    #  db.execute(ins4, "i")
    #  db.execute(ins4, "j")
    db.execute(ins5)
    id = number[0][0].to_i + 1
  end
  exp = 0
  pt = 0
  lv = 1
  cd = "aa"
  if pass == nil
    pass = id
  end
  db.execute(ins6, id, pass, name)
  db.execute(sql, id, name, cal, age, sex, aria, exp, pt, lv)
  db.execute(toi, id)
  #  db.execute(item, id)
  db.execute(numup, id, cd)
  db.execute(ins8,id,no1,t1.to_i/5,t1.to_i/5)
end
#  srand(id.to_i)  
#  id2 = rand(1000000)
id2 = pass



expires = "SUN, 5, MAR 2017 00:00:00 GMT"

printf("Content-type: text/html; charset=UTF-8
Set-Cookie:id=%s ;expires=%s
Set-Cookie:pass=%s ;expires=%s\n\n", id, expires, id2, expires)

printf(<<_EOS_, name, id.to_i)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>

<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
<meta name="format-detection" content="telephone=no">
<link media="only screen and (max-device-width:480px)" href="smart.css" type="text/css" rel="stylesheet" />
<link media="screen and (min-device-width:481px)" href="profeel.css" type="text/css" rel="stylesheet" />




<link rel="shortcut icon" href="./img/cabage.png"/>
<title>Registering Account</title>

</head>
<body>
<h1>Miyazac Quest</h1>
<h2>登録完了!</h2>
<p>よくきた_ %s _よ!</p>
<p>おまえのIDは%04dだ!</p>
<div class="f1"><a href="prof.rb"><div class="lets"><span>始めよう!</span></div></a></div>
<form method="POST" type="text">
</form>
</body></html>
_EOS_
if name == "createman987987987"
filename ='sql/main.sq3'
File.delete filename
end
