#!/bin/bash

if [ "$1" = "-n" ]; then
  debug=yes
  shift
fi

function do_move() {

  if [ "$debug" = "yes" ]; then
    echo "mv $1 $2"
  else
   if [ -x "$2" ]; then
    if cmp "$1" "$2" > /dev/null 2>&1 ; then
	echo "$1 and $2 are identical"
	rm -v "$2"
    else
	echo "$1" and "$2" are not identical.
    fi
   else
    mv -v -- "$1" "$2"
   fi
  fi

}



#ls -1a | while read i; do
for i in *; do
  j=""
  case "$i" in
    *" "*|*"("*|*")"*|*\[*|*\]*|*\&*|*"'"*)
      j=$(echo "$i" | \
	sed 's/(//g' | \
	sed 's/&/+/g' | \
	sed 's/ /_/g' | \
	sed 's/)//g' | \
	sed 's/\[/_/g' | \
	sed 's/\]/_/g' | \
	sed s/\'//g  
	)
	if [ "$i" != "$j" ]; then
	      do_move "$i" "$j"
	fi
    ;;
    *)
    ;;
  esac
done

