#!/usr/bin/python3

import sys
import os
import re
import subprocess

from wordcloud import WordCloud
from collections import Counter

CAPTURE = [
    '.cpp',
    '.c',
    '.m',
    '.mm',
]

all_symbols_counter = Counter()
symbols_rexp = re.compile(r'[a-zA-Z][a-zA-Z_0-9]*')

def scan_source(filepath):
    print('scanning', filepath)
    source_text = open(filepath).read()

    # with open(filepath) as f:
        #text = f.read()
    pre_command = subprocess.Popen(
            ['gcc', '-E', '-P', '-fpreprocessed', filepath],
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT
        )
    stdout, stderr = pre_command.communicate()

    #print(stdout.decode('utf-8'))
    local_symbols = re.findall(symbols_rexp, stdout.decode('utf-8'))
    all_symbols_counter.update(local_symbols)

def inspect(path):
    if not os.path.isdir(path):
        print('{} is not an existing directory'.format(path))
        return

    to_scan = []
    unhandled = set()

    for dirpath, dirnames, filenames in os.walk(path):
        for afile in filenames:
            name, ext = os.path.splitext(afile)
            if ext in CAPTURE:
                #print(f"{dirpath}/{afile}")
                to_scan.append(f"{dirpath}/{afile}")
            else:
                unhandled.add( ext )

    print( 'found {} source files.'.format(len(to_scan)))
    print( 'unhandled file extensions', unhandled )

    for filepath in to_scan:
        scan_source(filepath)

    reduced = {k:all_symbols_counter[k] for k in all_symbols_counter if len(k)>3}
    print(f'Building wordcloud out of {len(reduced)} symbols')
    wc = WordCloud(width=1920, height=1080)\
            .generate_from_frequencies(reduced)
    wc.to_file('all_in_one.png')

def main(argv):
    if argv:
        for folder in argv:
            inspect(folder)
    else:
        print('Please specify source folder.')

if __name__ == '__main__':
    main(sys.argv[1:])
