package Cpanel::Easy::OptLib::imap;

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

sub path_installed {
    my ($easy_obj) = @_;

    my $make_opt = 'slx';
    my $opt_dir  = '/opt/php_with_imap_client';
    my $ver      = '2007f';
    my $cver     = "$ver-cpanel";

    if ( -e '/etc/krb5.conf' ) {
        $make_opt .= ' EXTRAAUTHENTICATORS=gss';
    }

    $make_opt .= " SSLDIR=" . $easy_obj->get_openssl_prefix( { 'skip_mach' => 1 } );
    my $cflags  = '';
    my $ldflags = '';
    if ( $easy_obj->{'cpu_bits'} eq '64' ) {
        $cflags = '-fPIC -DPIC';
    }
    if ( -d '/usr/kerberos/include' ) {
        $cflags .= $cflags ? ' ' : '';
        $cflags .= '-I/usr/kerberos/include';
    }

    if ( -d '/usr/kerberos/lib64' ) {
        $ldflags = '-L/usr/kerberos/lib64';
    }
    elsif ( -d '/usr/kerberos/lib' ) {
        $ldflags = '-L/usr/kerberos/lib';
    }

    $make_opt .= " EXTRACFLAGS='$cflags' EXTRALDFLAGS='$ldflags'";

    return {
        'name'         => 'imap',
        'install_path' => "$opt_dir/",
        'itis_up2date' => sub {
            `cat $opt_dir/version 2>/dev/null` =~ m{ $cver }xms || `cat $opt_dir/version 2>/dev/null` =~ m{ keepmine }xms;

            # if they have a system one they want to use:
            #   1) make $opt_dir/c-client.a a symlink to theone they want to use
            #   2) echo keepmine > $opt_dir/version
            #   3) rebuild this option
        },
        'working_path' => "imap-$ver",
        'command_list' => [
            ["echo y | make $make_opt SSLTYPE=nopwd"],                                        ["mkdir $opt_dir 2>/dev/null || true"],
            ["rm -rf $opt_dir/c-client.a"],                                                   ["mv -f c-client/c-client.a $opt_dir/"],
            ["rm -rf $opt_dir/version"],                                                      ["echo $cver > $opt_dir/version"],
            ["mkdir $opt_dir/lib 2>/dev/null || true"],                                       ["ln -s $opt_dir/c-client.a $opt_dir/lib/c-client.a 2>/dev/null || true"],
            ["ln -s $opt_dir/lib/c-client.a $opt_dir/lib/libc-client.a 2>/dev/null || true"], ["rm -rf $opt_dir/include || true"],
            ["mkdir $opt_dir/include 2>/dev/null || true"],                                   ["cp -fL c-client/*.h $opt_dir/include 2>/dev/null || true"]
        ],
    };
}

1;
