#!/usr/bin/perl

use strict;
use PerlBean;
use PerlBean::Collection;
use PerlBean::Method;
use PerlBean::Attribute::Factory;
use PerlBean::Style;
use PerlBean::Symbol;
use PerlBean::Described::ExportTag;

# Bean description array
@::bean_desc = ();

# Add descriptions to bean description array
foreach my $fn (<gen/attr-*.pl>) {
    require $fn;
}

# Make bean collection
my $collection = PerlBean::Collection->new( {
    license => <<EOF,
This file is part of the C<PerlBean> module hierarchy for Perl by
Vincenzo Zocca.

The PerlBean module hierarchy 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.

The PerlBean module hierarchy 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 the PerlBean module hierarchy; if not, write to
the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
EOF
} );

# Make attribute factory
my $factory = PerlBean::Attribute::Factory->new();

# Add beans to collection
foreach my $bean_desc (@::bean_desc) {

    # Back reference collection
    $bean_desc->{bean_opt}{collection} = $collection;

    # Creat bean
    my $bean = PerlBean->new( $bean_desc->{bean_opt} );

    # Add bean to collection
    $collection->add_perl_bean($bean);

    # Add attributes to bean
    foreach my $attr_opt ( @{ $bean_desc->{attr_opt} } ) {
        my $attr = $factory->create_attribute($attr_opt);
        $bean->add_method_factory($attr);
    }

    # Add methods to bean
    foreach my $meth_opt ( @{ $bean_desc->{meth_opt} } ) {
        my $meth = PerlBean::Method->new($meth_opt);
        $bean->add_method($meth);
    }

    # Add symbols to bean
    foreach my $sym_opt ( @{ $bean_desc->{sym_opt} } ) {
        my $sym = PerlBean::Symbol->new($sym_opt);
        $bean->add_symbol($sym);
    }

    # Add tag descriptions to bean
    foreach my $tag_opt ( @{ $bean_desc->{tag_opt} } ) {
        my $tdesc = PerlBean::Described::ExportTag->new($tag_opt);
        $bean->add_export_tag_description($tdesc);
    }

    # Add dependencies to bean
    foreach my $use_opt ( @{ $bean_desc->{use_opt} } ) {
        my $dep = PerlBean::Dependency::Use->new($use_opt);
        $bean->add_dependency($dep);
    }
}

# Revove the old tmp directory
system('rm -rf tmp');

# Revove the old auto directory
system('rm -rf auto');

# Make a new tmp directory
mkdir('tmp');

# Make a new auto directory
mkdir('auto');

# Write the hierarch
$collection->write('tmp');

# Return a true value
1;
