#!/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 DBI;
use lib '/opt/oma/oma-bin';
use lib '/opt/oma/config';
use OMA;
use XML::Parser;


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

sub parsetag {
	local $xmltext = shift;
	local $item = shift;
	local $node = shift;

	local $video;
	local $audio;
	local $text;
	local $picture;
	local $newasset;
	local $reboot;
	local $location;
	local $opts;
	local $others;
	local $messageboard;

	($path,$location,$opts) = OMA::pathandoptsbynode($node) if($node>0);

	local $i;

	local @xmlarr = split(/\n/,$xmltext);
	local $p = XML::Parser->new(Style => 'Stream');
	local $doc = $p->parse($xmltext);

	sub StartDocument {}
	sub EndTag {}
	sub Text {}

	sub StartTag {
		local $exp = shift;
		local $type = shift;
		local %attr = %_;
		local $tag = $_;
		local %hash = %$exp;
		local $otag = $tag;
		chop($otag);

		if($item) {
		if($type eq "path" && $location ne "") {
			$xmlarr[$exp->current_line-1] =~ s/$otag[\ ]*\/?>/$location/;
		}

		if($type eq "video" && $video ne "") {
			$xmlarr[$exp->current_line-1] =~ s/$otag[\ ]*\/?>/$attr{'desc'}/;
		}

		if($type eq "audio") {
			$medialist = OMA::medialist("ora","",$attr{'size'},"","",$item);
			$xmlarr[$exp->current_line-1] =~ s/$otag[\ ]*\/?>/$medialist/;
		}

		if($type eq "picture") {
			$medialist = OMA::medialist("opa","",$attr{'size'},"","",$item);
			$xmlarr[$exp->current_line-1] =~ s/$otag[\ ]*\/?>/$medialist/;
		}

		if($type eq "text") {
			$path =~ s/\ /%20/g;
			$medialist = OMA::medialist("ota","homepage",$attr{'size'},"","",$item,$path);
			$xmlarr[$exp->current_line-1] =~ s/$otag[\ ]*\/?>/$medialist/;
		}

		if($type eq "video") {
			$medialist = OMA::medialist("ova","",$attr{'size'},"","",$item);
			$xmlarr[$exp->current_line-1] =~ s/$otag[\ ]*\/?>/$medialist/;
		}
#		if($type eq "newasset") {
#			$medialist = OMA::medialist("newasset","",$attr{'size'},"","",$item);
#			$xmlarr[$exp->current_line-1] =~ s/$otag[\ ]*\/?>/$medialist/;
#		}
		if($type eq "reboot") {
			$medialist = OMA::medialist("reboot","",$attr{'size'},"","",$item);
			$xmlarr[$exp->current_line-1] =~ s/$otag[\ ]*\/?>/$medialist/;
		}
		}
		if($type eq "tree-view") {
			my $tree = OMA::painttree();
			$xmlarr[$exp->current_line-1] =~ s/$otag[\ ]*\/?>/$tree/;
		}
		if($type eq "fast-arch") {
			my $form = OMA::fastArch();
			$xmlarr[$exp->current_line-1] =~ s/$otag[\ ]*\/?>/$form/;
		}

	}
	$xmltext="";
	for($i=0;$i<=$#xmlarr;$i++) {
		$xmltext .= $xmlarr[$i];
	}
	$xmltext =~ s/&quot;/"/g;
	return($xmltext);
}



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


local $homepage_head, $homepage_body, $homepage_bottom;
local $p1, $p2, $o;

$db=OMA::opendb();


$homepage_head = OMA::loadtempl(1,'homepage_head');
$homepage_body = OMA::loadtempl(1,'homepage_body');
$homepage_bottom = OMA::loadtempl(1,'homepage_bottom');


($p1,$p2,$o) = OMA::pathandoptsbynode(1);
$homepage_head =~ s/<location \/>/$p2/g;
$homepage_head =~ s/<options \/>/$o/g;



$db=OMA::opendb();
$st=$db->prepare("
	SELECT	item.sid,item.link_item,node.sid
	FROM	node,item
	WHERE	node.node='Homepage'
	AND		node.sid=item.node_sid
	AND		node.prev_sid=1
	AND		item.type='meta'
	AND		item.published <> 0
	ORDER BY item.sid DESC
");
$st->execute();

undef($ENV{'REMOTE_USER'});

open(F,"> $OMA::BASEPATH/index.html");
local $text="";
while(@row=$st->fetchrow()) {
	if($row[1] > 0) {
		@row2 = OMA::realitem($row[1]);
		
	} else {
		@row2 = OMA::realitem($row[0]);
    }
	$text .= parsetag($homepage_body,$row2[0],$row2[1]);
}
#print F $homepage_head;
#print F $text;
#print F $homepage_bottom;
print F parsetag($homepage_head . $text . $homepage_bottom);
close(F);
print "So I did.\n";

