package Cpanel::Easy::Utils::File;

# cpanel - Cpanel/Easy/Utils/File.pm              Copyright(c) 2013 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);
use Symbol            ();
use Cpanel::SafeFile  ();
use Cpanel::RcsRecord ();

# Allows you to add text to file AFTER a regex has been found
sub add_to_file_after_regex {
    my $self   = shift;
    my $file   = shift;
    my $string = shift;
    my $match  = shift;
    my $fh     = Symbol::gensym();
    my $found  = 0;
    my @content;
    my @ret;
    my $lock;
    local $_;

    eval { $lock = Cpanel::SafeFile::safeopen( $fh, '+<', $file ); };

    if ($lock) {
        seek( $fh, 0, 0 );

        while ( readline($fh) ) {
            push @content, $_;

            if ( !$found && $_ =~ $match ) {
                push @content, $string, "\n";
                $found = 1;
            }
        }

        # error reading file
        if ($!) {
            Cpanel::SafeFile::safeclose( $fh, $lock );
            @ret = ( 0, q{System error while reading file, '[_1]'}, $file );
        }
        else {
            seek( $fh, 0, 0 );
            for my $line (@content) {
                print {$fh} $line;
            }
            Cpanel::SafeFile::safeclose( $fh, $lock );
            Cpanel::RcsRecord::rcsrecord($file);

            @ret = ( 1, 'Ok' );
        }
    }
    else {
        @ret = ( 0, q{Failed to safely open '[_1]'}, $file );
    }

    return @ret;
}

# Remove one or more (entire) lines from a file that match a regex
sub strip_from_file {
    my $self = shift;
    my $file = shift;
    my $mref = shift;
    my $fh   = Symbol::gensym();
    my @content;
    my @ret;
    my $lock;
    local $_;

    my @match = ref($mref) eq 'ARRAY' ? @$mref : $mref;    # supports array ref or scalar

    eval { $lock = Cpanel::SafeFile::safeopen( $fh, '+<', $file ); };

    if ($lock) {
        seek( $fh, 0, 0 );

        # strip text from file
        for my $m (@match) {
            while ( ( $_ = readline($fh) ) ) {
                next if $_ =~ $m;
                push @content, $_;
            }
        }

        # error reading file
        if ($!) {
            Cpanel::SafeFile::safeclose( $fh, $lock );
            @ret = ( 0, q{System error while reading file, '[_1]'}, $file );
        }
        else {
            truncate( $fh, 0 );
            seek( $fh, 0, 0 );

            for my $line (@content) {
                print {$fh} $line;
            }
            Cpanel::SafeFile::safeclose( $fh, $lock );
            Cpanel::RcsRecord::rcsrecord($file);

            @ret = ( 1, 'Ok' );
        }
    }
    else {
        @ret = ( 0, q{Failed to safely open '[_1]'}, $file );
    }

    return @ret;
}

sub is_string_in_file {
    my $self  = shift;
    my $file  = shift;
    my $re    = shift;    # assumes caller used qr//
    my $found = 0;

    # open file
    my $fh = Symbol::gensym();
    my $lock;
    eval { $lock = Cpanel::SafeFile::safeopen( $fh, '<', $file ); };

    # search for regex in file
    if ($lock) {
        local $_;

        while (<$fh>) {
            chomp;

            if ( $_ =~ $re ) {
                $found = 1;
                last;
            }
        }

        # close file
        Cpanel::SafeFile::safeclose( $fh, $lock );
    }
    else {
        $self->log_warn( [ q{Failed to safely open '[_1]': [_2]}, $file, $! ] );
    }

    return $found;
}

1;
