#!/usr/bin/env perl
use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use POSIX qw(strftime);
use Text::CSV_XS;

my $q = new CGI;

# タイトルは適当に 
print $q->header(), $q->start_html( -title=>'enquate');

# ファイルを置く先 (ディレクトリ)
# ディレクトリのセキュリティ等はいちおうちゃんとしとくこと
my $outputdir = "logdir";		

my $csv = Text::CSV_XS->new({binary=>1});

# 渡されるフォーム要素の name を羅列
my @itemlist = (
	     'name',
	     'number',
	     'A1',
	     'A2',
	     );

my $accesstime = strftime "%Y/%m/%d %H:%M:%S", localtime;

my @columns = (
	       $accesstime,
	       $q->remote_host(),
	       $q->remote_addr(),
	       $q->user_agent(),
);

foreach my $value (@itemlist){
    if ($q->param($value)){
	push (@columns, $q->param($value));
    } else {
	push (@columns, ' ');
    }
}

my $status = $csv->combine(@columns);

my $timestamp = strftime "%y%m%d%H%M%S", localtime;

my $outputfn = "$outputdir/$timestamp.csv";

open(F, "> $outputfn") or die;
flock(F, 2);
print F $csv->string();
print F "\n";
close F;

# ありがとうございましたメッセージ。
my $body = <<"HTML_BODY";
Thank you !<BR>
<A HREF="/">to TopPage</A>

HTML_BODY

print qq($body);

print $q->end_html;
