#!/usr/bin/python
#
# Copyright (c) 2002 Gustavo Niemeyer <niemeyer@conectiva.com>
#
# This file is part of patcher.
# 
# pybot 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.
# 
# pybot 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 pybot; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
from patcher import Error
from patcher.command import *
import sys

AUTHOR  = "Gustavo Niemeyer <niemeyer@conectiva.com>"
VERSION = "0.6"

HELP = """\
Usage: ptr COMMAND [COMMAND ARGUMENTS]

Available commands:
    start     Create patcher structures
    stop      Remove patcher structures
    diff      Run diff between working copies and pristine versions
    status    Check entries status
    revert    Revert working copy to pristine version
    ignore    Ignore given entries when doing certain operations
    unignore  Revert effect of ignore command
    add       Add given entries to patcher control
    remove    Remove given entries from patcher control

Run "ptr COMMAND --help" for more information.

Written by Gustavo Niemeyer <niemeyer@conectiva.com>.
"""

def parse_options():
    parser = OptionParser(help=HELP, version="patcher "+VERSION)
    parser.disable_interspersed_args()
    parser.add_option("--debug", action="store_true")
    opts, args = parser.parse_args()
    opts.args = args
    if len(args) < 1:
        sys.stderr.write(HELP)
        sys.exit(1)
    opts.command = args[0]
    opts.args = args
    return opts

def dispatch_command(command, args, debug=0):
    sys.argv = args
    try:
        patcher_module = __import__("patcher.commands."+command)
        commands_module = getattr(patcher_module, "commands")
        command_module = getattr(commands_module, command)
    except (ImportError, AttributeError):
        if debug:
            import traceback
            traceback.print_exc()
            sys.exit(1)
        raise Error, "invalid command '%s'" % command
    command_module.main()
    
if __name__ == "__main__":
    do_command(parse_options, dispatch_command)

# vim:et:ts=4:sw=4
