#! @PYTHON@
# 
# Copyright (C) 2002 by the Free Software Foundation, Inc.
#
# 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.
#
"""Blow away the per list htdig files.

This script is for when you:
    a. decide to stop using Mailman-htdig integration
    b. move from local to remote htdig or vice-versa
    c. are updgrading to a version of htdig which has an incompatible
       index/db file format
    d. want to force the creation of new per-list htdig conf files
       and the list indexes.

You really want to stop Mailman operating while you are running this. For
instance, shutdown the MTA delivering mail to Mailman and remove Mailman's
crontab.

Usage: %(program)s [-v] [-h] [i] [listnames]

Where:
    --verbose / -v
        print each list as htdig is run for it

    --help / -h
        print this message and exit

    --config/ -c
        delete htdig search indices for the lists
        and recreate the htdig conf file for the lists

    --indices / -i
        only delete htdig search indices for the lists
        leave the htdig conf file in place. This is overridden by 
        the -c option

    listnames
        Optionally, only runs htdig for the named lists.  Without 
        this, all archivable lists are processed.

"""

# this code was derived from the nightly_gzip cron script

import sys
import os
from stat import *
import time
from stat import *
import getopt
import paths
from Mailman import MailList
from Mailman import Utils
from Mailman import mm_cfg
from Mailman.Archiver import HyperArch
from Mailman.i18n import _

program = sys.argv[0]

def usage(code, msg=''):
    print >> sys.stderr, _( __doc__)
    if msg:
        print msg
    sys.exit(code)

def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'vhic', 
                                   ['verbose', 'help', 'indices', 'config'])
    except getopt.error, msg:
        usage(1, msg)

    # defaults
    verbose = 0
    indices_only = 0
    redo_configs = 0
    for opt, arg in opts:
        if opt in ('-h', '--help'):
            usage(0)
        elif opt in ('-v', '--verbose'):
            verbose = 1
        elif opt in ('-c', '--config'):
            redo_configs = 1
            indices_only = 0
        elif opt in ('-i', '--indices') and not redo_configs:
            indices_only = 1
	
    # limit to the specified lists?
    if args:
        listnames = args
    else:
        listnames = Utils.list_names()

    # make sure htdig use is off for the moment in this process
    mm_cfg.USE_HTDIG = 0

    # process all the specified lists
    for name in listnames:
        mlist = MailList.MailList(name, lock=0)
        if not mlist.archive:
            continue
        archive = HyperArch.HyperArchive(mlist)
        if verbose:
            if redo_configs:
                print _('Blowing away all htdig indices of list %(name)s')
                print _('Rebuilding htdig config of list %(name)s')
            elif indices_only:
                print _('Blowing away all htdig indices of list %(name)s')
            else:
                print _('Blowing away all htdig stuff of list %(name)s')
        archive.remove_htdig(indices_only)
        if redo_configs:
            archive.setup_htdig()
        archive.write_TOC()

if __name__ == '__main__' and \
   mm_cfg.USE_HTDIG and \
   mm_cfg.ARCHIVE_TO_MBOX in (0, 2):
    # we're only going to run this if messages are archived to the internal 
    # archiver and we are using htdig to provide archive search
    omask = os.umask(002)
    try:
        main()
    finally:
        os.umask(omask)
