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

import os
import re
import sys
import glob
from io import BytesIO
from fnmatch import fnmatch
from itertools import ifilter
from os.path import join as pathjoin, relpath, dirname, \
                    abspath, basename, splitext, sep as pathsep

import lxml.etree as ET


HERE = dirname(abspath(__file__))
AUTOGEN_WARNING = ('<!-- ******** AUTOGENERATED by {0} ' +
            ('*' * 20) + '-->').format(basename(sys.argv[0]))

ROOT = ''.join([_line.strip() for _line in """\
<project name="{PROJECT_NAME}" basedir="." default="build">
  <property environment="env" />
  <property name="FLEX_HOME" value="${{env.FLEX_HOME}}" />
  <property name="FLEXUNIT_HOME" value="${{env.FLEXUNIT_HOME}}" />
  <taskdef resource="flexTasks.tasks" """
      """classpath="${{env.FLEX_HOME}}/ant/lib/flexTasks.jar" />
  <target name="build" />
  <target name="clean">
    <delete>
      <fileset dir="${{basedir}}" casesensitive="yes">
        <patternset id="swf.files">
          <include name="**/*.swf" />
        </patternset>
        <patternset id="swf.files">
          <include name="**/*.swc" />
        </patternset>
      </fileset>
    </delete>
  </target>
</project>
""".splitlines() if _line.strip()])


def main(sysargs=sys.argv[:]):
    build_xml = pathjoin(HERE, 'build.xml')
    os.chmod(build_xml, 0600)
    buildxml_maker = \
        BuildXMLMaker(project_name=sysargs[1], outstream=open(build_xml, 'wb'))
    buildxml_maker.make()
    os.chmod(build_xml, 0444)
    return 0


class BuildXMLMaker(object):
    _to_glob = (
        pathjoin(HERE, '[0-9]*-*', '*.mxml'),
        pathjoin(HERE, 'custom-*', '*.mxml'),
    )
    _source_fileexts = ('.mxml', '.as', '.css')

    def __init__(self, project_name='%PROJECT%', outstream=sys.stdout):
        self.outstream = outstream
        self.targets = {'compc': {}, 'mxmlc': {}}
        self.tree = ET.parse(BytesIO(ROOT.format(PROJECT_NAME=project_name)))
        self.root = self.tree.getroot()
        self.build = self.tree.xpath('//target[@name="build"]')[0]

    def make(self):
        self._glob_for_top_level_mxml_targets()
        self._find_test_suite_targets()
        self._write_out_targets()

    def _glob_for_top_level_mxml_targets(self):
        for pattern in self._to_glob:
            for mxml in glob.glob(pattern):
                self._add_target_from_mxml(mxml)

    def _find_test_suite_targets(self):
        for dirpath, dirnames, filenames in os.walk(HERE):
            filter_generated_dirs(dirnames)
            for filename in filenames:
                if is_test_suite(filename):
                    test_suite = pathjoin(dirpath, filename)
                    self._add_target_from_test_suite(test_suite)

    def _add_target_from_test_suite(self, test_suite):
        rel_class = relpath(test_suite, HERE)
        self.targets['compc'][rel_class] = {}

    def _add_target_from_mxml(self, mxml):
        rel_mxml = relpath(mxml, HERE)
        self.targets['mxmlc'][rel_mxml] = {} #dict(sources='')
        self._add_deps_from_alt_source_file_extensions(rel_mxml)

    def _add_deps_from_alt_source_file_extensions(self, rel_mxml):
        basedir = dirname(rel_mxml)
        # sources = self.targets['mxmlc'][rel_mxml]['sources']

        for dirpath, dirnames, filenames in os.walk(basedir):
            filter_generated_dirs(dirnames)
            filter_test_suitees(filenames)
            for filename in filenames:
                if ext(filename) in self._source_fileexts:
                    rel_source = relpath(pathjoin(dirpath, filename), HERE)
                    # sources += (' ' + rel_source)

    def _write_out_targets(self):
        for mxml in sorted(self.targets['mxmlc'].iterkeys()):
            rel_target = './' + mxml.lstrip('./')
            attrs = dict(file=rel_target, as3='true')
            attrs.update(self.targets['mxmlc'][mxml])
            ET.SubElement(self.build, 'mxmlc', **attrs)

        for as3 in sorted(self.targets['compc'].iterkeys()):
            rel_target = './' + as3.lstrip('./')
            as_swf = re.sub('(.*).as$', '\\1.swf', rel_target)
            as_class = re.sub('(.*).as$', '\\1', rel_target).replace('/', '.')
            class_parts = as_class.strip('./').split('.')
            as_class = '.'.join(class_parts[1:])
            source_path_dir = './' + '/'.join(class_parts[:1])
            attrs = {'output': as_swf, 'include-classes': as_class}
            attrs.update(self.targets['compc'][as3])
            compc_element = ET.SubElement(self.build, 'compc', **attrs)
            ET.SubElement(compc_element, 'sp',
                **{'path-element': source_path_dir})
            library_path = ET.SubElement(compc_element, 'l',
                **{'dir': '${FLEXUNIT_HOME}', 'append': 'yes'})

        print(ET.tostring(self.root, pretty_print=True),
              file=self.outstream)

    def _get_deps_for_target(self, target):
        deps = []
        for dep in reversed(sorted(list(self.targets[target]), key=ext)):
            deps.append(dep)
        return deps

    def _get_primary_dep(self, target, deps):
        primary_dep = deps[0]

        for dependency in deps:
            if fnmatch(target, namebase(dependency) + '.*'):
                primary_dep = dependency

        return primary_dep

    def _get_pattern_rule_kwargs(self, primary_dep, target):
        ret = dict(
            primary_dep=primary_dep,
            parent_dir=dirname(primary_dep),
            package_base=primary_dep.strip('./').split(pathsep)[0],
            target=target,
            flexunit=FLEXUNIT
        )
        return ret


def ext(filename):
    return splitext(filename)[-1]


def namebase(filename):
    return splitext(filename)[0]


def filter_generated_dirs(dirnames):
    for i, directory in enumerate(dirnames[:]):
        if directory == 'generated':
            dirnames[i] = None
    dirnames[:] = list(ifilter(None, dirnames))


def filter_test_suitees(filenames):
    for i, filename in enumerate(filenames[:]):
        if is_test_suite(filename):
            filenames[i] = None
    filenames[:] = list(ifilter(None, filenames))


def is_test_suite(filename):
    base_filename = basename(filename)
    return base_filename.startswith('TestSuite') and \
        ext(filename) == '.as'


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

# vim:filetype=python
