## Active Perl 5.8.8
use utf8;
use warnings;
use strict;
binmode STDOUT,'encoding(cp932)';

open CSV, '<:utf8', 'temp/itemname-j.txt'  or die $!;
my $mtime = (stat CSV)[9];

open SQL, '>:utf8', 'itemname_ja.sql'  or die $!;
print SQL "--
-- from 'itemname-j.dat'
--
DROP TABLE IF EXISTS itemname_ja;
CREATE TABLE itemname_ja (
	 id INTEGER NOT NULL
	,name VARCHAR(70) DEFAULT ''
	,PRIMARY KEY (id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

";

my $count = 0;
my $delim = ' ';
<CSV>; # skip header
while (not eof CSV) {
	my $buf = <CSV>;
	chomp $buf;
	my @field = split /\t/, $buf;
	foreach (@field) {s/^[ua],//; s/\\0//}
	my ($id,$name) = ($field[0],$field[1]);
	$name =~ s/'/''/g;
	print "$id\r";

	if (++$count > 500) {
		print SQL ";\n";
		$count = 1;
	}
	if ($count == 1) {
		print SQL "INSERT INTO itemname_ja (id,name) VALUES\n";
		$delim = ' ';
	}
	print SQL $delim,"($id,'$name')\n";
	$delim = ',';
}
print SQL ";\n";
print SQL "
-- Update for Japanese.
UPDATE armor,itemname_ja SET armor.name = itemname_ja.name
 WHERE armor.item_id = itemname_ja.id;
UPDATE etcitem,itemname_ja SET etcitem.name = itemname_ja.name
 WHERE etcitem.item_id = itemname_ja.id;
UPDATE weapon,itemname_ja SET weapon.name = itemname_ja.name
 WHERE weapon.item_id = itemname_ja.id;

-- DROP TABLE itemname_ja;
";
close SQL;
utime $mtime,$mtime,'itemname_ja.sql';

close CSV;
print "\n";
