package Cpanel::ServiceManager::Services::Cpanel_ccs;

# cpanel - Cpanel/ServiceManager/Services/Cpanel_ccs.pm
#                                                    Copyright 2019 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;

use parent 'Cpanel::ServiceManager::Base';

use Cpanel::Locale::Lazy       ();
use Cpanel::LoadModule::Custom ();

our $CCS_DIR = '/opt/cpanel-ccs';

sub service_name    { return 'cpanel-ccs' }
sub service_package { return ['cpanel-ccs-calendarserver'] }
sub ports           { return [ 2079, 2080 ] }
sub support_reload  { return 1; }
sub is_enabled      { return 1; }
sub is_configured   { return 1; }

sub get_service_description_hashref {

    my $locale = Cpanel::Locale::Lazy::lh();
    return {
        'name'        => $locale->maketext('Calendar and Contacts Server (CCS) Plugin'),
        'description' => $locale->maketext(
            'The Calendar and Contacts Server (CCS) plugin. ' . 'Disabling this plugin does not uninstall it. ' . 'You can uninstall this plugin from the Manage Plugins interface.',
        ),
    };
}

sub check {
    my ($self) = @_;
    return 0 if !$self->SUPER::check(@_);

    # Get password from file
    my $ccs_pass_file = "$CCS_DIR/conf/.ccspass";
    my $pass          = "";
    if ( -s $ccs_pass_file ) {
        open( my $fh, "<", $ccs_pass_file );
        $pass = <$fh>;
        chomp($pass);
        close $fh;
    }
    else {
        $self->logger()->warn("Could not open “$ccs_pass_file”: File does not exist");
    }

    # Check password
    require Cpanel::PwCache;
    require Cpanel::CheckPass::UNIX;
    my $hashed_pass = ( Cpanel::PwCache::getpwnam("cpanel-ccs") )[1];
    my $password_ok = Cpanel::CheckPass::UNIX::checkpassword( $pass, $hashed_pass );
    if ( !$password_ok ) {
        require Cpanel::Exception;
        die Cpanel::Exception::create_raw( "ConnectionFailed", "Could not authenticate as “cpanel-ccs”" );
    }

    return 1;
}

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

    # Overriding the start so that I can do a check afterwards
    return 0 if !$self->SUPER::start(@_);
    if ( !eval { $self->check() } ) {
        $self->logger()->info( q{The service '} . $self->service() . q{' failed the check after restart. The system will now reset the cpanel-ccs user password and rebuild the caldavd.plist file.} );
        unlink '/opt/cpanel-ccs/conf/.ccspass';
        Cpanel::LoadModule::Custom::load_perl_module("Cpanel::CCS::Utils");
        local $@;
        eval { Cpanel::CCS::Utils::rebuild_ccs_conf(); };
        if ($@) {
            $self->logger()->warn("Rebuilding CCS config failed: $@");
            return 0;
        }
        else {
            $self->logger()->info("Rebuilt CCS config successfully.");
        }
        return 0 if ( !$self->SUPER::start(@_) || !eval { $self->check() } );
    }

    return 1;
}

1;
