#!/usr/bin/bash

# Useful directories.
readonly MAILDIR="${HOME}/Maildir"
readonly SPAMDIR="${MAILDIR}/Spam"
#readonly HAMDIR="${MAILDIR}/Ham"

# The spam/ham found are saved for future reference.
readonly SAVE_SPAM_DIR="${HOME}/spamassassin/spam/"
readonly SAVE_HAM_DIR="${HOME}/spamassassin/ham/"

# The minimum age (in days) of spam to be processed.
readonly SPAM_MIN_AGE="5"

# "--force" flag.
FORCE="false"

# The temporary file where a list of spam filenames will be stored.
SPAM_LIST=$(mktemp)
readonly SPAM_LIST
HAM_LIST=$(mktemp)
readonly HAM_LIST

trap 'rm -f "${SPAM_LIST}" "${HAM_LIST}"' EXIT

readonly LOGFILE="${HOME}/spam-ham-learn.log"

log()
{
    printf "I: %s" "$*" >> "${LOGFILE}"
}

learn_spam()
{
    local findargs=""

    log "Entering learn_spam"

    if [ "${FORCE}" = "false" ]; then
        log "--force detected; not using -ctime on find"
        findargs=" -ctime +${SPAM_MIN_AGE} "
    fi

    # Collecting the spam.
    log "Collecting spam"
    find "${SPAMDIR}/cur" "${SPAMDIR}/new" -type f "${findargs}" | while read -r msg; do
        log "Found ${msg}, adding it to the spam list (${SPAM_LIST})"
        printf "%s\n" "${msg}" >> "${SPAM_LIST}"
    done

    # Invoking sa-learn.
    log "Invoking sa-learn"
    /usr/bin/sa-learn -f "${SPAM_LIST}"

    # Saving the spam into a different location.
    log "Saving the spam into a different location"
    while read -r msg; do
        log "Moving ${msg} into ${SAVE_SPAM_DIR}"
        mv "${msg}" "${SAVE_SPAM_DIR}"
    done < "${SPAM_LIST}"
}

learn_ham()
{
    local nspam

    log "Entering learn_ham"

    nspam=$(wc -l "${SPAM_LIST}" | cut -d' ' -f1)
    log "Number of spam handled: ${nspam}"

    # We only learn ham if any spam has been processed.
    if [ "${nspam}" -le 0 ]; then
        log "The number of spam is 0, doing nothing"
        return
    fi

    # Collecting the ham.
    log "Collecting the ham"
    find "${MAILDIR}/cur" -type f -printf "%T@ %p\n" \
        | sort -n \
        | cut -d ' ' -f2 \
        | tail -n"${nspam}" \
        | while read -r msg; do
        log "Found ham ${msg}, adding it to the ham list (${HAM_LIST})"
        printf "%s\n" "${msg}" >> "${HAM_LIST}"
    done

    # Invoking sa-learn.
    log "Invoking sa-learn"
    /usr/bin/sa-learn -f "${HAM_LIST}"

    # Saving the ham.
    log "Saving the ham into a different location"
    while read -r msg; do
        log "Copying ${msg} to ${SAVE_HAM_DIR}"
        cp "${msg}" "${SAVE_HAM_DIR}"
    done < "${HAM_LIST}"
}

while [ -n "${1}" ]; do
    case "${1}" in
        -f|--force)
            FORCE="true"
            ;;
    esac
    shift
done

learn_spam
learn_ham