# -*- sh -*-

flameeyes_warning_if_file() {
    if [[ -s "${T}"/$1 ]]; then
	ewarn "Flameeyes QA Warning! $2"
	cat "${T}"/$1
	ewarn "Flameeyes QA Warning (end)! $2"
    fi
}

my_post_src_prepare() {
    find "${S}" -name configure.ac -o -name configure.in | while read acfile; do
	acdir=$(dirname "$acfile")
	pushd "$acdir" &>/dev/null
	autoconf --trace AC_CANONICAL_TARGET 2>/dev/null | sed "s:^:$(pwd)/:" > "${T}"/flameeyes-canonical-target.log
	popd  &>/dev/null
    done
    flameeyes_warning_if_file flameeyes-canonical-target.log "AC_CANONICAL_TARGET used"

#    egrep -rlZ '^#! */bin/sh$' "${S}" | xargs -0 file -i | grep 'text/x-shellscript' | sed -e 's$:[[:space:]]\+text/x-shellscript$$' | xargs /root/repos/anon/debian-devscripts/scripts/checkbashisms.pl &> "${T}"/flameeyes-bashisms.log
#    if [[ -s "${T}"/flameeyes-bashisms.log ]]; then
#	ewarn "Flameeyes QA Warning! sh scripts contain bashisms"
#	cat "${T}"/flameeyes-bashisms.log
#    fi
}

post_src_prepare() {
    my_post_src_prepare
}

post_src_unpack() {
    case ${EAPI} in
	2)
	    # Do nothing
	    ;;
	0|1)
	    my_post_src_prepare ;;
	*)
	    ewarn "Unknown EAPI! Running post_src_prepare just in case!"
	    my_post_src_prepare ;;
    esac
}

post_src_install() {
    # scanelf -q -F "#s%F" -R -s '-__(|l|f)xstat' "${D}" > "${T}"/flameeyes-scanelf-stat64.log
    # if [[ -s "${T}"/flameeyes-scanelf-stat64.log ]]; then
    # 	ewarn "Flameeyes QA Warning! Missing largefile support"
    # 	cat "${T}"/flameeyes-scanelf-stat64.log >/dev/stderr
    # fi

    rm -f "${T}"/flameeyes-scanelf-bundled.log
    for symbol in adler32 BZ2_decompress jpeg_mem_init XML_Parse avcodec_init png_get_libpng_ver lt_dlopen GC_stdout; do
	scanelf -qRs +$symbol "${D}" >> "${T}"/flameeyes-scanelf-bundled.log
    done
    flameeyes_warning_if_file flameeyes-scanelf-bundled.log "Possibly bundled libraries"

    rm -f "${T}"/flameeyes-scanelf-insecure.log
    for symbol in tmpnam tmpnam_r tempnam gets sigstack getpw getwd mktemp; do
	scanelf -qRs -$symbol "${D}" >> "${T}"/flameeyes-scanelf-insecure.log
    done
    flameeyes_warning_if_file flameeyes-scanelf-insecure.log "Insecure functions used"

    find "${D}" \
	\( -name '._*' -fprintf "${T}"/flameeyes-osx-forkfile.log "%P\n" \) , \
	\( -perm /6000 -fprintf "${T}"/flameeyes-setXid-binaries.log "%#m %u:%g %P\n" \) , \
	\( \( -path "${D}"usr/man/\* -or -path "${D}"usr/info/\* -or \
	-path "${D}"usr/X11R6/\* -or -path "${D}"usr/doc/\* -or \
	-path "${D}"usr/locale/\* -or -path "${D}"usr/lib/perl5/site_perl/\* -or \
	-path "${D}"usr/local/\* \
	\) -fprintf "${T}"/flameeyes-invalid-directory.log "/%P\n" \) , \
	\( -path "${D}"usr/share/doc/\* -type d -prune -not -name "${PF}" \
	   -fprintf "${T}"/flameeyes-invalid-directory.log "/%P\n" \) ,  \
	\( -path "${D}"usr/share/locale/\* -name '*.mo' \
	   -fprintf "${T}"/flameeyes-locales.log "/%P\n" \)

    if [[ -d "${D}"/usr/share/locale ]] && ! [[ -s "${T}"/flameeyes-locales.log ]]; then
	eqawarn "No locales installed (bug #264114)"
    fi

    flameeyes_warning_if_file flameeyes-invalid-directory.log "Invalid directories in image"
    flameeyes_warning_if_file flameeyes-osx-forkfile.log "OSX fork files found (._*)"
    flameeyes_warning_if_file flameeyes-setXid-binaries.log "setXid files found"
}

make() {
    if [[ "${FUNCNAME[1]}" == "einstall" ]] ; then
	emake -j1 "$@"
    else
        eqawarn "/etc/portage/bashrc QA notice: 'make' called by ${FUNCNAME[1]}"
        emake "$@"
    fi
}