#!/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/config';
use lib '/opt/oma/oma-bin';
use lib '/opt/oma/news-bin';
use lib '/opt/oma/transact';
use lib '/opt/oma/ora';
use lib '/opt/oma/ota';
use lib '/opt/oma/ova';
use lib '/opt/oma/opa';
use lib '/opt/oma/reboot';
use lib '/opt/oma/newasset';

use DBI;
use CGI;
use OMA;
use PARSE;
use ORAPARSE;
use OVAPARSE;
use OPAPARSE;
use OTAPARSE;
use NEWASSETPARSE;
use REBOOTPARSE;

$|=1;

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


sub toggleitem
{
	local $item = shift;
	local @row;
	local $st;

	@row = OMA::loaditem($item);
	if($row[5] == 1) {
		$path = OMA::getpath($row[1]);
		if(-f "$OMA::BASEPATH$path/$row[0].html") {
			unlink("$OMA::BASEPATH$path/$row[0].html");
		}
		$st=$db->prepare("UPDATE item SET published = 0 WHERE sid = $item");
		print "unpublish $row[2]<br>\n";
	} else {
		$st=$db->prepare("UPDATE item SET published = 1 WHERE sid = $item");
		print "publish $row[2]<br>\n";
	}
	$st->execute;
}

##   ##            ##            ######         ##   ##
### ###           ####             ##           ###  ##
#######          ##  ##            ##           #### ##
#######          ##  ##            ##           ## ####
## # ##          ######            ##           ##  ###
##   ##          ##  ##            ##           ##   ##
##   ##          ##  ##          ######         ##   ##

$db = OMA::opendb();



if(!$ARGV[0]) {
	CGI::ReadParse();
} else {
	$node = $ARGV[0];
}
#print "Content-type: text/html\n\n<html><head></head><body bgcolor=#dfdfdf link=#007700 vlink=#770000>";
print "\n<html><head></head><body bgcolor=#dfdfdf link=#007700 vlink=#770000>";

print "\n<TT>";

$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);

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);
}
$st->finish();

print "meta objects done<br>\n";

chdir("$OMA::PATHPREFIX/ora/");
$st=$db->prepare("
		SELECT	item.sid,item.node_sid,item.description,
				ora_conn.ora_page, ora_conn.location
		FROM	item,ora_conn
		WHERE	item.type='ora'
		AND		(item.sid=ora_conn.item_sid OR item.link_item=ora_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);
}
$st->finish();
print "audio objects done<br>\n";


chdir("$OMA::PATHPREFIX/ova/");
$st=$db->prepare("
		SELECT	item.sid,item.node_sid,item.description,
				ova_conn.ova_page, ova_conn.location
		FROM	item,ova_conn
		WHERE	item.type='ova'
		AND		(item.sid=ova_conn.item_sid OR item.link_item=ova_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);
}
$st->finish();
print "film objects done<br>\n";


chdir("$OMA::PATHPREFIX/opa");
$st=$db->prepare("
		SELECT	item.sid,item.node_sid,item.description,
				opa_conn.opa_page, opa_conn.location
		FROM	item,opa_conn
		WHERE	item.type='opa'
		AND		(item.sid=opa_conn.item_sid OR item.link_item=opa_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);
}
$st->finish();
print "picture objects done<br>\n";




# publish text objects
chdir("$OMA::PATHPREFIX/ota/");
$st=$db->prepare("
		SELECT	item.sid,item.node_sid,item.description,
				ota_conn.ota_page, ota_conn.location
		FROM	item,ota_conn
		WHERE	item.type='ota'
		AND		(item.sid=ota_conn.item_sid OR item.link_item=ota_conn.item_sid)
		AND		item.published > 0
		AND		ota_conn.page>=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);
}
$st->finish();
print "text objects done<br>\n";



# publish newasset objects
chdir("$OMA::PATHPREFIX/newasset/");
$st=$db->prepare("
		SELECT	item.sid,item.node_sid,item.description,
				newasset_conn.newasset_page, newasset_conn.location
		FROM	item,newasset_conn
		WHERE	item.type='newasset'
		AND		(item.sid=newasset_conn.item_sid OR item.link_item=newasset_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 = NEWASSETPARSE::showitem($row[0]);
	open(F,"> $OMA::BASEPATH$path/$row[0].html");
	print F $cont;
	close(F);
}
$st->finish();
print "newasset objects done<br>\n";



# publish reboot objects
chdir("$OMA::PATHPREFIX/reboot/");
#$db->trace(1);
$st=$db->prepare("
		SELECT	item.sid,item.node_sid,item.description,
				reboot_conn.reboot_page, reboot_conn.location
		FROM	item,reboot_conn
		WHERE	item.type='reboot'
		AND		(item.sid=reboot_conn.item_sid OR item.link_item=reboot_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") {
#print "mkdir -p \"$OMA::BASEPATH$path\"<br>";
		system("mkdir -p \"$OMA::BASEPATH$path\"");
	}
	$cont = REBOOTPARSE::showitem($row[0]);
#print "open > $OMA::BASEPATH$path/$row[0].html<br>";
	open(F,"> $OMA::BASEPATH$path/$row[0].html");
	print F $cont;
	close(F);
}
$st->finish();
#$db->trace(0);
print "reboot objects done<br>\n";

$st=$db->prepare("select sid from node where sid > 10"); $st->execute();
while(@row=$st->fetchrow()) {
#	print "batch node $row[0]\n";
	OMA::index_node($row[0]);
}
$st->finish();

# finaly generate the index page
@row=OMA::loadnode($node) if($node);
if($row[0] && ($row[1] ne "Homepage")) {
	OMA::index_node($row[0]) if($row[0] && ($row[1] ne "Homepage"));
} else {
	print "</TT>";
	exec("/opt/oma/oma-bin/genhome") || die "Can't generate homepage.";
#	$st=$db->prepare("SELECT * FROM node WHERE node <> 'Homepage'");
#	$st->execute();
#	while(@row=$st->fetchrow()) {
#		OMA::index_node($row[0]);
#	}
#	$st->finish();
}

print "index pages done<br>\n";
print "</TT></body></html>";


