mail-aging/move-mails.sh
2021-11-22 14:02:05 +01:00

95 lines
1.9 KiB
Bash
Executable File

#!/bin/bash
FOLDER_FROM=''
FOLDER_TO=''
DAYS=20
print_help () {
cat << EOF
Usage:
$(basename "$0") <options>
Possible options:
-f|--from <folder> Folder where to look for mails
-t|--to <folder> Folder to put outdated mails
-d|--days <days> Number of days to keep the mails
EOF
}
while [ $# -gt 0 ]
do
case "$1" in
--from|-f)
FOLDER_FROM="$2"
shift
;;
--to|-t)
FOLDER_TO="$2"
shift
;;
--days|--day|-d)
DAYS="$2"
shift
;;
--help|-h)
print_help
exit
;;
*)
echo "Unrecognized option: $1"
print_help
exit 1
;;
esac
shift
done
ensure_mail_folder () {
local folder="$1"
if ! echo "$folder" | grep '/cur/*$' > /dev/null; then
folder="$folder/cur/"
fi
if [ ! -d "$folder" -o ! -w "$folder" -o ! -r "$folder" -o ! -x "$folder" ]; then
echo "This is no valid folder: $folder"
exit 1
fi
echo -n "$folder"
}
if [ -z "$FOLDER_FROM" ]; then
echo "There is no folder given to read the mails from. Please provide it with --from"
exit 1
fi
if [ -z "$FOLDER_TO" ]; then
echo "There is no folder given to put the outdated mails. Please provide it with --to"
exit 1
fi
FOLDER_FROM=$(ensure_mail_folder "$FOLDER_FROM")
FOLDER_TO=$(ensure_mail_folder "$FOLDER_TO")
if [ ! "$DAYS" -ge 0 ]; then
echo 'No valid number of days given.'
exit 1
fi
find "$FOLDER_FROM" -type f -mtime +$DAYS | while read l
do
suffix=$(echo "$l" | grep -o ',[^,]*$')
if ! echo "$suffix" | grep 'S' > /dev/null ; then
# echo Unseen Mail $l
continue
fi
if echo "$suffix" | grep 'F' > /dev/null ; then
# echo Flagged Mail $l
continue
fi
mv -n "$l" "$FOLDER_TO"
done