#!/usr/bin/python -u
# vim: set fileencoding=utf-8 :
#
# (C) 2010 Guido Guenther <agx@sigxcpu.org>
#    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
#
"""Add a patch from debian/patches and autocreate the commit message from the
patch info

running

    gbp-add-patch debian/patches/0010-bla-fasel

commits debian/patches/0010-bla-fasel with this changelog message:

    New patch 0010-bla-fasel

    <patch summary>
    Closes: <bugs>
    Thanks: <author>
"""

import re
import sys
import os, os.path
from gbp.command_wrappers import (Command)
from gbp.config import (GbpOptionParserDebian, GbpOptionGroup)
from gbp.errors import GbpError
from gbp.git import (GitRepositoryError, GitRepository)
from gbp.patch_series import Patch


def build_commit_msg(repo, patch, options):
    bug_r = r'(?:bug)?\#?\s?\d+'
    bts_closes = re.compile(r'(?P<bts>%s):\s+%s' % (options.meta_closes, bug_r), re.I)
    thanks = ''
    closes = ''

    author = repo.get_author_info()
    if author.name != patch.author:
        thanks = "Thanks: %s" % patch.author

    for line in patch.long_desc.split('\n'):
        if bts_closes.match(line):
            closes += line + '\n'

    patch_name = os.path.basename(patch.path)
    msg="""New patch %s

%s
%s
%s""" % (patch_name, patch.subject, thanks, closes)
    return msg


def main(argv):
    retval = 0

    parser = GbpOptionParserDebian(command=os.path.basename(argv[0]), prefix='',
                             usage='%prog [options] - add a new patch')
    parser.add_config_file_option(option_name="meta-closes", dest="meta_closes",
                      help="Meta tags for the bts close commands, default is '%(meta-closes)s'")
    parser.add_option("-e", "--edit", action="store_true", dest="edit", default=False,
                      help="edit commit message before committing")
    parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False,
                      help="verbose command execution")

    (options, args) = parser.parse_args(argv)

    if options.verbose:
        Command.verbose = True

    try:
        repo = GitRepository(os.path.curdir)
    except GitRepositoryError:
        print >>sys.stderr, "%s is not a git repository" % (os.path.abspath('.'))
        return 1

    try:
        if len(args) != 2:
            parser.print_help()
            raise GbpError
        else:
            patchfile = args[1]

        patch = Patch(patchfile)

        repo.add_files(patchfile)
        msg = build_commit_msg(repo, patch, options)
        repo.commit_staged(edit=options.edit, msg=msg)
        # FIXME: handle the series file

    except GitRepositoryError:
        retval = 1
    except GbpError as err:
        if len(err.__str__()):
            print >>sys.stderr, err
        retval = 1

    return retval

if __name__ == '__main__':
    sys.exit(main(sys.argv))

# vim:et:ts=4:sw=4:et:sts=4:ai:set list listchars=tab\:»·,trail\:·:
