#[BOFN]###############################################################################
#
#
#Pagenews - a free script to publish news on websites
#Copyright (C) 2004,2005,2006,2007,2008 Philipp Kindt
#
#This file is part of Pagenews.
#
# 	 This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#[EOFN]###############################################################################


#this package contains functions for creating static documents from
#templates using the donparser Create - On - The - Fly (COF) capabilities

package dncof;


#########################################################################################################
#builds static url from dnstandard_own_url in $_[0] and static_dir in $_[1]
sub get_static_URL{
	#workaround for inproper own_urls
	my $own_url = $_[0];
	if($own_url !~ /\//){
		$own_url = "http://".$ENV{'HTTP_HOST'}.$ENV{'REQUEST_URI'};
		($own_url, my $waste) = split(/\?/,$own_url);
	}
	my @parts = split(/\//,$own_url);
	
	my $static_url;	
	my $substract = 0;


	if($_[1] eq ".."){
		$substract = 1;
	}

	for(my $cnt = 0; $cnt < @parts - 1 - $substract; $cnt++){
		$static_url .= $parts[$cnt]."\/";		
	}
	
	#some "eye-candy"
	if($_[1] ne ".."){
		$static_url .= $_[1];	
	}else{
		chop($static_url);	
	}
	
	return $static_url;
}
########################################################################################################
#builds all templates (and their further ones) which are in array ref in $_[0]
sub build_multiples{
	my @arr = @{$_[0]};
	foreach my $template (@arr){
		build($template);
	} 
}

#########################################################################################################
#builds static template $_[0] (in user task) and all templates that are
#needed by it
sub build{


	if($_[0] eq "main"){
		return;	
	}
	$donparser::COF_mode = "on";
	$donparser::main_mode = "on";

	
	
	
	push(@{$donparser::clientinfo->{'urls'}},$dnmain::settings->{'own_url'});

	push(@{$donparser::clientinfo->{'urls'}},"news.cgi");
	
	$donparser::clientinfo->{'template_key'} = "do";
	$donparser::clientinfo->{'static_extension'} = "html";
	$donparser::clientinfo->{'static_prefix'} = $dnmain::settings->{'COF_static_prefix'};
	$donparser::clientinfo->{'static_dir'} = $dnmain::settings->{'COF_static_dir'};
	$donparser::clientinfo->{'static_URL'} = dncof::get_static_URL($dnmain::settings->{'own_url'},$dnmain::settings->{'COF_static_dir'});
	$donparser::clientinfo->{'path_to_client'} = $dnmain::settings->{'COF_path_to_client'};

	$donparser::clientinfo->{'main_template'} = "main";
	$donparser::clientinfo->{'main_template_key'} = "main_template";

	$donparser::clientinfo->{'standard_template'} = "Standard";		

	$donparser::clientinfo->{'path_ident'} = "task";
	$donparser::clientinfo->{'default_path_ident'} = "user";
	
	$donparser::clientinfo->{'redirector_template'} = "donparser_COF_redirect";
	$donparser::clientinfo->{'redirector_template_path_ident'} = "internal";

	$donparser::clientinfo->{'fixed_filename_key'} = "donparser_COF_filename";

	donparser::static_set_browserparam_callback("dncof::fake_browserparams");
	donparser::static_set_path_ident_lookup_callback("dntemplatemanager::determine_path");



	#this is the relative path from the client to the write file
	$donparser::clientinfo->{'static_dir'} = $dnmain::settings->{'COF_static_dir'};

	#the (absolute) url of the donparser client
	$donparser::clientinfo->{'client_URL'} = $donparser::clientinfo->{'path_to_client'}."/news.cgi";
	my $parser = donparser->new(dntemplatemanager::determine_path("user"));
	$parser->COF_start($_[0]);

}	
########################################################################################################
#provides static faked browserparams to donparser
#returns %donparser::in;
sub fake_browserparams{
my %in = %donparser::in;
if($in{'do'} eq ""){
	$in{'do'} = "Standard";

};


	return %in;
}

1;

