#!/home/dirk/bin/venv/bin/python
# -*- coding: UTF-8 -*-

import optparse
import os

repolist = []


def findrepos(p, **kwargs):
    depth = kwargs.get('depth', 0)
    max_depth = kwargs.get('max_depth')
    ld = os.listdir(p)
    if '.git' in ld:
        if os.path.isdir(os.path.join(p, '.git')):
            repolist.append(p)
            return
    else:
        for entry in ld:
            np = os.path.join(p, entry)
            if os.path.isdir(np):
                findrepos(np, depth=depth+1, max_depth=max_depth)


if __name__ == "__main__":
    parser = optparse.OptionParser("usage: %%prog target_path [opions]")
    parser.add_option("-d", "--depth", dest="depth", default=None, type="int", help="Folder depth to search for a repo")
    parser.add_option("-p", "--human-readable", dest="human_readable", action="store_true", default=False, help="Print a human readable list")
    (options, args) = parser.parse_args()

    if len(args) != 1:
        parser.print_help()
    else:
        findrepos(args[0])
        repolist.sort()
        if options.human_readable:
            print('- ' + '\n- '.join(repolist), '\n')
        else:
            print(' '.join(repolist))