#!/bin/sh

files=`find "$1" -type f`

for f in $files; do
    actual_size=`ls -l "$f" | awk '{print $5}'`
    expected_size=`echo $f | sed -e 's,.*S=\([0-9]\+\).*,\1,'`
    if file "$f" | grep gzip > /dev/null; then
        if test $actual_size -eq $expected_size; then
            real_size=`zcat "$f" | wc -c`
            # echo "$f (gzipped): expected $expected_size, actual $actual_size, real $real_size"
            real_filename=`echo $f | sed -e "s,\(.*\)S=[0-9]\+\(.*\),\1S=$real_size\2,"`
            echo "mv \"$f\" \"$real_filename\""
        fi
    else
        if test $actual_size -ne $expected_size; then
            # echo "$f (raw): expected $expected_size, actual $actual_size"
            real_filename=`echo $f | sed -e "s,\(.*\)S=[0-9]\+\(.*\),\1S=$actual_size\2,"`
            echo "mv \"$f\" \"$real_filename\""
        fi
    fi
done
