## Active Perl 5.8.8

# 目的：
#   java に tee を付け、ログをとれるようにする。
#   おまけ -- title変更、カレントディレクトリ変更

use utf8;
use strict;
use warnings;
use Encode;

binmode STDOUT,':encoding(cp932)';
sub   FS {Encode::encode('cp932',shift)}
sub UTF8 {Encode::decode('cp932',shift)}

&JavaTee('gameserver/startGameServer.bat', 'l2jserver.jar');
&JavaTee('login/startLoginServer.bat'    , 'l2jlogin.jar');
exit 0;

sub JavaTee {
	my ($filepath,$l2jar) = @_;

    print $filepath,"\n";
	open FILE,'<:encoding(cp932)',FS($filepath)  or die;
	read FILE, my $bat, -s FILE;
	close FILE;

	my ($pattern, $string1, $string2);

#	if ($bat !~ m/^title /mi) {
#		$pattern = '^';
#		$string1 = '@@title %~n0';
#		$bat =~ s/$pattern/$string1\n/;
#	}

	if ($bat !~ m/cd \/d %~dp0/mi) {
		$pattern = '^';
		$string1 = '@@cd /d %~dp0';
		$bat =~ s/$pattern/$string1\n/;
	}

	$pattern = '^(java .*$)';
	$string1 = '@@move UPDATE\\'.$l2jar.' . 2>NUL
@@call L2LogRotate.bat';
	$string2 = '@@cmd /v:on /c "%JAVA% 2>&1 & echo exit /b !ERRORLEVEL! > __errorlevel.bat" |tee log/stdout.log
@@call __errorlevel.bat';
	$bat =~ s/$pattern/$string1\nSET JAVA=$1\n$string2/mi;

	open FILE,'>:encoding(cp932)',FS($filepath);
	print FILE $bat;
	close FILE;
}
