#!/usr/local/bin/perl
###############################################################################
# Copyright (C) 2000 T.Kimata <t_kimata@nk.rim.or.jp>
# http://www.nk.rim.or.jp/~t_kimata/cgi/
# 
# All rights reserved.
# 
# This software is provided 'as-is', without any express or implied warranty.
# In no event will the authors be held liable for any damages arising from the
# use of this software.
# 
# Permission is granted to anyone to use this software for any purpose,
# including commercial applications, and to alter it and redistribute it
# freely, subject to the following restrictions:
# 
# 1. The origin of this software must not be misrepresented; you must not claim
# that you wrote the original software. If you use this software in a product,
# an acknowledgment in the product documentation would be appreciated but is
# not required.
# 
# 2. Altered source versions must be plainly marked as such, and must not be
# misrepresented as being the original software.
# 
# 3. This notice may not be removed or altered from any source distribution.
###############################################################################
# (SS)C-BOARD v3.8                            by N.Hirai / atman@skullysoft.com
#
# 　　Sorry, these softwares are prepared for Japanese residents only.
# 　　And it is constituted only in Japanese.
# 
# C-BOARDのオリジナルはt_Kimata氏が開発されました、
# 当バージョンはスカリーソフト( http://www.skullysoft.com/ )により、
# 不具合の修正等を行った改造版です。
# 当バージョンのライセンスはオリジナルに準拠します。
# オリジナルのライセンスは冒頭の通りです。
# 著作権はオリジナルソース部分が T.Kimata 氏、改造部分は改造者（複数）にありま
# す。
#
###############################################################################


package main;
#use strict;

#┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
#┃   下準備
#┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
BEGIN {	
	
	# カレントディレクトリの移動（IIS, mod_perl用）
	#chdir 'c:/www/develop/c-board/c-board';

	require './app/lib/cmn.pl';
	require './app/lib/cgi.pl';
	require './app/commander.pl';
	require './app/cmd/bse.pl';

}


#┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
#┃   実行
#┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━


#── 初期化
&COMMANDER::init;

#── 実行前処理
&COMMANDER::do_before_command;

#── コマンド実行
&COMMANDER::command;

exit;


#┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
#┃   後始末
#┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
END {
	map(&::unlock($_), keys %{$::FLAG{'lock'}});
}



#━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

#┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
#┃   オートローダー
#┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
sub AUTOLOAD {

	$::FLAG{'autoload'}++ && exit; # 無限ループ防止

	eval <<'__SUB__';
#┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
#┃   ロック
#┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
	sub lock {
		my $mode = shift;
		return(&COMMANDER::sub_lock($mode));
	}

#┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
#┃   ロック解除
#┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
	sub unlock {
		my $mode = shift;
		&COMMANDER::sub_unlock($mode);
	}

#┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
#┃   デバッグ用の初期化
#┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
	sub debug_init {
		eval{'$::FLAG{\'start\'} = [(times)[0, 1]]'};
		length($ENV{'HTTP_USER_AGENT'}) && return;
		$ENV{'HTTP_USER_AGENT'} ||= 'Mozilla/4.0 (Win98)';
		$ENV{'QUERY_STRING'} ||= "cmd=$ARGV[0];id=test";
		# and more...
	}

#┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
#┃   デバッグ用情報を記録
#┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
	sub debug {
		my $name = shift; my $r_value = shift;
		&::lock('dbg') or return;
		open(DBG, "+>>./$CNF::place{'data'}{'path'}/debug.cgi") or return;
		print DBG '━' x 38, "\n■$name\n${$r_value}\n"; close(DBG);
		&::unlock('dbg');
		return 0;
	}

#┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
#┃   エラー表示
#┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
	sub error {
		my $r_error = shift;

		(++$::FLAG{'error'} > 25) && exit; my($file, $line) = (caller)[1, 2];
		my $caller = "［$file($line)］";
		($::FLAG{'debug'} == 2) or ($caller = "<!-- $caller -->");
		if( ! $::SYS{'path_tmp'} ) { $::SYS{'path_tmp'} = "./tmp" }
		if ($::FLAG{'file'}) {
			foreach (@{$::FLAG{'file'}}) {
				unlink("$::SYS{'path_tmp'}/${$_}{'name'}");
			}
			undef $::FLAG{'file'};
		}
		$::FLAG{'template'} or &COMMANDER::skin;
		require "$::FLAG{'template'}/err.skin.pl";
		if ($::FLAG{'http_header'}) { &DESIGN::small_error($r_error, $caller);}
		else {
			select(STDOUT); &BASE::head2('ror');
			&DESIGN::error($r_error, $caller); &BASE::foot2('ror'); exit;
		}
	}
__SUB__

	our $AUTOLOAD;

	length($@) && die $@; 
	my $name = ($AUTOLOAD =~ /^main::(.+)$/)[0];

	if (!defined &{$name}) {
		&::error(\"定義されていない関数($AUTOLOAD)が呼ばれました。"); 
		exit;
	}
	goto &{'main::' . $name};

}

