#!/usr/local/cpanel/3rdparty/bin/perl
#                                      Copyright 2025 WebPros International, LLC
#                                                           All rights reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited.

package autofixer2::remove_carddav_sample;

use strict;
use warnings;
use constant DIR_TO_REMOVE => '/usr/local/cpanel/base/3rdparty/roundcube/plugins/carddav/dbmigrations/0000-sample';

# This autofixer is associated with CPANEL-47568
exit run() unless caller;

sub run {
    return 0 unless -d DIR_TO_REMOVE;
    return 0 unless supported_on_this_major( 110, 999 );
    rmdir(DIR_TO_REMOVE) or die sprintf( 'Failed to remove directory “%s”: %s', DIR_TO_REMOVE, $! );
    return 0;
}

# Do we run this code?
sub supported_on_this_major {
    my ( $min_ver, $max_ver ) = @_;

    my $major = get_major_version();

    return 0 if $major < $min_ver;
    return 0 if $major > $max_ver;

    return 1;
}

sub get_major_version {
    my $major_version;

    if ( open( my $fh, '<', '/usr/local/cpanel/version' ) ) {
        my $full_version = <$fh>;
        chomp $full_version;
        close($fh);
        ($major_version) = $full_version =~ /^[0-9]+\.([0-9]+)/;
    }

    # Safe default.
    return $major_version || 30;
}

1;
