#!/usr/bin/perl
#------------------------------------------------------------------------------
# Copyright (C) 2001 Thomax Kaulmann
#
# 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 2, 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, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.



use lib '/opt/oma/oma-bin';
use DBI;
use OMA;
use PARSE;
use lib '/opt/oma/ora';
use ORAPARSE;
use lib '/opt/oma/ova';
use OVAPARSE;
use lib '/opt/oma/opa';
use OPAPARSE;
use lib '/opt/oma/ota';
use OTAPARSE;

$|=1;

local $st;
local @row;
local $path;
local $nodecond="";
local $itemcond="";
local $node;

local $COMMENT = 1;

$db = OMA::opendb();

#$stmain=$db->prepare("
#    SELECT item.*
#	FROM item
#	WHERE item.published <> 0
#");
#$stmain->execute();
#while(@rowmain=$stmain->fetchrow()) {
#$in{'node'} = $rowmain[1];
#$in{'item'} = $rowmain[0];


$node = $in{'node'} if($in{'node'});

$nodecond="AND item.node_sid=$in{'node'}" if($in{'node'});
if($in{'item'}) {
#	toggleitem($in{'item'});
	$itemcond="AND item.sid=$in{'item'}";
}
PARSE::getmap(1);

undef(%in); undef(%ENV);


sub meta {
	print "publish meta\n" if($COMMENT);

	chdir("$OMA::PATHPREFIX/$OMA::OMABIN");
	$st=$db->prepare("
		SELECT *
		FROM item
		WHERE type='meta'
		AND published > 0
		$nodecond
		$itemcond
	");
	$st->execute();
	while(@row=$st->fetchrow()) {
		$path = OMA::getpath($row[1]);
		if(!-d "$OMA::BASEPATH$path") {
			system("mkdir -p \"$OMA::BASEPATH$path\"");
		}
		$cont = PARSE::showitem($row[0]);
		open(F,"> $OMA::BASEPATH$path/$row[0].html");
		print F $cont;
		close(F);

		print "#" if($COMMENT);
	}
	$st->finish();
}


sub ora {
	print "\npublish ora\n" if($COMMENT);
	chdir("$OMA::PATHPREFIX/ora/perl/");
	$st=$db->prepare("
		SELECT	item.sid,item.node_sid,item.description,
				audio_conn.audio_page, audio_conn.location
		FROM	item,audio_conn
		WHERE	item.type='ora'
		AND		item.sid=audio_conn.item_sid
		AND		item.published > 0
		$nodecond
		$itemcond
	");
	$st->execute();
	while(@row=$st->fetchrow()) {
		$path = OMA::getpath($row[1]);
		if(!-d "$OMA::BASEPATH$path") {
			system("mkdir -p \"$OMA::BASEPATH$path\"");
		}
		$cont = ORAPARSE::showitem($row[0]);
		open(F,"> $OMA::BASEPATH$path/$row[0].html");
		print F $cont;
		close(F);
		print "#" if($COMMENT);
	}
	$st->finish();
}


sub ova {
	print "\npublish ova\n" if($COMMENT);
	chdir("$OMA::PATHPREFIX/ova/perl/");
	$st=$db->prepare("
		SELECT	item.sid,item.node_sid,item.description,
				video_conn.video_page, video_conn.location
		FROM	item,video_conn
		WHERE	item.type='ova'
		AND		item.sid=video_conn.item_sid
		AND		item.published > 0
		$nodecond
		$itemcond
	");
	$st->execute();
	while(@row=$st->fetchrow()) {
		$path = OMA::getpath($row[1]);
		if(!-d "$OMA::BASEPATH$path") {
			system("mkdir -p \"$OMA::BASEPATH$path\"");
		}
		$cont = OVAPARSE::showitem($row[0]);
		open(F,"> $OMA::BASEPATH$path/$row[0].html");
		print F $cont;
		close(F);
		print "#" if($COMMENT);
	}
	$st->finish();
}


sub opa {
	print "\npublish opa\n" if($COMMENT);
	chdir("$OMA::PATHPREFIX/opa/perl/");
	$st=$db->prepare("
		SELECT	item.sid,item.node_sid,item.description,
				picture_conn.picture_page, picture_conn.location
		FROM	item,picture_conn
		WHERE	item.type='opa'
		AND		item.sid=picture_conn.item_sid
		AND		item.published > 0
		$nodecond
		$itemcond
	");
	$st->execute();
	while(@row=$st->fetchrow()) {
		$path = OMA::getpath($row[1]);
		if(!-d "$OMA::BASEPATH$path") {
			system("mkdir -p \"$OMA::BASEPATH$path\"");
		}
		$cont = OPAPARSE::showitem($row[0]);
		open(F,"> $OMA::BASEPATH$path/$row[0].html");
		print F $cont;
		close(F);
		print "#" if($COMMENT);
	}
	$st->finish();
}


sub ota {
	print "\npublish ota\n" if($COMMENT);
	# publish text objects
	chdir("$OMA::PATHPREFIX/ota/perl/");
	$st=$db->prepare("
		SELECT	item.sid,item.node_sid,item.description,
				text_conn.text_page, text_conn.location
		FROM	item,text_conn
		WHERE	item.type='ota'
		AND		item.sid=text_conn.item_sid
		AND		item.published > 0
		$nodecond
		$itemcond
	");
	$st->execute();
	while(@row=$st->fetchrow()) {
		$path = OMA::getpath($row[1]);
		if(!-d "$OMA::BASEPATH$path") {
		    system("mkdir -p \"$OMA::BASEPATH$path\"");
		}
		$cont = OTAPARSE::showitem($row[0]);
		open(F,"> $OMA::BASEPATH$path/$row[0].html");
		print F $cont;
		close(F);
		print "#" if($COMMENT);
	}
	$st->finish();
}



sub indices {
	print "\npublish indices\n" if($COMMENT);
	# publish text objects
	# finaly generate the index page
	$st=$db->prepare("select * from node");
	$st->execute();
	while(@row=$st->fetchrow()) {
		if(($row[0]>1) && ($row[1] ne "Homepage")) {
			OMA::index_node($row[0]) if($row[0] && ($row[1] ne "Homepage"));
		}
		print "#" if($COMMENT);
	}
	print "\ndone.\n" if($COMMENT);
}

if($ARGV[0]) {
	meta if($ARGV[0] eq "meta");
	indices if($ARGV[0] eq "indices");
	ora if($ARGV[0] eq "ora");
	ova if($ARGV[0] eq "ova");
	opa if($ARGV[0] eq "opa");
	ota if($ARGV[0] eq "ota");
} else {
	meta;
	ora;
	ova;
	opa;
	ota;
	indices;
}

print "Finaly published the homepage\n";

