dotfiles/.bin/compress_maildir.sh
2023-02-22 08:31:38 +01:00

43 lines
1.1 KiB
Bash
Executable File

#!/bin/ksh
set -e # fail on error
# mail permission
UID=sdk
GID=sdk
MODE=600
BACKUP=0 # if 1, bak directory will be created within maildir
[[ -z $1 ]] && \
print "$0 <maildir>" && exit 2
# conditions:
# - filename as size S=
# - filename does not end in Z (already compressed)
# - file must not return gzip compressed data (already compressed)
# filename is cut from the file(1) output
FILES=$(cd "$1/cur/" \
&& find . -iname '*,S=*[^Z]' -exec file {} \; \
| grep -v 'gzip compressed data' \
| sed 's/: /!/' \
| cut -d '!' -f 1)
[[ $BACKUP != 0 ]] && mkdir "$1/bak"
# loop through mail and compress appending Z
# sync mtime with original file
# set used,group,permissions
# remove original mail (or backup)
for MAIL in $FILES; do
print "gzipping $1/cur/$MAIL to $1/cur/${MAIL}Z"
gzip -8 -S Z "$1/cur/$MAIL" -c > "$1/cur/${MAIL}Z"
touch -r "$1/cur/$MAIL" "$1/cur/${MAIL}Z"
chown $UID:$GID "$1/cur/${MAIL}Z"
chmod $MODE "$1/cur/${MAIL}Z"
if [[ $BACKUP != 0 ]]; then
mv "$1/cur/$MAIL" "$1/bak/$MAIL"
else
rm -f "$1/cur/$MAIL"
fi
done