#!/usr/local/bin/python2.7
# -*- coding: iso-8859-1 -*-

# Copyright 2004-2005 Andr Malo or his licensors, as applicable.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
usage: svn-mailer <options>

options:
  --version             show program's version number and exit
  -h, --help            show this help message and exit

  COMMON PARAMETERS:
    --debug             Run in debug mode (means basically that all messages
                        are sent to STDOUT)
    -dREPOSITORY, --repository=REPOSITORY
                        The repository directory
    -fCONFIG, --config=CONFIG
                        The configuration file
    -ePATH_ENCODING, --path-encoding=PATH_ENCODING
                        Specifies the character encoding to be used for
                        filenames. By default the encoding is tried to be
                        determined automatically depending on the locale.
    -b, --background    Lets the mailer do its work in the background. That way
                        the hook script can exit faster.

  BEHAVIOUR OPTIONS:
    The behaviour options are mutually exclusive, i.e. the last one wins.
    -c, --commit        This is a regular commit of versioned data (post-commit
                        hook). This is default.
    -p, --propchange    This is a modification of unversioned properties (post-
                        revprop-change hook)
    -l, --lock          (svn 1.2 and later) This is a locking call (post-lock
                        hook)
    -u, --unlock        (svn 1.2 and later) This is a unlocking call (post-
                        unlock hook)

  SUPPLEMENTAL PARAMETERS:
    -rREVISION, --revision=REVISION
                        The modified/committed revision number
    -aAUTHOR, --author=AUTHOR
                        The author of the modification
    -nPROPNAME, --propname=PROPNAME
                        The name of the modified property
    -oACTION, --action=ACTION
                        (svn 1.2 and later) The property change action

Alternatively you can use the old style compatibility command lines (options
described above don't apply then):

svn-mailer commit <repos> <revision> [<config>]
svn-mailer propchange <repos> <revision> <author> <propname> [<config>]

svn 1.2 and later:
svn-mailer propchange2 <repos> <revision> <author> <propname> <action>
           [<config>]
svn-mailer lock <repos> <author> [<config>]
svn-mailer unlock <repos> <author> [<config>]
"""
import os, sys

try:
    import locale, traceback
    locale.setlocale(locale.LC_CTYPE, "") # needed for proper svn behaviour

    from svnmailer import main, subversion

    try:
        main.Main.fromCommandline().run()

    except main.CommandlineError, exc:
        print >> sys.stderr, str(exc)
        sys.exit(1)

    except main.ConfigError, exc:
        print >> sys.stderr, "Configuration Error: %s\n" % str(exc)
        print >> sys.stderr, '-' * 78
        traceback.print_exc(file = sys.stderr)
        sys.exit(1)

    except main.NotifierError, exc:
        print >> sys.stderr, "One or more notifiers crashed. You may want " \
            "to send the following traceback(s) to the author:\n"
        for backtrace in exc.args:
            print >> sys.stderr, '-' * 78
            print >> sys.stderr, backtrace
        sys.exit(1)

    except subversion.RepositoryError, exc:
        print >> sys.stderr, "Something bad happened while accessing the " \
            "repository:\n%s (%s)\n%s\n" % (
                exc.svn_err_name, exc.svn_err_code, exc.svn_err_str
            )
        print >> sys.stderr, '-' * 78
        traceback.print_exc(file = sys.stderr)

except (SystemExit, MemoryError):
    raise

except KeyboardInterrupt:
    print >> sys.stderr, "Interrupted by user request"
    sys.exit(0)

except:
    print >> sys.stderr, \
        "Oops, %s crashed. You may want to send the traceback " \
        "to the author:\n" % os.path.basename(sys.argv[0])
    raise
