#!/usr/bin/python

# CLI for maintaining the duplicate database
#
# Copyright (c) 2007 - 2012 Canonical Ltd.
# Author: Martin Pitt <martin.pitt@ubuntu.com>
#
# 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.  See http://www.gnu.org/copyleft/gpl.html for
# the full text of the license.

import optparse, sys, os.path

import apport.crashdb_impl.memory
import apport


def command_dump(crashdb, opts, args):
    '''Print out all entries.'''

    for (sig, (id, version, lastchange)) in crashdb._duplicate_db_dump(True).items():
        sys.stdout.write('%7i: %s ' % (id, sig))
        if version == '':
            sys.stdout.write('[fixed] ')
        elif version:
            sys.stdout.write('[fixed in: %s] ' % version)
        else:
            sys.stdout.write('[open] ')
        print('last change: %s' % str(lastchange))


def command_changeid(crashdb, opts, args):
    '''Change the master ID of a crash.'''

    if len(args) != 2:
        apport.fatal('changeid needs exactly two arguments (use --help for a short help)')
    (oldid, newid) = args

    crashdb.duplicate_db_change_master_id(oldid, newid)


def command_removeid(crashdb, opts, args):
    '''Remove a crash.'''

    if len(args) != 1:
        apport.fatal('removeid needs exactly one argument (use --help for a short help)')
    crashdb.duplicate_db_remove(args[0])


def command_publish(crashdb, opts, args):
    '''Publish crash database to a directory.'''

    if len(args) != 1:
        apport.fatal('publish needs exactly one argument (use --help for a short help)')
    crashdb.duplicate_db_publish(args[0])

#
# main
#

# parse command line options
optparser = optparse.OptionParser('''%prog [options] dump
%prog [options] changeid <old ID> <new ID>
%prog [options] removeid <ID>
%prog [options] publish <path>''')

optparser.add_option('-f', '--database-file', dest='db_file', metavar='PATH',
                     default='apport_duplicates.db',
                     help='Location of the database file')
options, args = optparser.parse_args()

if len(args) == 0:
    optparser.error('No command specified')

if not os.path.exists(options.db_file):
    apport.fatal('file does not exist: %s', options.db_file)

# pure DB operations don't need a real backend, and thus no crashdb.conf
crashdb = apport.crashdb_impl.memory.CrashDatabase(None, {})

# if args[0] in ():
#     # these commands require a real DB
#     crashdb = get_crashdb(None, None, {})
# else:

crashdb.init_duplicate_db(options.db_file)

try:
    command = globals()['command_' + args.pop(0)]
except KeyError:
    apport.fatal('unknown command (use --help for a short help)')

command(crashdb, options, args)
