#!/bin/sh


#We get stuff like this ( from stdin. redirect the file if you have to)
#1341234 534545 aljsdfljasdf
#1234141 818987 asdfafas
#1200034 989087 asdfasf
#1002412 234013 asdfasdf

count=0

# assumes the iod is hashing on 101 entries
# not as thourough a test..
if [ ! -d 000 ]; then
	echo "not in pvfs-data"
	exit -1
fi

# awk '{x = ($1 % 101 ); printf "%03d/f%d.0\n", x, $1}' 
while read old_inode new_inode filename; do
	OLD_NAME=$(printf "%03d/f%d.0" $(( old_inode % 101 )) $old_inode);
	NEW_NAME=$(printf "%03d/f%d.0" $(( new_inode % 101 )) $new_inode);
	mv "$OLD_NAME ${NEW_NAME}.migrate"
	count = $(( count + 1 ))
done

# if we migrated no files
if [ $count -eq 0 ]; then
	echo "error: no input files" 1>&2
	exit -1
fi

# Print notices for all files that we will remove on second pass.
find . -type f -name "*.saveme" -exec \
	echo notice: will remove old saved data file \{\} on second pass. \;

find . -type f -name "*.0" -exec \
	echo notice: will remove unreferenced data file \{\} on second pass. \;

find . -type f -not -name "*.0" -not -name "*.saveme" -not -name "*.migrate" \
	-exec echo notice: will remove unrecognized file \{\} on second pass. \;


echo "ctrl-c in 10 sec to abort file removal"
read -t 10 foo

# XXX: we won't actually remove anything. but it might not be 
# a bad idea, sysadmin, to clean these files out
#find . -type f -name "*.saveme" -exec rm \{\} \;
#find . -type f -name "*.0" -exec rm \{\} \;
#find . -type f -not -name "*.0" -not -name "*.saveme" -not -name "*.migrate" \
#	-exec rm \{\} \;

# strip off the .migrate 

find . -name \*.migrate |\
while read old; do
	mv $old ${old%.migrate}
done
