#!/bin/bash



#Preupgrade Assistant performs system upgradability assessment
#and gathers information required for successful operating system upgrade.
#Copyright (C) 2013 Red Hat Inc.
#Petr Pisar <ppisar@redhat.com>
#
#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 3 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, see <http://www.gnu.org/licenses/>.
. /usr/share/preupgrade/common.sh
check_applies_to "perl-libs"
check_rpm_to "perl" ""
#END GENERATED SECTION

FOREIGN_FOUND=0
log_file() {
    printf '%s\n' "$@" >> "$SOLUTION_FILE" || exit_error
    FOREIGN_FOUND=1
}
is_module_rh() {
    
    if [ $# -ne 1 ]; then
         return 1
    fi
    
    RPM_NAME=$(rpm -qf --qf '%{NAME}\n' "$1")
    
    if [ $? -eq 1 ]; then
        log_slight_risk "The $1 Perl module is not handled by any package."
        log_file "$1"
    else
        is_dist_native $RPM_NAME
        if [ $? -ne 0 ]; then
            log_slight_risk "The $1 Perl module was not installed by any package signed by CentOS."
            log_file "$1"
        fi
    fi
}

cat > "$SOLUTION_FILE" <<'EOM'
Perl was updated from version 5.10 to version 5.16. Read the Perl
section in the "CentOS 7 Developer Guide" for more details:
[link:https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/Developer_Guide/lib.details.html#libraries.perl]

The following Perl module files located in system Perl paths are either not
handled by any package or not signed by CentOS:

EOM

PERL_DIRS=$(perl -MConfig -e '$,=q{ }; print @Config{installarchlib,installprivlib,installvendorarch,installvendorlib}') \
    || exit_error
#Limiting the lookup to local directories only
for dir in ${PERL_DIRS[@]};
do 
    fs_type=$(stat -f -L -c %T "$dir")
    if  echo "$fs_type" | egrep -q 'nfs|nfs4|autofs|cifs|sshfs|fuseblk'; then
        PERL_DIRS=( "${PERL_DIRS[@]/$dir}")
        log_info "Directory $dir is not on the local filesystem and it will be skipped in the analysis"
    fi
done
for file in $(find -P $PERL_DIRS -type f -name '*.pm'); do
    is_module_rh "$file"
done

test "$FOREIGN_FOUND" = 0 && exit_informational || exit_fail
