#!/usr/bin/env python
from __future__ import print_function

import os
import sys


def get_directories(args):
    search_dirs = set()
    extensions = {'.h', '.c'
                  '.hpp', '.hh', '.cpp', '.cc', '.cxx'}
    excluded_dirs = {
        # cmake
        'build', 'cmake-build-debug',
        # repo
        '.git',
        # IDE
        '.vscode', '.idea',
        'tests', 'doc', 'cmake', 'config',
    }
    for arg_ in args:
        if arg_.startswith('-'):
            excluded_dirs.add(arg_)
        else:
            search_dirs.add(arg_)
    dirs = set()
    for search_dir in search_dirs:
        for cur_dir, sub_dirs, file_names  in os.walk(search_dir):
            sub_dirs[:] = [d for d in sub_dirs if d not in excluded_dirs]
            for file_name in file_names:
                if os.path.splitext(file_name)[-1] not in extensions:
                    continue
                dirs.add(cur_dir)
    return dirs


def pprint(dirs, flavor='gdb'):
    if flavor == 'gdb':
        print(' '.join(['-d {}'.format(d) for d in dirs]))


if __name__ == '__main__':
    dirs = get_directories(sys.argv[1:])
    if dirs:
        pprint(dirs)
