#!/bin/bash

PWD="$1"

if [ -z "$PWD" ]; then
	echo "Usage: $0 DIRNAME"
	exit 0
fi

FILELIST=$(find "$PWD" | grep " " | tr " " "?")

for F in $FILELIST; do
	while [ "$F" != "$PWD" ]; do
		if basename $(echo "$F" | tr " " "?") | grep -q " "; then
			DD=$(dirname $(echo "$F" | tr " " "?"))
			FF=$(basename $(echo "$F" | tr " " "?") | tr " " "_")
			echo "Rename: $(realpath $(echo $F | tr " " "?")) -> $DD/$FF"
			mv $(echo "$F" | tr " " "?") "$DD"/"$FF"
		fi
		F=$(dirname $(echo "$F" | tr " " "?"))
	done
done
