#!/bin/sh

# $Id: prerequisites_imapsync,v 1.40 2025/09/20 13:35:11 gilles Exp gilles $

MODULES_MANDATORY='
App::cpanminus
Authen::NTLM
CGI
Compress::Zlib
Crypt::OpenSSL::RSA
Data::Dumper
Data::Uniqid
Digest::HMAC
Digest::HMAC_MD5
Digest::MD5
Digest::SHA
Email::Address
Encode
Encode::Byte
Encode::IMAPUTF7
File::Copy::Recursive
File::Tail
HTTP::Daemon
HTTP::Daemon::SSL
HTTP::Request
HTTP::Response
HTTP::Status
URI
IO::Socket::INET
IO::Socket::INET6
IO::Socket::SSL
IO::Tee
JSON
JSON::WebToken
JSON::WebToken::Crypt::RSA
HTML::Entities
LWP::UserAgent
Mail::IMAPClient
MIME::Base64
Module::Implementation
Module::Runtime
Module::ScanDeps
Net::Server
Net::SSLeay
Net::DNS
Package::Stash
Package::Stash::XS
PAR::Packer
Parse::RecDescent
Pod::Usage
Proc::ProcessTable
Readonly
Regexp::Common
Sys::MemInfo
Term::ReadKey
Test::MockObject
Test::More
Test::Pod
Test::Deep
Text::ParseWords
Try::Tiny
Unicode::String
'

MODULES_FOR_CODERS='
Dist::CheckConflicts
Smart::Comments
'


test_perl() {
# First we need perl

	if perl -v > /dev/null 2>&1 ; then
		perl_version=`perl -e 'printf "%vd\n", $^V;'`
		echo Ok: Found Perl $perl_version
		return 0
	else
		echo 'Failure: Perl is not here. You have to install Perl first.'
		return 1
	fi
}

test_make() {
# Second we need make to build some Perl modules

	if echo foo: | make -f - foo ; then
		make_version=`make -v |head -1`
		echo Ok: Found make $make_version
		return 0
	else
		echo 'Failure: make is not here. You have to install the "make" command.'
		return 1
	fi
}


test_cpanm() {
# Second we need make to build some Perl modules
        # redirect "cpanm ... < /dev/null" is there for macos buid via ssh
        # no clue why it's necessary
	if cpanm -h > /dev/null 2>&1 < /dev/null; then
		cpanm_version=`cpanm --version < /dev/null | head -1`
		echo Ok: Found cpanm $cpanm_version
		return 0
	else
		echo 'Failure: cpanm is not here. You have to install the "cpanm" command.'
		return 1
	fi
}



test_module() {
	test -n $1 || return
	M_tested=$1
	shift
	if perl -m"$M_tested" -e '' >/dev/null 2>&1 ; then
		echo "Ok: Found Perl module $M_tested"
	else
		echo "Failure: Not found Perl module $M_tested $@"
		LIST_TO_INSTALL="$LIST_TO_INSTALL $M_tested"
	fi
        return
}

test_mandatory_modules() {
	for M in $MODULES_MANDATORY
	do
		test_module $M
	done
}


search_modules_any()
{
	test -n "$*" || {
		echo "All needed modules are already installed"
		return
	}
        
        # Darwin
        sw_vers > /dev/null 2>&1 && 
        {
		search_modules_cpanm "$@"
		return
	}

        
        # Debian, Ubuntu & Co
	apt-cache -h > /dev/null 2>&1 &&
        {
		search_modules_apt "$@"
		return
	}
        
        # Centos & Co
	yum -h > /dev/null 2>&1 &&
        {
		search_modules_yum  "$@"
		return
	}
        
        # ArchLinux & Co
        pacman -h > /dev/null 2>&1 &&
        {
		search_modules_pacman  "$@"
		return
	}
        
        # FreeBSD
	pkg version > /dev/null 2>&1 &&
        {
		search_modules_freebsd  "$@"
		return
	}
        
	# no yum, no apt-get, no pacman
	{
		search_modules_cpanm "$@"
		return
	}
} 



search_modules_cpanm() {
		cat <<EOD
Here is a cpanm command to install missing Perl modules:
cpanm $@
EOD
}

search_modules_pacman() {
	echo 
	echo Searching pacman packages names
        #echo pacman -Fy
        #pacman -Fy 
        echo pacman -S --noconfirm --needed  pkgfile
        pacman -S --noconfirm --needed  pkgfile
        echo pkgfile --update
        pkgfile --update
	for M in "$@" ; do
		echo "==== Searching pacman package name for $M"
		F=`echo $M|tr -s ":" "/"`.pm
                
                # Not very good "pacman -Fs"
		#echo pacman -Fs  "$F"
		#echo
		#pacman -Fs  "$F"
                #echo
                # Better! pkgfile --reg
                echo "pkgfile --reg $F | grep perl-"
                echo
                pkgfile --reg "$F" | grep perl-
		echo
	done
}

search_modules_yum() {
	echo 
	echo Searching rpm packages names
	for M in "$@" ; do
		echo "==== Searching rpm package name for $M"
		F=`echo $M|tr -s ":" "/"`.pm
		echo yum -q whatprovides "*/$F"
		echo
		yum -q whatprovides "*/$F"
		echo
	done
}

search_modules_apt() {
	echo 
	echo Searching deb packages names
	for M in "$@" ; do
		F=`echo $M|tr -s ":" "/"`.pm
		echo "==== Searching deb package name for $M with: apt-file search /$F"
		#echo apt-file search /$F
		echo
                if apt-file -h > /dev/null 2>&1
                then
                        apt-file search /$F
                else
                        echo "apt-file is not installed. Suggestion: apt-get install apt-file; apt-file update"
                fi
                echo
		echo "==== Searching deb package name for $M with: apt-cache search $M"
		#apt-cache search "$M"
                apt-cache search "$M"
		echo
	done
}

search_modules_freebsd()
{
	echo 
	echo Searching pkg FreeBSD names
	for M in "$@" ; do
		F=`echo $M|tr -s ":" "-"`
		echo "==== Searching deb package name for $M with: pkg search p5-$F"
		echo
		pkg search "p5-$F"
                echo
	done

}

list_to_install() {
	test -n "$LIST_TO_INSTALL" || return 0
	echo
	echo 'What you have to do before using imapsync:'
	for M in $LIST_TO_INSTALL ; do
		echo "Install Perl module $M"
	done
        # return false/bad if some modules are missing.
        return 1
}

test_unix() {
	echo '$SHELL says ' $SHELL
	echo '$0 gives ' $0 
	echo -n "ps -ef gives " ; ps -ef | grep $$ | grep -v grep| grep -v 'ps -ef'
	sw_vers > /dev/null 2>&1 && sw_vers # Darwin
	lsb_release -dirc > /dev/null 2>&1 && lsb_release -dirc # Linux
	uname -a
}


main()
{
        test_unix
        test_perl || return 1
        test_make || return
        test_mandatory_modules
        list_to_install
        EXIT=$?

        # Help the user to install missing modules
        search_modules_any $LIST_TO_INSTALL

        if test "$1" = "MODULES_MANDATORY"
        then
                search_modules_any $MODULES_MANDATORY
        elif test -n "$1" 
        then
                search_modules_any "$@"
        fi

        test_cpanm
        return $EXIT
}

main "$@"


