# Copyright (C) 2001-2007, The Perl Foundation.
# $Id: /mirror/trunk/config/gen/config_pm.pm 30398 2008-08-20T02:21:59.881769Z jkeenan  $

=head1 NAME

config/gen/config_pm.pm - Record configuration data

=head1 DESCRIPTION

Writes the C<Parrot::Config::Generated> Perl module, the
F<runtime/parrot/library/config.fpmc> generator program, and the F<myconfig>
file.

=cut

package gen::config_pm;

use strict;
use warnings;

use base qw(Parrot::Configure::Step);
use Parrot::Configure::Utils ':gen';

use Cwd qw(cwd);
use File::Spec::Functions qw(catdir);

sub _init {
    my $self = shift;
    my %data;
    $data{description} = q{Record configuration data for later retrieval};
    $data{result}      = q{};
    $data{templates}    = {
        myconfig        => 'config/gen/config_pm/myconfig.in',
        Config_pm       => 'config/gen/config_pm/Config_pm.in',
        config_lib      => 'config/gen/config_pm/config_lib.in',
    };
    return \%data;
}

sub runstep {
    my ( $self, $conf ) = @_;

    $conf->data->clean;

    $conf->genfile($self->{templates}->{myconfig}, 'myconfig' );

    open( my $IN, "<", $self->{templates}->{Config_pm} )
        or die "Can't open Config_pm.in: $!";

    my $configdir = catdir(qw/lib Parrot Config/);
    if ( !-d $configdir ) {
        mkdir $configdir
            or die "Can't create dir $configdir: $!";
    }
    my $gen_pm = q{lib/Parrot/Config/Generated.pm};
    $conf->append_configure_log($gen_pm);
    open( my $OUT, ">", $gen_pm )
        or die "Can't open $gen_pm: $!";

    print {$OUT} "# Generated by config/gen/config_pm.pm\n";

    while (<$IN>) {
        s/<<PCONFIG>>/$conf->data->dump(q{c}, q{*PConfig})/e;
        s/<<PCONFIGTEMP>>/$conf->data->dump(q{c_temp}, q{*PConfig_Temp})/e;
        print {$OUT} $_;
    }

    close $IN  or die "Can't close Config_pm.in: $!";
    close $OUT or die "Can't close Config.pm: $!";

    my $template = $self->{templates}->{config_lib};
    open( $IN,  "<", $template ) or die "Can't open '$template': $!";
    my $c_l_pasm = q{config_lib.pasm};
    $conf->append_configure_log($c_l_pasm);
    open( $OUT, ">", $c_l_pasm ) or die "Can't open $c_l_pasm: $!";

    print {$OUT} <<"END";
# Generated by config/gen/config_pm.pm from the template
# '$template' and \%PConfig
# This file should be the last thing run during
# the make process, after Parrot is built.
END

    while (<$IN>) {
        if (/<<PCONFIG>>/) {
            my $k;
            for $k ( sort { lc $a cmp lc $b || $a cmp $b } $conf->data->keys ) {
                my $v = $conf->data->get($k);
                if ( defined $v ) {
                    $v =~ s/(["\\])/\\$1/g;
                    $v =~ s/\n/\\n/g;
                    print {$OUT} qq(\tset P0["$k"], "$v"\n);
                }
                else {
                    print {$OUT} qq(\tset P0["$k"], P1\n);
                }
            }
        }
        elsif (s/<<PWD>>/cwd/e) {
            print {$OUT} $_;
        }
        else {
            print {$OUT} $_;
        }
    }

    close $IN  or die "Can't close config_lib.in: $!";
    close $OUT or die "Can't close $c_l_pasm: $!";

    return 1;
}

1;

# Local Variables:
#   mode: cperl
#   cperl-indent-level: 4
#   fill-column: 100
# End:
# vim: expandtab shiftwidth=4:
