# Check for issues with PNG files

source "${PORTAGE_BIN_PATH}/helper-functions.sh" || exit 1

pngfix_check() {
	local chunksize=1 jobs pngfix=$(type -P pngfix) xargs=(${XARGS})

	if "${xargs[@]}" --help | grep -q -- --max-procs=; then
		jobs=$(makeopts_jobs)
		if [[ ${jobs} -gt 1 ]]; then
			xargs+=("--max-procs=${jobs}" -L "${chunksize}")
		fi
	fi

	if [[ -n ${pngfix} ]] ; then
		local pngout=()
		local next

		while read -r -a pngout ; do
			local error=""

			case "${pngout[1]}" in
				CHK)
					error='invalid checksum'
					;;
				TFB)
					error='broken IDAT window length'
					;;
			esac

			if [[ -n ${error} ]] ; then
				if [[ -z ${next} ]] ; then
					eqawarn "QA Notice: broken .png files found:"
					next=1
				fi
				eqawarn "   ${pngout[@]:7}: ${error}"
			fi
		done < <(find "${ED}" -type f -name '*.png' -print0 | "${xargs[@]}" -0 "${pngfix}")
	fi
}

pngfix_check
: # guarantee successful exit

# vim:ft=sh
