#!/usr/bin/perl -w
# --
# bin/otrs.checkModules - to check needed cpan framework modules
# Copyright (C) 2001-2008 OTRS AG, http://otrs.org/
# --
# $Id: otrs.checkModules,v 1.24 2008/05/08 09:36:57 mh Exp $
# --
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
# --

# use ../ as lib location
use File::Basename;
use FindBin qw($RealBin);
use lib dirname($RealBin);
use lib dirname($RealBin) . "/Kernel/cpan-lib";

# config
my @NeededModules = (
    {
        Module   => 'CGI',
        Required => 1,
    },
    {
        Module   => 'Date::Pcalc',
        Required => 1,
    },
    {
        Module   => 'Date::Format',
        Required => 1,
    },
    {
        Module   => 'DBI',
        Required => 1,
    },
    {
        Module   => 'DBD::mysql',
        Required => 1,
    },
    {
        Module   => 'Digest::MD5',
        Required => 1,
    },
    {
        Module   => 'Crypt::PasswdMD5',
        Required => 1,
    },
    {
        Module   => 'LWP::UserAgent',
        Required => 1,
    },
    {
        Module   => 'IO::Scalar',
        Required => 1,
    },
    {
        Module   => 'IO::Wrap',
        Required => 1,
    },
    {
        Module   => 'MIME::Base64',
        Required => 1,
    },
    {
        Module   => 'Mail::Internet',
        Required => 1,
    },
    {
        Module   => 'MIME::Tools',
        Required => 1,
        Version  => '5.423',
    },
    {
        Module       => 'Net::DNS',
        Required     => 1,
        NotSupported => [
            {
                Version => 0.60,
                Comment =>
                    'This version is broken and not useable, please upgrade to a higher version!',
            },
        ],
    },
    {
        Module   => 'Net::POP3',
        Comment  => 'for POP3 connections',
        Required => 1,
        Depends  => [
            {
                Module   => 'Net::POP3::SSLWrapper',
                Required => 0,
                Comment  => 'for SSL connections',
            },
        ],
    },
    {
        Module   => 'Net::IMAP::Simple',
        Comment  => 'for IMAP connections',
        Required => 0,
        Depends  => [
            {
                Module   => 'Net::IMAP::Simple::SSL',
                Required => 0,
                Comment  => 'for SSL connections',
            },
        ],
    },
    {
        Module   => 'Net::SMTP',
        Required => 0,
        Comment  => 'for SMTP connections',
        Depends  => [
            {
                Module   => 'Authen::SASL',
                Required => 0,
                Comment  => 'for SMTP backend',
            },
        ],
    },
    {
        Module   => 'Net::LDAP',
        Required => 0,
        Comment  => 'for directory authentication',
    },
    {
        Module   => 'GD',
        Required => 0,
        Comment  => 'for stats',
        Depends  => [
            {
                Module   => 'GD::Text',
                Required => 0,
                Comment  => 'for stats',
            },
            {
                Module   => 'GD::Graph',
                Required => 0,
                Comment  => 'for stats',
            },
            {
                Module   => 'GD::Graph::lines',
                Required => 0,
                Comment  => 'for stats',
            },
            {
                Module   => 'GD::Text::Align',
                Required => 0,
                Comment  => 'for stats',
            },
        ],
    },
    {
        Module   => 'PDF::API2',
        Version  => 0.57,
        Required => 0,
        Comment  => 'for PDF required!',
        Depends  => [
            {
                Module   => 'Compress::Zlib',
                Required => 0,
                Comment  => 'for PDF required!',
            },
            ]
    },
    {
        Module   => 'XML::Parser',
        Required => 0,
        Comment  => 'for faster xml speed'
    },
);

# try to load modules
my $Depends = 0;
foreach my $Module (@NeededModules) {
    _Check( $Module, $Depends );
}
exit;

sub _Check {
    my ( $Module, $Depends ) = @_;

    for ( 0 .. $Depends ) {
        print "   ";
    }
    print "o $Module->{Module}";
    my $Length = length( $Module->{Module} ) + ( $Depends * 3 );
    for ( $Length .. 30 ) {
        print ".";
    }
    if ( eval "require $Module->{Module}" ) {

        # some strange CPAN module do not export VERSION
        my $Version = eval "\$$Module->{Module}::Version::VERSION";

        # ask for CPAN module VERSION
        if ( !$Version ) {
            $Version = eval "\$$Module->{Module}::VERSION";
        }
        if ( $Module->{Version} ) {
            if ( $Version >= $Module->{Version} ) {
                print "ok (v$Version)\n";
            }
            else {
                print
                    "failed!!! Version $Version installed but $Module->{Version} or higher is required!\n";
            }
        }
        elsif ( $Module->{NotSupported} ) {
            my $NotSupported = 0;
            for my $Item ( @{ $Module->{NotSupported} } ) {
                if ( $Item->{Version} == $Version ) {
                    $NotSupported = $Item->{Comment};
                    last;
                }
            }
            if ($NotSupported) {
                print "failed!!! Version $Version not supported! $NotSupported\n";
            }
            else {
                print "ok (v$Version)\n";
            }
        }
        else {
            print "ok (v$Version)\n";
        }
    }
    else {
        my $Comment = $Module->{Comment} || '';
        my $Required = $Module->{Required};
        if ($Required) {
            $Required = 'required - use "perl -MCPAN -e shell;"';
        }
        else {
            $Required = 'not required';
        }
        print "not installed! ($Required / $Comment)\n";
    }

    if ( $Module->{Depends} ) {
        for my $ModuleSub ( @{ $Module->{Depends} } ) {
            _Check( $ModuleSub, $Depends + 1 );
        }
    }
}

