#!/bin/bash

set -e

makemaildir() {
    dir="$1"

    umask 077
    mkdir -p "${dir}/cur" "${dir}/new" "${dir}/tmp"
    chmod 0700 "${dir}/cur" "${dir}/new" "${dir}/tmp"
}

refile() {
    src="$1"
    dst="$2"

    md="${HOME}/Maildir/${dst}"
    if [ ! -d "${md}" ]; then
	makemaildir "${md}"
    fi
    mv "${src}" "${md}/new"
}

echo "tag:refile:"

# Get a list of messages with the "refile" tag.
notmuch search --output=messages tag:refile | while read id
do
    # Find the full set of tags for the message.
    tags=$(notmuch search --output=tags "${id}")

    # Which of these specifies a destination folder?
    dst=""
    for tag in $tags; do
	if [[ "${tag}" == folder:* ]]; then
	    ftag="${tag}"
	    dst="${tag##folder:}"
	fi
    done

    if [ "${dst}" == "" ]; then
	echo "No destination folder for refile: ${id}."
	continue
    fi

    # Find the filename for the message.
    notmuch search --output=files "${id}" | while read file
    do
	# If a refile previously happened but the notmuch database didn't
	# notice it yet, the filename may be incorrect.
	if [ ! -f "${file}" ]; then
	    echo "Source file not found: ${id}."
	    continue
	fi

	# Refile it.
	echo "${file} -> ${dst}"
	refile "${file}" "${dst}"
    done

    # Remove the refile, folder and inbox tags.
    notmuch tag -inbox -refile "-${ftag}" "${id}"
done

autofile() {
    comment="$1"
    search="$2"
    dst="$3"

    echo "${comment}:"
    notmuch search --output=files \
	"${search} AND NOT tag:inbox AND NOT tag:deleted AND NOT tag:refile AND NOT tag:flagged AND NOT date:7d..now" | while read file
    do
	echo "$file -> $dst"
	refile "${file}" "${dst}"
    done
}

# Various bits of automated filing.
autofile "Amazon" "(folder:.d.INBOX and (from:@amazon.com or from:@amazon.co.uk))" ".d.companies.amazon"
autofile "AMEX" "(folder:.d.INBOX and from:aexp.com and subject:statement)" ".d.companies.americanexpress"
autofile "Apple" "(folder:.d.INBOX and from:apple.com and subject:invoice)" ".d.companies.apple"
autofile "Dreamhost" "(folder:.d.INBOX and from:@dreamhost.com)" ".d.companies.dreamhost"
autofile "Ebay (uk)" "(folder:.d.INBOX and from:@ebay.co.uk)" ".d.companies.ebay"
autofile "Ebay (us)" "(folder:.d.INBOX and from:@ebay.com)" ".d.companies.ebay"
autofile "Ebay (au)" "(folder:.d.INBOX and from:@ebay.com.au)" ".d.companies.ebay"
autofile "Futurelearn" "(folder:.d.INBOX and from:@futurelearn.com)" ".d.companies.futurelearn"
autofile "Hitachi Capital (1)" "(folder:.d.INBOX and from:hccf@hitachicapital.co.uk)" ".d.companies.hitachicapital"
autofile "Hitachi Capital (2)" "(folder:.d.INBOX and from:customer_service@hitachipersonalfinance.co.uk)" ".d.companies.hitachicapital"
autofile "Parmiters" "(folder:.d.INBOX and from:@capita-intouch.co.uk and parmiter)" ".d.family.louis"
autofile "Paypal" "(folder:.d.INBOX and from:@paypal.co.uk))" ".d.companies.paypal"
autofile "Wiggle" "(folder:.d.INBOX and from:@wiggle.co.uk))" ".d.companies.wiggle"
autofile "Wiggle (2)" "(folder:.d.INBOX and from:orders@wiggle.com))" ".d.companies.wiggle"
autofile "Wisepay" "(folder:.d.INBOX and from:@wisepay.co.uk)" ".d.companies.wisepay"
autofile "giffgaff" "(folder:.d.INBOX and from:@giffgaff.com)" ".d.companies.giffgaff"
autofile "planetx" "(folder:.d.INBOX and from:noreply@planetx.co.uk)" ".d.companies.planetx"
autofile "Waitrose" "(folder:.d.INBOX and from:customersupport@waitrose.co.uk)" ".d.companies.waitrose"
autofile "Waitrose" "(folder:.d.INBOX and from:customersupport@waitrosepet.com)" ".d.companies.waitrose"

autofile "TEMA" "(folder:.o.INBOX and subject:\"TEMA Alerts\")" ".o.stash.phone"
autofile "Expense Images" "(folder:.o.INBOX and from:Oracle-Workflow-GSI_WW and subject:\"Image File\")" ".o.stash.expenses"

# Update the database.
notmuch-new
