#!/bin/sh

# This is slow as it starts an iconv process per file,
# but it mitigates this by filtering out ascii only names beforehand.

. fslver

Usage() {
	ProgName=`basename "$0"`
	echo "find names with invalid UTF8 encoding.
Usage: $ProgName [[-r] [-f] paths(s) ...]

If no path(s) specified then the currrent directory is assumed."
	exit
}

for arg
do
	case "$arg" in
	-h|--help|-help)
		Usage ;;
	-v|--version)
		Version ;;
	*)
		argsToPassOn="$argsToPassOn $arg" ;;
	esac
done

. getfpf "$argsToPassOn"

find $findArgs -printf "$FPF\n" |
LANG=C grep -E '[^ -~]' | #filter out purely ASCII names
while read name; do
    echo "$name" | iconv --from=utf8 --to=utf8 >/dev/null 2>&1
    [ $? -ne 0 ] && echo "$name"
done |
tr '\n' '\0' |
if [ ! -p /proc/self/fd/1 ]; then
    xargs -r0 ls -b1Ud --color=auto --
else
    cat
fi
