#!/usr/bin/perl

use DBI;
use lib '/opt/oma/oma-bin';
use OMA;
use XML::Parser;

$|=1;

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

sub checktempl {
    my $node = shift;
    my $type = shift;
    my $xmlstr="";
    local $st;
    local @row;

    if($type =~ /^search_/ && !$xmlstr) {
        $st=$db->prepare("SELECT * FROM template WHERE node_sid=$node AND type='search_head'");
        $st->execute();
        @row = $st->fetchrow();
        $st->finish();
        $xmlstr = $row[2];

        $st=$db->prepare("SELECT * FROM template WHERE node_sid=$node AND type='search_body'");
        $st->execute();
        @row = $st->fetchrow();
        $st->finish();
        $xmlstr .= $row[2];

        $st=$db->prepare("SELECT * FROM template WHERE node_sid=$node AND type='search_bottom'");
        $st->execute();
        @row = $st->fetchrow();
        $st->finish();

    }
    if($type =~ /^homepage_/ && !$xmlstr) {
        $st=$db->prepare("SELECT * FROM template WHERE node_sid=$node AND type='homepage_head'");
        $st->execute();
        @row = $st->fetchrow();
        $st->finish();
        $xmlstr = $row[2];

        $st=$db->prepare("SELECT * FROM template WHERE node_sid=$node AND type='homepage_body'");
        $st->execute();
        @row = $st->fetchrow();
        $st->finish();
        $xmlstr .= $row[2];

        $st=$db->prepare("SELECT * FROM template WHERE node_sid=$node AND type='homepage_bottom'");
        $st->execute();
        @row = $st->fetchrow();
        $st->finish();
        $xmlstr .= $row[2];

    }
    if(!$xmlstr) {
        $st=$db->prepare("SELECT * FROM template WHERE node_sid=$node AND type='$type'");
        $st->execute();
        @row = $st->fetchrow();
        $st->finish();
        $xmlstr = $row[2];
    }

    if(!fork()) {
        open(STDOUT, '>', "/dev/null") || die "Can't redirect stdout";
        open(STDERR, '>', "/tmp/xmlparse.err")     || die "Can't dup stdout";
        my $p = XML::Parser->new(Style => 'Stream');
        my $doc = $p->parse($xmlstr);
    }
    else {
        wait(); 
        $exit = $? >> 8;
        $sign = $? & 127;
        if($exit > 0) {
            open(F,"< /tmp/xmlparse.err");
            while(<F>) {
                                $err .= $_; 
                        }
            close(F);
            $err =~ s/\ at\ \/(.)+Parser.pm line 185//g;
            print "Found an error in $type:<br>$err<br>";
                        $xmlstr =~ s/</&lt;/g;
                        $xmlstr =~ s/>/&gt;/g;
                        @xmlarr = split(/\n/,$xmlstr);
                        ($t1) = split(/, column/,$err);
                        ($t2) = split(/\ /,reverse($t1));
                        $line = reverse($t2);
                        print "<pre>";
                        for($i=($line-3 < 0)?0:$line-3;$xmlarr[$i] && $i <= $line+3;$i++) {
                                printf("%03d: %s\n",$i,$xmlarr[$i]);
                        }
                        print "</pre>";
                        print "You better go <a href=javascript:history.back()>back</a> and fix it!<br><br>";
        }
                else {
                        print "Template looks fine.<br>";
                }
     #   unlink("/tmp/xmlparse.out");
    }
}

$db = OMA::opendb();

checktempl($ARGV[0], $ARGV[1]);

