#!/bin/bash
#
#Beschreibung: stellt im Dateinamen den Zeichensatz auf utf-8 und ersetzt Leerzeichen und Umlaute
#
#benötigt: /usr/bin/convmv
#
#Aufruf: ms2linux.sh [Pfad]
#
#Hinweise und Fehler an tuxator@tuxator.de 
#
#Status: ok
###################################################################
typeset -i counter
typeset -i oldcounter
currentifs=${IFS}
IFS=$'\n'
if [[ ! $1 ]]; then
currentpath=${PWD}
else
currentpath=${1}
fi
if [[ -e /usr/bin/convmv ]]; then
	convmv -r -f iso-8859-15 -t utf-8 --notest * >&-
fi
find ${currentpath} -depth -name "*" | while read file ; do
	path=$(dirname ${file})
	oldfile=$(basename ${file})
	newfile=$(echo "${oldfile}" | sed -e 's/ /_/g' -e 's/_-_/-/g' -e 's/ä/ae/g' -e 's/ö/oe/g' -e 's/ü/ue/g' -e 's/Ä/Ae/g' -e 's/Ö/Oe/g' -e 's/Ü/Ue/g' -e 's/ß/sz/g')
	if [[ ${newfile} != ${oldfile} ]]; then
		counter=1
		while [[ -e "${path}/${newfile}" ]]
		do
			if (( ${counter} > 1 )); then
				oldcounter=${counter}-1
				newfile=$(echo "${newfile}" | sed "s/#${oldcounter}/#${counter}/g")
			else
				newfile=$(echo "${newfile}#${counter}")
			fi
			counter=${counter}+1
		done
		mv ${path}/${oldfile} ${path}/${newfile}
	fi
done
IFS=${currentifs}
###################################################################