#!/usr/bin/perl -w
#  Copyright (C) 2002  Stanislav Sinyagin
#
#  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.

# Stanislav Sinyagin <ssinyagin@k-open.com>


use strict;
use warnings;
BEGIN { require '/usr/share/torrus/conf_defaults/torrus-config.pl'; }

use Getopt::Long;

use Torrus::ConfigTree::XMLCompiler;
use Torrus::SiteConfig;
use Torrus::Log;

exit(1) unless Torrus::SiteConfig::verify();

our @trees;
our $all_trees;
our $no_validation;
our $force;
our $nogc;

our $debug;
our $verbose;
our $help_needed;

my $opts_ok = GetOptions ('tree=s'   => \@trees,
                          'all'      => \$all_trees,
                          'noval'    => \$no_validation,
                          'force'    => \$force,
                          'nogc'     => \$nogc,
                          'debug'    => \$debug,
                          'verbose'  => \$verbose,
                          'help'     => \$help_needed);

if( not $opts_ok or not (scalar(@trees) or $all_trees) or
    $help_needed or scalar(@ARGV) > 0 )
{
    print STDERR "Usage: $0 --tree=NAME [options...]\n",
    "Options:\n",
    "  --tree=NAME     tree name(s) to compile\n",
    "  --all           compile all trees\n",
    "  --noval         disable parameter validation\n",
    "  --force         force re-processing of all XML files\n",
    "  --nogc          skip the Git garbage collector\n",
    "  --debug         set the log level to debug\n",
    "  --verbose       set the log level to info\n",
    "  --help          this help message\n";
    exit 1;
}

if( $all_trees )
{
    @trees = Torrus::SiteConfig::listTreeNames();
}

if( $debug )
{
    Torrus::Log::setLevel('debug');
}
elsif( $verbose )
{
    Torrus::Log::setLevel('verbose');
}


Verbose(sprintf('Torrus version %s', '3.00'));

our $global_ok = 1;

foreach my $tree ( @trees )
{
    if( not Torrus::SiteConfig::treeExists( $tree ) )
    {
        Error("Tree named \"" . $tree . "\" does not exist");
        exit(1);
    }

    Verbose("Compiling tree: $tree");

    my $ok = 1;
    my $compiler =
        new Torrus::ConfigTree::XMLCompiler( -TreeName => $tree,
                                             -ForceRebuild => $force );
    if( not defined( $compiler ) )
    {
        Error('Cannot initialize compiler for tree ' . $tree . '. Exiting');
        Error('If you are sure there are no other compiler processes ' .
              'running, use the --force option');
        $global_ok = 0;
        last;
    }     
    
    my @xmlFiles = @Torrus::Global::xmlAlwaysIncludeFirst;
    push( @xmlFiles, Torrus::SiteConfig::listXmlFiles( $tree ) );
    push( @xmlFiles, @Torrus::Global::xmlAlwaysIncludeLast );

    if( not $compiler->compile_files( \@xmlFiles ) )
    {
        Error("Errors found during XML compilation in the tree named \"" .
              $tree . "\"");
        $global_ok = 0;
        last;
    }

    Verbose('Preparing a commit...');
    if( not $compiler->commitConfig() )
    {
        Error('Errors found during post-processing');
        $ok = 0;
    }

    if( $ok )
    {
        if( $no_validation )
        {
            Verbose('Skipping data validation...');
        }
        else
        {
            Verbose('Data validation...');
            if( not $compiler->validate() )
            {
                Error('Errors found during validation process');
                $ok = 0;
            }
        }
    }
                             
    $compiler->finalize( $ok );

    if( $ok )
    {
        Verbose('Updating agent configurations');
        $compiler->updateAgentConfigs();
    }

    $global_ok = $ok ? $global_ok:0;
}

if( not $nogc )
{
    if( Torrus::ConfigTree::running_writers_exist() )
    {
        Verbose('Skipping Git GC because compilers are running');
    }
    else
    {
        # Garbage collector is needed because of this:
        # https://github.com/libgit2/libgit2/issues/4093
        Verbose('Running Git garbage collector...');
        system(sprintf('cd %s; %s gc',
                       $Torrus::Global::gitRepoDir,
                       $Torrus::Global::gitExec));
        
        # Update "objects/info/packs" and "info/refs" needed by dumb server
        system(sprintf('cd %s; %s update-server-info',
                       $Torrus::Global::gitRepoDir,
                       $Torrus::Global::gitExec));
    }
}

exit($global_ok ? 0:1);


# Local Variables:
# mode: perl
# indent-tabs-mode: nil
# perl-indent-level: 4
# End:
