From d48043eab4deaaeda0f4795b183501116ade247c Mon Sep 17 00:00:00 2001 From: Christian Wolf Date: Mon, 22 Nov 2021 12:10:42 +0100 Subject: [PATCH] Create initial version --- move-mails.sh | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100755 move-mails.sh diff --git a/move-mails.sh b/move-mails.sh new file mode 100755 index 0000000..7bb8c8b --- /dev/null +++ b/move-mails.sh @@ -0,0 +1,89 @@ +#!/bin/bash + +FOLDER_FROM='' +FOLDER_TO='' +DAYS=20 + +print_help () { + cat << EOF +Usage: + $(basename "$0") + +Possible options: + -f|--from Folder where to look for mails + -t|--to Folder to put outdated mails + -d|--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 + + mv -n "$l" "$FOLDER_TO" +done