#!/usr/bin/env python

import os
from os import path
import sys
import commands

if len(sys.argv) < 2:
    print 'Usage: %s GENERIC_LIBRARY_PATH [SOURCE_DIR_OR_FILE ...]' % sys.argv[0]
    quit()

bin_dir = os.path.dirname( sys.argv[0] )
gen_lib_path = sys.argv[1]
gen_lib_basename = os.path.basename( gen_lib_path )
if len(sys.argv) == 2:
    source_paths = ['.'];
else:
    source_paths = sys.argv[2:]

def get_sub_components( c ):
    return commands.getoutput( path.join( bin_dir, 'find-subset' ) + ' ' + gen_lib_path + ' ' + path.join( gen_lib_path, 'src', c ) ).split()

def get_subset_recursive( components, result_set ):
    for c in components:
        if c not in result_set:
            result_set.add( c )
            get_subset_recursive( get_sub_components( c ), result_set )
    return result_set

toplevel_components = commands.getoutput( path.join( bin_dir, 'find-subset' ) + ' ' + gen_lib_path + ' ' + ' '.join( source_paths ) ).split( ' ' )

print ' '.join( sorted( get_subset_recursive( toplevel_components, set() ) ) )
