package Cpanel::Easy::PHP5::SysTimezone;

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

our $easyconfig = {
    'name'   => 'System Timezone',
    'note'   => 'Use system timezone data in /usr/share/zoneinfo instead of PHP timezonedb',
    'url'    => 'http://pecl.php.net/package/timezonedb',
    'dryrun' => {
        '0' => {
            'name'    => 'Check zoneinfo',
            'command' => sub {
                my ($self) = @_;
                my $default_zoneinfo = '/usr/share/zoneinfo';

                if ( -d $default_zoneinfo ) {
                    $self->{'__'}{'tzdir'} = $default_zoneinfo;
                }
                else {
                    return ( 0, 'Timezone directory not found' );
                }

                return ( 1, 'ok' );
            },
        },
    },
    'step' => {
        '0' => {
            'name'    => 'Apply system-tzdata patch',
            'command' => sub {
                my ($self) = @_;

                my $php_version = '';
                my $pns         = "Cpanel::Easy::PHP5";
                if ( $self->{'working_profile'}{$pns} ) {
                    for my $spec ( $pns->versions() ) {
                        if ( $self->{'working_profile'}{ $pns . '::' . $spec } ) {
                            $php_version = $pns . '::' . $spec;
                            next;
                        }
                    }
                }
                return ( 0, 'Could not determine PHP version' ) unless ($php_version);

                my $pp_ec = $self->get_easyconfig_hr_from_ns_variations($php_version);
                return ( 0, 'Could not find parent PHP path info' )
                  if ref $pp_ec ne 'HASH' || !exists $pp_ec->{'src_cd2'};

                require File::Spec;    # Already brought in, but just in case...
                my $path = File::Spec->catdir( $self->{'opt_mod_src_dir'}, $pp_ec->{'src_cd2'} );
                return $self->apply_patch( '../cppatch/php5-use_embedded_timezonedb.patch', $path );
            },
        },
    },
};

1;
