#!/usr/bin/python
# vim: set fileencoding=utf-8 : sts=4 : et : sw=4
#    This is a component of mailpie, a full-text search for email
#
#    Copyright © 2008 Jeff Epler <jepler@unpythonic.net>
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import email.Message
import email.Parser
import getopt
import mailpie.swishfilter
import mailpie.parsedate
import mailpie.log
import os
import rfc822
import string
import subprocess
import sys
import tempfile
import time

def usage(result=0):
    print """mailpie-index: Manually index messages
Usage: %s [-B dir] [-s secs|-i|-f|-m]
    -B dir, --base=dir      Choose the base location of the mailpie storage
                            Default: $HOME/.mailpie

    -s DATE, --since=DATE   Do not index messages added before this time
                            Date formats are like "date -d=DATE"
    -i, --incremental       Index only messages added since last run
    -f, --full              Index all messages (default)

    -m, --merge             Merge index of recent messages with long-term index
                            (typically used with --incremental)
""" % os.path.basename(sys.argv[0])
    raise SystemExit, result

def main(args):
    try:
        opts, args = getopt.getopt(args, "B:s:ifmh?",
            ["help", "since=", "incremental", "full", "merge"])
    except getopt.GetoptError, detail:
        usage(detail)

    base = os.path.expanduser("~/.mailpie")
    since = 0
    merge = False

    for k, v in opts:
        if k in ("-?", "-h", "--help"): usage()
        if k in ("-B", "--base"): base = v
        if k in ("-s", "--since"): since = mailpie.parsedate.parsedate(v)
        if k in ("-i", "--incremental"): since = mailpie.swishfilter.Incremental
        if k in ("-f", "--full"): since=0
        if k in ("-m", "--merge"): merge = True

    swish = mailpie.swishfilter.Swish(base, since, time.time())

    os.chdir(base)
    dirs = ['%02x' % i for i in range(255)]
    for dir in dirs:
        mailpie.log.progress("%5.1f%% complete", int(dir, 16) * 100. / 256)
        if not os.path.exists(dir): continue
        os.chdir(dir)
        for file in os.listdir(os.path.curdir):
            swish.do_one(file, dir+file)
        os.chdir(os.path.pardir)
    swish.close()

    if merge:
        swish.merge()

if __name__ == '__main__':
    main(sys.argv[1:])
