#!/bin/bash

# File rename script (clean.sh) v0.001c (beta test release)

# Copyright (C) 2000 Ralf Heiringhoff <ralf@freeze.rhwd.de>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc., 59
# Temple Place, Suite 330, Boston, MA 02111-1307 USA


#
# This Script will rename all files in the current directory to lower case
# filenames without any of those nasty chars.
# 
# before:
# !!!Yeah(we!-'----___will[rock=your\\\ass.txt
#
# after:
# yeah_we_will_rock_your_ass.txt

#
# Changelog:
#
#	Just started.
#
# TODO:
#	
#	add filename to|from id3 tag
#
# BUGS:
#
#   Report  bugs  to  ralf@freeze.rhwd.de. Include a complete,
#   self-contained example that  will  allow  the  bug  to  be
#   reproduced,  and say which version of clean.sh you are using.
		      
cleaned_files=''
z=0
y=0

function syntax_clean 
		{
		tr   	' ' '_' 			|\
		tr    	'A-Z' 'a-z' 			|\
		tr -s 	'!§$%&()={[]}\+\~.' 		|\
		tr    	'\\\#\`!§$%()={[]}\+~' '_'	|\
		tr    	'\&' '_and_'			|\
		tr 	\' '_' 				|\
		sed -e	's/\_\-/\-/g' 			|\
		sed -e	's/\-\_/\-/g' 			|\
		tr -s 	'\-\_'; 
		}
		

if [ "$1" = "mv" ]; then
	command=mv
elif [ "$1" = "test" ]; then
	command=echo
else
	echo -e "\nUse clean.sh [mv|test] ...\n"
	exit 0
fi

echo -n -e "\nProcessing " 

process='\-|-/-'


for files in *
	do
	
	process=`echo -n $process | sed -e 's/\(.\)\(.*\)/\2\1/'`
		echo -n $process | sed -e 's/\(.\).*/\1/'

	
	clean=`echo $files | syntax_clean`
	real_clean=`echo $clean | syntax_clean`
	
		while ! [ "$clean" = "$real_clean" ]; do
			clean=`echo $real_clean | syntax_clean`
			real_clean=`echo $clean | syntax_clean`
		done

	first_char=`echo $clean | head -c 1`
		while [ "$first_char" = "_" -o "$first_char" = "-" ]; do
			length=$((`echo $clean | wc -c`-1))
			clean=`echo $clean | tail -c $length`
			first_char=`echo $clean | head -c 1`
		done
	
	last_char=`echo -n $clean | tail -c 1`
		while [ "$last_char" = "_"  -o "$last_char" = "-" ]; do
			length=$((`echo -n $clean | wc -c`-1))
			clean=`echo $clean | head -c $length`
			last_char=`echo -n $clean | tail -c 1`
		done
		
	if ! [ "$files" = "$clean" ]; then
	
		if ! [ -e "$clean" ]; then
			$command "./$files" "$clean"
		else
			a=1
			while [ -e $clean-$a ]
				do
					a=$((a+1))
					y=$((y+1))
				done
			$command "./$files" "$clean-$a"
		fi
		
		cleaned_files="$cleaned_files $files"
	else
		z=$((z+1))
	fi
	echo -n 
done

if [ "$command" = "mv" ]; then

	echo -e "\n\nFile(s) renamed to a clean Name:\n"
		if [ -z "$cleaned_files" ]; then
			echo -e "No Files renamed ...\n"
		else
			echo -e "$cleaned_files\n"
		fi
		if [ "$z" -eq "0" ];then
			echo -e "All Files have been renamed ...\n"
		else
			echo -e "$z File(s) not renamed ...\n"
		fi
		if ! [ "$y" -eq "0" ];then
			echo -e "$y File(s) with duplicate Names detected ...\n"
		else
			echo -e "No duplicate Names have been detected ...\n"
		fi

elif [ "$command" = "echo" ]; then

	echo -e "\n\nFile(s) that would have been renamed to a clean Name:\n"
		if [ -z "$cleaned_files" ]; then
			echo "No File(s) would have been renamed ...\n"
		else
			echo "$cleaned_files\n"
		fi
	echo -e "\n$z File(s) would not have been renamed ...\n"
	echo -e "\n$y File(s) with duplicate Names detected ...\n"

fi
