#!/usr/bin/perl -w

#----------------------------------------------------------------------
# copyright (C) 1999-2003 Mitel Networks Corporation
# 
# 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 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, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
# 
# Technical support for this program is available from Mitel Networks 
# Please visit our web site www.mitel.com/sme/ for details.
#----------------------------------------------------------------------
package esmith;

use strict;

use esmith::ConfigDB;
use esmith::FormMagick;
use esmith::I18N;

my $navigation_ignore = 
    "(\.\.?|navigation|noframes|online-manual|(internal|pleasewait)(-.*)?)";

my $i18n = new esmith::I18N;

foreach my $lang ( $i18n->availableLanguages() )
{
    use constant WEBFUNCTIONS => '/etc/e-smith/web/functions';
    use constant NAVIGATIONDIR => '/home/e-smith/db/navigation';

    opendir FUNCTIONS, WEBFUNCTIONS or 
	die "Couldn't open ", WEBFUNCTIONS, "\n";

    my @files = grep (!/^${navigation_ignore}$/, readdir (FUNCTIONS));
	
    my $navinfo = NAVIGATIONDIR . "/navigation.$lang";

    system ("/bin/touch", $navinfo);

    my $navdb = esmith::ConfigDB->open($navinfo) or 
	die "Couldn't open $navinfo\n";

    foreach my $file (@files)
    {
	next if (-d WEBFUNCTIONS . "/$file");
	next unless (-x WEBFUNCTIONS . "/$file");

	my $rec = $navdb->get($file) || 
		$navdb->new_record($file, { type => 'panel' } );

	#-------------------------------------------------- 
	# extract heading, description and weight information
	# from CGI script
	#-------------------------------------------------- 
	$ENV{'HTTP_ACCEPT_LANGUAGE'} = $lang;

	# trick esmith::FormMagick into thinking we are the panel
	local $0 = WEBFUNCTIONS . "/$file";

	open(main::DATA, $0);

	while ( <main::DATA> )
	{
	    last if /^__DATA__/;
	}

	#-------------------------------------------------- 
	# If an FM subclass for this panel exists, lets try to 
	# instantiate it.  Otherwise, we'll just instantiate an
	# esmith::FormMagick object.
	#-------------------------------------------------- 

	my $fm;
	eval "use esmith::FormMagick::Panel::${file} ()";
	unless ($?)
	{
	    $fm = eval "esmith::FormMagick::Panel::${file}->new()";
	}
	unless (defined $fm)
	{
	    $fm = esmith::FormMagick->new();
	}
	
	#-------------------------------------------------- 
	# parse_xml() will not be happy if this is not in fact
        # a FormMagick panel and thus doesn't have a __DATA_ section
	#-------------------------------------------------- 
	my $parse_result = eval { $fm->parse_xml(); };

	$rec->merge_props(
		Heading => $fm->heading,
		Description => $fm->description,
		HeadingWeight => $fm->heading_weight,
		DescriptionWeight => $fm->description_weight);
    }

    $navdb->close();

    chmod 0644, $navinfo;
}
