#! /bin/bash
set -euo pipefail
## TOKEN's creation time marks the time since last recompression
TOKEN=.lastRecompression
if [ -f ${TOKEN} ]
then
find -name '*.gz' -cnewer "${TOKEN}"
else
# Process all compressed files if there is no token.
find -name '*.gz'
fi | while read f
do
# Do it in two steps
gunzip < "$f" | gzip --rsyncable > "$f.tmp"
# Preserve attributes
cp "$f" "$f.tmp" --attributes-only
# and rename atomically.
# set -e ensures that a problem in the previous step
# will stop the full script.
mv -v "$f.tmp" "$f"
done
# Update the token
touch ${TOKEN}