package Cpanel::Easy::Utils::Raw;

# cpanel - Cpanel/Easy/Utils/Raw.pm               Copyright(c) 2015 cPanel, Inc.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cpanel license. Unauthorized copying is prohibited

use strict;
use warnings;
no warnings qw(redefine);

sub set_rawenv {
    my ($self) = @_;
    my $confpath = $self->{'rawenv_dir'};
    $confpath =~ s{/$}{}g;

    if ( $self->get_param('skip-rawenv') ) {
        $self->print_alert( q{Skipping '[_1]'}, 'rawenv' );
        return;
    }

    if ( -d $self->{'rawenv_dir'} ) {
        if ( opendir my $rawenv_dh, $self->{'rawenv_dir'} ) {
            my @rawenv = grep !/^[.]+$/, readdir $rawenv_dh;
            closedir $rawenv_dh;
            for my $key (@rawenv) {
                if ( open my $fh, '<', "$self->{'rawenv_dir'}/$key" ) {
                    if ( $key =~ m{.append_(.*)} ) {
                        my $cur = $ENV{$1} || 'undef';
                        $ENV{$1} .= do { local $/; <$fh> };
                        chomp $ENV{$1};
                        $self->print_alert( q{rawenv '[_1]': '[_2]' before '[_3]' - after '[_4]'}, $key, $1, $cur, $ENV{$1} );
                    }
                    elsif ( $key =~ m{.prepend_(.*)} ) {
                        my $pre = do { local $/; <$fh> };
                        chomp $pre;
                        my $cur = $ENV{$1} || 'undef';
                        $ENV{$1} = "$pre$ENV{ $1 }";
                        $self->print_alert( q{rawenv '[_1]': '[_2]' before '[_3]' - after '[_4]'}, $key, $1, $cur, $ENV{$1} );
                    }
                    else {
                        my $cur = $ENV{$key} || 'undef';
                        $ENV{$key} = do { local $/; <$fh> };
                        chomp $ENV{$key};
                        $self->print_alert( q{rawenv '[_1]': '[_2]' before '[_3]' - after '[_4]'}, $key, '', $cur, $ENV{$key} );
                    }

                    close $fh;
                    push @{ $self->{'attach_files_to_report'} }, "$self->{'rawenv_dir'}/$key";
                    $self->{'_'}{'rawenv_was_used'} = 1;
                }
                else {
                    $self->log_warn( [ q{Could not open() file '[_1]' for reading: [_2]}, "$self->{'rawenv_dir'}/$key", $!, ] );
                }

                # else log or carp or no ??
            }
        }
    }
}

sub get_raw_opts_as_array {
    my $self = shift;
    my $ref  = $self->get_raw_opts_if_any(@_);
    my @opts;

    while ( my ( $key, $val ) = each(%$ref) ) {
        push @opts, map { $_ ? "$key=$_" : $key } @$val;
    }

    return \@opts;
}

sub get_raw_opts_if_any {
    my ( $self, $file ) = @_;
    my $raw_hr = {};

    if ( $self->get_param('skip-rawopts') ) {
        $self->print_alert( q{Skipping '[_1]'}, 'rawopts' );
        return;
    }

    my $abs_file = "$self->{'rawopts_dir'}/$file";
    if ( -f $abs_file ) {
        if ( open my $file_fh, '<', $abs_file ) {
            while (<$file_fh>) {
                chomp;
                my ( $key, $val ) = split /=/, $_, 2;
                next if !defined $key;
                $key =~ s/\s+//g;
                next if !length $key;

                if ( exists $raw_hr->{$key} ) {
                    $self->print_alert( q{rawopt: '[_1]' added '[_2]'}, $key, $val || '' );
                    push @{ $raw_hr->{$key} }, $val;
                }
                else {
                    $self->print_alert( q{rawopt: '[_1]' set to '[_2]'}, $key, $val || '' );
                    $raw_hr->{$key} = [$val];
                }
            }

            close $file_fh;
            push @{ $self->{'attach_files_to_report'} }, $abs_file;
            $self->{'_'}{'rawopt_was_used'} = 1;
        }
        else {
            $self->log_warn( [ q{Could not open() file '[_1]' for reading: [_2]}, $file, $! ] );
        }
    }
    return $raw_hr;
}

1;
