#!/usr/bin/perl

# Copyright (C) 2011-2013 Zentyal S.L.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2, as
# published by the Free Software Foundation.
#
# 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

use strict;
use warnings;

use Getopt::Long;
use Cwd;
use HTML::Mason;
use File::Slurp;
use File::Copy;

use constant
    {
       MODULE_VERSION => '2.2',
       STUBS_PATH => '/usr/share/zentyal/stubs/zmoddev',
    };

sub _printUsage
{
    print STDERR "\nUsage: $0 --main-class class [Options]\n\n";
    print STDERR "Options:\n";
    print STDERR "\t--name\n";
    print STDERR "\t--destdir\n";
    print STDERR "\t--parent\n";
    _printValidTypes();
    exit 1;
}


sub _parseOptions
{
    my %options = (
            name => undef,
            parent => undef,
            destdir => undef,
            mainClass => undef,
            modNameSpace => undef,
            );
    my $help;
    my $info;
    my @fields;
    my $optionsOk = GetOptions(
            'main-class=s' => \$options{mainClass},
            'name=s' => \$options{name},
            'parent=s' => \$options{parent},
            'destdir=s' => \$options{destdir},
            'module-namespace=s' => \$options{modNameSpace},
            'help'  => \$help,
            'info'  => \$info,
            );

    if (not $optionsOk or $info or $help) {
        _printUsage();
    }

    unless (defined($options{mainClass})) {
        _printUsage();
    }

    if ($options{destdir}) {
        unless (-d $options{destdir}) {
            die "$options{destidir} does not exist!";
        }
    } else {
        $options{destdir} = getcwd;
    }

    unless ( -d $options{destdir} ) {
        die "$options{destdir} does not exist!";
    }

    return \%options;
}

sub _types
{
    my ($dir) = @_;

    my @models ;
    opendir ( my $dirFH, $dir );
    while ( defined ( my $file = readdir ( $dirFH ))) {
          next unless ( $file =~ m/.*\.pm/ );
          push(@models, $file);
    }

    return \@models;
}

sub _src_EBox_Module_Types
{
    my ($interp, $output, $options) = @_;

    my $modNameSpace = $options->{mainClass};
    my $dir = $options->{destdir} . "/src/EBox/$modNameSpace/Types";
    my $name = $options->{name};
    system ("mkdir -p $dir");

    my $template = '/src/Types/Type.pm.mas';

    my $comp = $interp->make_component(
            comp_file => STUBS_PATH . $template);
    ${$output} = '';
    my $package = "EBox::${modNameSpace}::Types::$name";
    $interp->exec($comp,
            (name => $name,
             parent => $options->{parent},
             package=> $package));
    write_file("$dir/$name.pm", ${$output});
}

sub _createType
{
    my $options = _parseOptions();

    my $dir = $options->{destdir};

    my $output;
    my $interp = HTML::Mason::Interp->new(comp_root => STUBS_PATH,
            out_method => \$output);

    if ($options->{name}) {
        _src_EBox_Module_Types($interp, \$output, $options);
    }
}

_createType();

