#!/bin/bash

icnt=0
cnt=0

# I don't know of a good way to find only maildir mail files.
FILES=`find /path/to/mail/user -name \*\.icewarp\,\*`

for f in $FILES; do
	fname=${f##*/}
	path=${f%/*}
#	echo "in  : $fname"

	let "icnt+=1"

	# Split the filename into string parts
	# for reassembly later and to get the size
	remainder=$fname
	part1="${remainder%%=*}"; remainder="${remainder#*=}"
	size="${remainder%%,*}"; remainder="${remainder#*,}"
	remainder="${remainder#*=}"
	wsize="${remainder%%:*}"; remainder="${remainder#*:}"
	rsize=`stat -c%s $f`

	# Don't continue processing if rsize is empty or if size is ok.
	[[ -z "$rsize" ]] && continue
	[[ "$size" == "$rsize" ]] && continue
 
	echo "in  : $fname"
#	echo "sizes: S=$size,W=$wsize  real: $rsize"
	out="$part1=$rsize,W=$rsize:$remainder"
	echo "out : $out"

#	Check operation before doing the move.
	echo mv $path/$fname $path/$out
#	mv $path/$fname $path/$out

	let "cnt+=1"
done

echo $cnt of $icnt files affected.




