#!/bin/sh
#	Id$: batch/rename-utf8	1.71	29.12.2016
#
#	(c) 1994-2016, Dirk Meyer, Im Grund 4, 34317 Habichtswald
#
#
# Anzahl der Argumente ist 0, Benuzer informieren
if test $# = 0
then
	echo								>&2
	echo "Usage:	${0##*/} Dateien [...]"				>&2
	echo								>&2
	echo "	Die angegeben Dateinamen werden entsprechend dem"	>&2
	echo "	von ISO nach UTF8 konvertiert."				>&2
	echo								>&2
	exit 64
fi
#
rename_a_file() {
	if test -d "${A}"
	then
		( cd "${A}" && "${0}" . )
	fi
	B=`echo "${A}" | iconv -f latin1 -t utf-8`
	if test "${B}" = ""
	then
		# error conversion
		continue
	fi
	if test "${A}" = "${B}"
	then
		# no differ
		continue
	fi
	echo "${A}" > "/tmp/rename-latin1.test"
	file=`file "/tmp/rename-latin1.test"`
	case "${file}" in
	*': UTF-8 Unicode text'*)
		`iconv -f utf-8 -t latin1 < "/tmp/rename-latin1.test" > "/tmp/rename-latin1.88"`
		file2=`file "/tmp/rename-latin1.88"`
		case "${file2}" in
		*': ISO-8859 text'*)
			# already converted
			continue
			;;
		*': UTF-8 Unicode text'*)
			C=`echo "${A}" | iconv -f utf-8 -t latin1`
			if test "${C}" = ""
			then
				# error conversion
				continue
			fi
			echo \
			mv -i "${A}" "${C}"
			mv -i "${A}" "${C}"
			;;
		esac
		continue
		;;
	*': ISO-8859 text'*)
		;;
	*)
		echo "${file}"
		;;
	esac
	echo \
	mv -i "${A}" "${B}"
	mv -i "${A}" "${B}"
}

if test "${1}" = "."
then
	ls |
	while read A
	do
		rename_a_file
	done
else
	for A in "$@"
	do
		rename_a_file
	done
fi
#
# eof
