#!/usr/bin/python

# qct - Commit Tool
#
# Copyright 2006 Steve Borho
#
# This software may be used and distributed according to the terms
# of the GNU General Public License, incorporated herein by reference.

import sys, os
import getopt
from PyQt4 import QtGui
from qctlib.gui_logic import CommitTool

# Standalone command line application

if __name__ == "__main__":
    # parse command line options
    opts = []
    try:
        opts, args = getopt.getopt(sys.argv[1:], "s4bghcm",
                ["svn", "p4", "bzr", "hg", "git", "cvs", "mtn"])
    except getopt.error, msg:
        pass

    # default to auto-detect VCS back-end if no matches were found
    if len(opts) == 0:
        if os.path.exists('.hg/'):
            print "Auto-detected Mercurial repository"
            from qctlib.vcs.hg import qctVcsHg
            vcs = qctVcsHg()
        elif os.path.exists('.bzr/'):
            print "Auto-detected Bazaar repository"
            from qctlib.vcs.bzr import qctVcsBzr
            vcs = qctVcsBzr()
        elif os.path.exists('.svn/'):
            print "Auto-detected Subversion repository"
            from qctlib.vcs.svn import qctVcsSvn
            vcs = qctVcsSvn()
        elif os.path.exists('.git/'):
            print "Auto-detected Subversion repository"
            from qctlib.vcs.git import qctVcsGit
            vcs = qctVcsGit()
        elif os.path.exists('_MTN/'):
            print "Auto-detected Monotone repository"
            from qctlib.vcs.mtn import qctVcsMtn
            vcs = qctVcsMtn()
        elif os.path.exists('CVS/'):
            print "Auto-detected CVS repository"
            from qctlib.vcs.cvs import qctVcsCvs
            vcs = qctVcsCvs()
        else:
            from qctlib.vcs.hg import qctVcsHg
            vcs = qctVcsHg()

    # process options
    for opt, arg in opts:
        if opt in ("-4", "--p4"):
            from qctlib.vcs.p4 import qctVcsP4
            vcs = qctVcsP4()
        if opt in ("-h", "--hg"):
            from qctlib.vcs.hg import qctVcsHg
            vcs = qctVcsHg()
        elif opt in ("-g", "--git"):
            from qctlib.vcs.git import qctVcsGit
            vcs = qctVcsGit()
        elif opt in ("-b", "--bzr"):
            from qctlib.vcs.bzr import qctVcsBzr
            vcs = qctVcsBzr()
        elif opt in ("-s", "--svn"):
            from qctlib.vcs.svn import qctVcsSvn
            vcs = qctVcsSvn()
        elif opt in ("-c", "--cvs"):
            from qctlib.vcs.cvs import qctVcsCvs
            vcs = qctVcsCvs()
        elif opt in ("-m", "--mtn"):
            from qctlib.vcs.mtn import qctVcsMtn
            vcs = qctVcsMtn()

    if vcs.initRepo(sys.argv) != 0:
        sys.exit()

    # Now we know it's worth the trouble to open the GUI
    app = QtGui.QApplication(sys.argv)
    dialog = CommitTool(vcs)
    dialog.show()
    sys.exit(app.exec_())
