#!/bin/bash


. /usr/share/preupgrade/common.sh

#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 Red Hat."
            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 "Red Hat Enterprise Linux 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 Red Hat:

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
