package renderer_file;
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(create_renderer_file);

#----------------------------------------------------------------------------
# /var/config/www/cgi-bin/renderer_file.pm - render configuration to file
#
# Creation:     06.01.2004  max
# Last Update:  $Id$
#
# Copyright (c) 2004-2005  eisfair webconf team
#
# 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.
#----------------------------------------------------------------------------

use strict;
use Fcntl ':flock';

use renderer;

our $VERBOSE;

my $separator = "-------------------------------------------------------------------------------";


#############################################################################
# sub create_renderer_file()
# - writes a new config-file
# IN:  $package    - name of the package     
#      $configfile - full path to an configfile (optional)
# OUT: renderer    - reference to hash with rendering functions
#############################################################################
sub create_renderer_file
{
    my $package     = shift;
    my $configfile  = shift || "/etc/config.d/$package";
    my $classname   = "";

    ### build up hashtable for rendering configfiles
    my $renderer  = create_renderer($package, $classname);

    ### open file
    open(CONF, ">$configfile") || die "Could not open '$configfile' for writing, reason: $!.\n";
    flock(CONF, LOCK_EX);


    ### default will be used if no specific function is provided
    $renderer->{"print_default"}   = file_print_default($renderer);
    $renderer->{"print_typeerror"} = file_print_default($renderer);

    ### empty line
    $renderer->{"print_empty"}  = file_print_empty($renderer);

    ### header and footer
    $renderer->{"print_header"} = file_print_header($renderer, $package);
    $renderer->{"print_footer"} = file_print_footer($renderer, $package);

    ### forms
#    $renderer->{"begin_form"}      = file_begin_form($renderer);  - not used for file
#    $renderer->{"end_form"}        = file_end_form($renderer);  - not used for file

    ### groups
    $renderer->{"begin_group"}  = file_begin_group($renderer);
    $renderer->{"end_group"}    = file_end_group($renderer);

    ### lists
    $renderer->{"begin_list"}   = file_print_empty($renderer);
    $renderer->{"next_list"}    = file_print_empty($renderer);
#    $renderer->{"end_list"}     = file_end_list;  - not used for file

    ### close renderer
    $renderer->{"close"}        = file_close($renderer);

    ### return renderer
    return $renderer
}


#############################################################################
#
# implementation of the renderer
# - structure -
#
#############################################################################

sub file_close
{
my ($renderer) = @_;
return sub
{
    flock(CONF, LOCK_UN);
    close(CONF);
}
}


sub file_print_header
{
my ($renderer) = @_;
my $package   = $renderer->{"info"}->{"package"};
return sub
{
    my ($sec, $min, $h, $day, $month, $year) = localtime(time);
    $year += 1900;

    if ($day   < 10) { $day   = "0$day"; }
    if ($month < 10) { $month = "0$month"; }
    my $timestamp = "$day.$month.$year";
    my $user      = "webconf";

    # print the head of the config
    printf CONF "#$separator\n";
    printf CONF "# /etc/config.d/$package - configuration for $package\n";
    printf CONF "#\n";
    printf CONF "# Creation:     \n";
    printf CONF "# Last Update:  $timestamp  $user\n";
    printf CONF "# \n";
    printf CONF "# Copyright (c) 2001-$year The Eisfair Team, c/o Frank Meyer, frank(at)eisfair(dot)org\n";
    printf CONF "# \n";
    printf CONF "# This program is free software; you can redistribute it and/or modify\n";
    printf CONF "# it under the terms of the GNU General Public License as published by\n";
    printf CONF "# the Free Software Foundation; either version 2 of the License, or\n";
    printf CONF "# (at your option) any later version.\n";
    printf CONF "#$separator\n\n";
}
}


sub file_print_footer
{
my ($renderer) = @_;
my $classname = $renderer->{"info"}->{"classname"};
return sub
{
    printf CONF "\n";
    printf CONF "#$separator\n";
    printf CONF "# End\n";
    printf CONF "#$separator\n";
}
}


sub file_begin_group
{
my ($renderer) = @_;
return sub
{
    my ($hformat) = @_;
    my $name = $hformat->{"name"};
    my $comment = $hformat->{"comment"};

    if ($name)
    {
        printf CONF "#$separator\n";
        printf CONF "# $name\n";

        if ($comment)
        {
            $comment =~ s/\n/\\#/g;
            $comment =~ s/\\#/\n# /g;
            printf CONF "#\n# $comment\n";
        }
    }
    printf CONF "#$separator\n\n";
}
}


sub file_end_group
{
my ($renderer) = @_;
return sub
{
    printf CONF "\n\n";
}
}


sub file_print_empty
{
my ($renderer) = @_;
return sub
{
    printf CONF "\n";
}
}


#############################################################################
#
# implementation of the renderer
# - elements -
#
#############################################################################


sub file_print_default
{
my ($renderer) = @_;
my $indent = 39 - 2;
return sub
{
    my ($name, $value, $hstruct, $undefined) = @_;

    my $hname    = $hstruct->{"name"}    || "";
    my $comment  = $hstruct->{"comment"} || "";

    my ($count, $space);

    if ($value ne "" || $undefined ne "yes")
    {
        printf CONF "$name='$value'%n", $count;
        if ($comment)
        {
            printf CONF "\n%n ", $count              if ($count >= $indent);
            printf CONF "%*s # ", $indent-$count, ' ';

            $space = 76 - $indent;
            foreach $value (split(/\s+/, $comment))
            {
                if (length($value) > $space)
                {
                    printf CONF "\n%*s # ", $indent, '';
                    $space = 76 - $indent;
                }
                printf CONF "%s %n", $value, $count;
                $space = $space - $count;
            }  
        }
        printf CONF "\n";
    }
}
}

1;

