from ..generator import Generator

import os
import sys


class JamfileGenerator(Generator):
    @staticmethod
    def identifier():
        return 'jamfile'

    def generate(self, needy):
        path = os.path.join(needy.needs_directory(), 'Jamfile')
        contents = """import feature ;
import toolset ;

path-constant NEEDY : %s ;
path-constant BASE_DIR : %s ;
path-constant NEEDS_FILE : %s ;

feature.feature needyargs : : free ;
toolset.flags $(__name__).satisfy-lib NEEDYARGS <needyargs> ;

rule needlib ( name : extra-sources * : requirements * : default-build * : usage-requirements * )
{
    local target = $(name) ;
    
    if <target-os>iphone in $(requirements) {
        target = "$(name) -u iphone" ;
    } else if <target-os>android in $(requirements) {
        target = "$(name) -t android:armv7" ;
    } else if <target-os>appletv in $(requirements) {
        target = "$(name) -t appletv:arm64" ;
    }

    local args = $(target) ;
    
    if <target-os>android in $(requirements) {
        args += "--android-toolchain=$(ANDROID_TOOLCHAIN)" ;
    }

    local builddir = [ SHELL "cd $(BASE_DIR) && $(NEEDY) builddir $(target)" ] ;
    local includedir = "$(builddir)/include" ;
    
    make lib$(name).touch : $(NEEDS_FILE) : @satisfy-lib : $(requirements) <needyargs>$(args) ;
    actions satisfy-lib
    {
        cd $(BASE_DIR) && $(NEEDY) satisfy $(NEEDYARGS) && cd - && touch $(<)
    }

    alias $(name)
        : $(extra-sources) 
        : $(requirements) 
        : $(default-build) 
        : <dependency>lib$(name).touch
          <include>$(includedir)
          <linkflags>-L$(builddir)/lib
          $(usage-requirements)
    ;
}
""" % (os.path.abspath(sys.argv[0]), os.path.dirname(needy.path()), needy.path())

        for library in needy.libraries_to_build():
            contents += """
needlib {0} ;
needlib {0} : : <target-os>iphone ;
needlib {0} : : <target-os>android ;
needlib {0} : : <target-os>appletv ;
""".format(library[0])

        with open(path, 'w') as jamfile:
            jamfile.write(contents)

@@ -686,38 +686,165 @@
     
-target = %22$(name) -u iphone%22 ;
+if %3Carchitecture%3Earm in $(requirements) %7B%0A            target = %22$(name) -u iphone%22 ;%0A        %7D else %7B%0A            target = %22$(name) -u iphonesim%22 ;%0A        %7D
 %0A   
@@ -1006,45 +1006,173 @@
     
-target = %22$(name) -t appletv:arm64%22 ;
+if %3Carchitecture%3Earm in $(requirements) %7B%0A            target = %22$(name) -t appletv:arm64%22 ;%0A        %7D else %7B%0A            target = %22$(name) -t appletvsim%22 ;%0A        %7D
 %0A   
@@ -2130,16 +2130,88 @@
 %3Eiphone 
+%3Carchitecture%3Earm ;%0Aneedlib %7B0%7D : : %3Ctarget-os%3Eiphone %3Carchitecture%3Ex86 
 ;%0Aneedli
@@ -2276,16 +2276,89 @@
 appletv 
+%3Carchitecture%3Earm ;%0Aneedlib %7B0%7D : : %3Ctarget-os%3Eappletv %3Carchitecture%3Ex86 
 ;%0A%22%22%22.fo

add simulators to jamfile generator