#!/usr/bin/python
"""
creates gitology-repo in the current directory.
"""

import sys, gitology, textwrap, os, path
from optparse import OptionParser
from gitology import utils

# build_parser # {{{
def build_parser():
    parser = OptionParser(
        version = "%prog " + gitology.VERSION,
        usage = "Usage: %prog [options] [repository_folder_location]",
    )
    parser.add_option(
        "--use-md5", action="store_true", dest="use_md5",
        default=False, help="use MD5 for creating document folders."
    )
    parser.add_option(
        "--rcs", metavar="RCS", default="git", dest="rcs",
        help="revision control system to be used. none, git, svn, bzr and hg are supported, default is git." 
    )
    parser.add_option(
        "-m", "--message", default="Initical checkin of gitology", dest="message", 
        help="the commit message to be used for initial commit, can be left empty to force no commits by gitology."
    )
    parser.add_option(
        "--about", action="callback", callback=print_about,
        help="find out more about this script"
    )
    return parser
# }}}


# print_about # {{{
def print_about(*args, **kw):
    print utils.smart_wrap(
        """Create a blank directory structure that can be used with gitology. 

If the "none" RCS is selected, gitology-init will not use any version control system. For other RCS, it will create a new repo, and initialize and add the files in it.""" 
    )
    raise SystemExit
# }}}

def main():
    # get arguments # {{{
    parser = build_parser()
    (options, args) = parser.parse_args()
    if len(args) > 1:
        parser.error("Please specify only one repository folder location")
    if len(args) == 0:
        try:
            repo_name = raw_input("Please enter a repository folder location: ") 
        except KeyboardInterrupt: 
            print
            return 1
    else: 
        repo_name = args[0]
    repo_path = path.path(".").joinpath(repo_name)
    if repo_path.exists():
        print "%s already exists, can not overwrite content. Exiting." % repo_path.abspath()
        return 3
    # }}}
    try:
        repo_path.makedirs()
    except OSError, e:
        print "Can not create directories. %s. Exiting." % e
        return 4
    repo_path.joinpath("editors.txt").touch()
    repo_path.joinpath("blocked-authors.txt").touch()
    rc_file = path.path(os.environ["HOME"]).joinpath(".gitologyrc")
    rc_file_content = """\
[DEFAULTS]
AUTHOR: www.example.com
USE_MD5: %s

[REPO]
LOCAL: %s
REMOTE: 
AUTO_COMMIT: False
""" % ( options.use_md5, repo_path.abspath() )
    s_rc_file = repo_path.joinpath("sample.gitologyrc")
    s_rc_file.open("w").write(rc_file_content)
    if rc_file.exists():
        print "You already have .gitlogyrc file, sample rc file written to", 
        print s_rc_file.abspath()
        print "Copy it to ~/.gitologyrc"
    else: 
        rc_file.open("w").write(rc_file_content)
        print "Information about this repo written to %s" % rc_file.abspath()
    return 0

if __name__ == "__main__":
    sys.exit(main())
