package Cpanel::Easy::Test::UI::CLI;

# cpanel10 - Cpanel/Easy/Test/UI/CLI.pm     Copyright(c) 2005-2007 cPanel, Inc.
#                                                            All rights Reserved.
# copyright@cpanel.net                                          http://cpanel.net
# This code is subject to the cpanel license. Unauthorized copying is prohibited

sub new {
    return bless {}, shift;
}

sub isa_ui {
    return 1;
}

sub setup {
    my ( $self, $easy ) = @_;
    $easy->_cpanelsync();

    if ( $easy->create_state_file() ) {
        $easy->_create_backup();
    }
}

sub cleanup {
    my ( $self, $easy ) = @_;

    if ( $easy->_restore_backup_if_needed() ) {
        print "\t\t\tbackup handled ok!\n";
    }

    $easy->report_errors();
}

sub pid_file_failed {
    my ( $self, $easy, $pid_file_rc ) = @_;

    if ( !defined $pid_file_rc ) {
        my $cur_run = <<'END_UNAVAIL';
        Easytest is currently running. You will need to wait for the other
        instance to finishing running. If a build is currently running you will
        need to wait for the build to complete before using this interface.
        This can take anywhere from 1-30 minutes.

        For advanced users: You can determine if test is rebuilding
        by using the following command on the command line as root:

        ps auxwww | grep easytest

        Easytest is still running if you see output other than something
        similar to:  root     22545  0.0  0.0  4192  648 pts/5    S+
        10:11   0:00 grep easytest

END_UNAVAIL
        print $easy->maketext($cur_run);
    }
    elsif ( $pid_file_rc == 0 ) {
        print $easy->maketext( 'Error reading or writing pidfile: [_1]', $! ) . "\n";
    }
    else {
        print $easy->maketext( q{Unknown/invalid pid_file_rc passed '[_1]'}, $pid_file_rc ) . "\n";
    }
}

sub output {
    my ( $self, $easy, $indent_level, @maketext ) = @_;
    my $string = "\t" x int($indent_level) . $easy->maketext(@maketext) . "\n";
    $easy->print_to_log_and_screen($string);
}

sub print_to_screen {
    my ( $self, $easy, $string ) = @_;
    print $string;
}

sub get_twiddler_new_hashref {
    my ($self) = @_;

    # use's main obj's lang_obj
    return {
        'output_ns' => 'Cpanel::CPAN::Text::Twiddler::CLI',

        # just use defaults or set these ?
        # 'start' => '',
        # 'text'  => '',
        # 'end'   => '',
    };
}

sub alert_over_last_twiddle {
    my ( $self, $easy, @maketext ) = @_;

    print "\b" x $easy->{'twiddler'}->get_longest() if $easy->{'twiddler'};

    $easy->print_alert(@maketext);

    $easy->print_to_screen("\n");
    print ' ' x $easy->{'twiddler'}->get_longest() if $easy->{'twiddler'};
}

# TODO: configure_profile()
sub configure_profile {
    my ( $self, $easy, $profile, $profile_setup ) = @_;    # already calculated file, hashref of that file

    # check all @{ $self->{'state'} } for params and handle as per reverse or not.

    # $self->save_prefs_hashref(); if you change $self->{'_'}{'prefs'} at all

    # to save modified $profile_setup as the main 'saved' config:
    # return 1;

    # if you do not want to do that (terminate session, 'save as' only (vs save_as() and then build()), etc...)
    # just 'return;' after having done the appropriate message screen:
    #   quit before save/build: nothing changed, exiting
    #   save only: saved as profile "Billy bob's favorite stuff"
}

sub install_profile {
    my ( $self, $easy, $profile ) = @_;

    if ( $easy->get_param('skip-bg') ) {
        $easy->install_profile($profile);
    }
    else {
        my ( $file, $spid ) = $easy->run_install_in_bg($profile);

        $easy->tail_file_while_pid(
            {
                'file' => $file,
                'pid'  => $spid,
            }
        );
    }
}

1;
