#!/usr/bin/perl

use lib 'd:/Programs/Perl/lib';
use lib 'd:/MyWebs/cgi-bin';
#use lib './lib';
#use lib '../../bin';

use strict;
#use warnings;
use Jcode;
use File::Path;
use File::Basename;
use File::Find;
use DBI;
use CGI;

use MyApplication;
use SQLDB;
use Utils;
use GetArg;
use SearchPaperDB;

BEGIN {
#	$| = 1;
	open (STDERR, ">&STDOUT");
}

#===============================================
# スクリプト大域変数
#===============================================

#===============================================
# 文字コード関係変数
#===============================================
# sjis, euc, jis, noconv
my $FileSystemCharCode = SearchPaperDB::FileSystemCharCode();
my $MySQLCharCode      = SearchPaperDB::MySQLCharCode();
my $WebCharSet         = SearchPaperDB::WebCharSet();
my $WebCharCode        = SearchPaperDB::WebCharCode();

#===============================================
# SQL関係変数
#===============================================
my $DBServer   = SearchPaperDB::MySQLServer();
my $DBUser     = SearchPaperDB::MySQLUser();
my $DBPassword = SearchPaperDB::MySQLPassword();
my $DBName     = SearchPaperDB::MySQLDBName();
my $TableName  = SearchPaperDB::MySQLPapersTableName();

#===============================================
# Applicationクラス作製
#===============================================
my $App = new MyApplication;
exit if($App->Initialize() < 0);
$App->SetDebug(0);
# 実行プログラム名（デフォルトでは$0から取得）
	my $Program = $App->Program();
# アプリケーション名
	$App->SetAppName($Program);
# バージョン
	$App->SetVersion("Ver 0.1");
# プログラムパスの設定
	my $BaseDir = '';
	my $ProgramPath = $App->SpeculateProgramPath($0, $BaseDir);
# IniFileの設定
#　　#最後の引数を1にすると、IniFileが存在しないと強制終了する
#	my $IniFile = $App->OpenIniFile($ProgramPath, 1);
#	my $IniFilePath = $App->IniFile()->IniFile();
#	print "IniFile Path: $IniFilePath\n";
#	my $MyDir = $IniFile->MyDir();

$App->SetLF("<br>\n");
$App->SetPrintCharCode("sjis");
$App->DebugPrint("Program Path: $ProgramPath\n");

#==========================================
# データベースオブジェクト作成
#==========================================
my $DB = new SQLDB;
$DB->SetApplication($App);

#==========================================
# コマンドラインオプション読み込み
#==========================================
$App->AddArgument("--Action",    "--Action: Set Action",       '');
$App->AddArgument("--DebugMode", "--DebugMode: Set DebugMode", '');
exit 1 if($App->ReadArgs(0) != 1);
my $Args = $App->Args();
my $form = new CGI;
$Args->SetCGIForm($form);
$Args->parseInput($WebCharCode);

#==========================================
# メイン関数スタート
#==========================================
my $NowTime   = time();
my $Today     = Utils::BuildDateString($NowTime);
my $IPAddress = Utils::GetIPAddress();

my $Action = $Args->GetGetArg('Action');
my $Debug  = $Args->GetGetArg('DebugMode');
$App->SetDebug($Debug);

Utils::InitHTML('Search Results', $WebCharSet, "_blank");
$App->DebugPrint("Action: $Action\n");
$App->print("東京工業大学\n");

my ($dbh, $dbsth) = $DB->Open($DBServer, "$DBUser", $DBPassword, $DBName, "sjis", 0, 1);
if(!$DB->dbh()) {
	exit;
}

my $sql = "select * from $TableName where FirstAuthor like '%toda%';";
$App->print("sql: $sql\n");
my ($sth, $ret) = $DB->Execute($sql, 0, 1);
my $nFields = $DB->nFields();
print "nFields: $nFields\n";
my $pFieldNames = $DB->pFieldNames();
for(my $i = 0 ; $i < $nFields ; $i++) {
	my $name = $pFieldNames->[$i];
	$App->print("$i: $name\n");
}

my $nHits = $DB->nHits();
print "nHits: $nHits\n";
#$nHits = 0;
for(my $i = 0 ; $i < $nHits ; $i++) {
	my %hit = $DB->GetNextHit();
	$App->print("Authors: $hit{Authors}\n");
	$App->print("  Journal: $hit{Journal}\n");
	$App->print("  Year: $hit{Year}\n");
}

$DB->Close();

exit;

#===============================================
# スクリプト終了
#===============================================

#==========================================
# &Subroutines
#==========================================

