#!/usr/bin/perl

use strict 'refs';
use lib "../libs";
use vars qw( $op $mod $gui $dbp $id $apply $MemberBase );
use Genel::Site;


$dbp->{USEYEAR} = 1;
$dbp->{USESECTION} = 0;
$dbp->{MAINOP}  = "main_index";
$dbp->{SITEURL} = "$dbp->{BASEURL}/index.pl";

if($dbp->is_module("Site::Katalog")) {
	$dbp->{MAINMOD} = "katalog";
}
else {
	$dbp->{MAINMOD} = "site";
}

$MemberBase = $dbp->use_module("Member::Base", $dbp);
if($MemberBase) {
	$MemberBase->cookie_pass();
}


# my %pages = (
	# PROD  => "kitap",
	# AUTH  => "yazar",
	# AUTHS => "yazarlar",
	# FIRM  => "yayinevi",
	# FIRMS => "yayinevleri",
# );
my @rw_types = $gui->rw_types(%pages);
my %types = map { $dbp->{RWTYPES}->{$_}->[1] => $_ } keys %{$dbp->{RWTYPES}};


# reWrite
if($dbp->{PARAM}->{"rw"}) {
	my $string = join '|', @rw_types;
	$dbp->{PARAM}->{"rw"} =~s/\.htm.*//;
	
	my ($type, $tid) = ($dbp->{PARAM}->{"rw"} =~m:^($string)/(.*):);
	$tid =~s/\/$//;
	$tid =~s/(.+)\/index([0-9]*)/$1/;
	$2 and $dbp->{PARAM}->{"page"} = $2;

	# /site/mod_uyelik/op_login.html
	if($type eq "site") {
		my @inps = split /\//, $tid;
		foreach my $inp (@inps) {
			my ($key, $val) = split /_/, $inp, 2;
			$dbp->{PARAM}->{$key} = $val;
		}
	}

	# /sayfa/tableid/table-title.html
	elsif($type eq "sayfa") {
		my $table;
		($table, $tid) = ($tid =~/^([\w]+)\/(.*)$/);
		$dbp->{RWTYPES}->{$types{$type}}->[4] = "page_$table";
	}

	if( $dbp->{RWTYPES}->{$types{$type}}->[2] and 
		$dbp->{RWTYPES}->{$types{$type}}->[3] ) {
			$dbp->{MOD} = $mod = $dbp->{RWTYPES}->{$types{$type}}->[2];
			$dbp->{OP}  = $op  = $dbp->{RWTYPES}->{$types{$type}}->[3];
		
	}
	
	my ($links) = $dbp->get_rwlink($dbp->{RWTYPES}->{$types{$type}}->[4], 1, $tid);
	$id = $dbp->{ID} = $dbp->{PARAM}->{"id"} = ($links->{$tid} || $tid);
}

if(! $mod) {
	# shop, site, katalog, news
	$mod = $dbp->{MOD} = ($dbp->{PARAM}->{"mod"} || $dbp->{MAINMOD});
}

if(! $op) {
	$op = $dbp->{OP} = ($dbp->{PARAM}->{"op"} || $dbp->{MAINOP});
}

# ...
my $mod_ucf = ucfirst( $mod );
if(defined $mod) {
	foreach my $path (@INC) {
		if(-e "$path/Site/$mod_ucf.pm") {
			$apply  = "use Site::$mod_ucf;\n";
			$apply .= "&Site::$mod_ucf"."::$op;";
			last;
		}
	}
} 
if(! $apply) {
    $apply  = "use Site::Site;\n";
    $apply .= "&Site::Site"."::$op;";
}

eval $apply;
exit(0);

1;

