# Additiones to Jambase by Igor Boukanov, Igor.Boukanov@fi.uib.no
# Modified by Ilya Mezhirov (added SubDirHdrs with variable backup)

# new rules
# ImportDir TOP d1 d2 ... ;             include a subdirectory Jamfile
#                                        if not already included
# ImportFile TOP d1 d2 ... file ;       include the given file
#                                        if not already included
# IncludeFile TOP d1 ... dn file ;      include the given file
#
# new utilities
# addFileName var : d1 d2 ... file ;    $(var) += path from root to file
# makeFileName var : d1 d2 ... file ;   $(var) = path from root to file

rule ImportCheckArgs {
    if ! $(<[1])
    {
        EXIT "ImportFile syntax error: TOP should be given" ;
    }
    if ! $($(<[1]))
    {
        EXIT "ImportFile syntax error: TOP should be already set" ;
    }
    if ! $(<[2])
    {
        EXIT "ImportFile syntax error: should have at least 2 arguments" ;
    }
}

rule ImportFile
{
    ImportCheckArgs $(1) : $(2) ;
    
    local ImportFile__marker, ImportFile__i ;
    ImportFile__marker = "imported__" ;
    for ImportFile__i in $(<[2-])
    {
        ImportFile__marker = $(ImportFile__marker)__$(ImportFile__i) ;
    }
    if $($(ImportFile__marker)) = CIRCULAR_DEPENDENCY_ALERT {
        local x ;
        makeFileName x : $(<[2-]) ;
        Echo Jamimport: circular dependency between Jamfiles through $(x) ;
        Exit ;
    }
    if ! $($(ImportFile__marker))
    {
        local variables_to_back_up = TOP SUBDIR SUBDIR_TOKENS SEARCH_SOURCE 
                                     LOCATE_SOURCE LOCATE_TARGET SOURCE_GRIST
                                     SUBDIRASFLAGS SUBDIRHDRS SUBDIRDCFLAGS
                                     SUBDIRCCFLAGS SUBDIRC++FLAGS 
                                     ImportFile__marker ;
        local BACKUP_$(variables_to_back_up) ;
        local i ;
        for i in $(variables_to_back_up) { BACKUP_$(i) = $($(i)) ; }
        
        $(ImportFile__marker) = CIRCULAR_DEPENDENCY_ALERT ;
        local x ;
        makeFileName x : $(<[2-]) ;
        local ImportFile__path ;
        makeFileName ImportFile__path : $($(<[1])) $(<[2-]) ;
        include $(ImportFile__path) ;
        local x ;
        makeFileName x : $(<[2-]) ;
        
        for i in $(variables_to_back_up) { $(i) = $(BACKUP_$(i)) ; }
        $(ImportFile__marker) = SUCCESSFULLY_IMPORTED ;
    }
}

rule ImportDir
{
    ImportCheckArgs $(1) : $(2) ;
    SubDirHdrs $($(<[1])) $(<[2-]) ;
    ImportFile $(<) $(JAMFILE) ;
}

rule IncludeFile
{
    if ! $(<[1])
    {
        EXIT "IncludeFile syntax error: TOP should be given" ;
    }
    if ! $(<[2])
    {
        EXIT "IncludeFile syntax error: should have at least 2 arguments" ;
    }
    local IncludeFile__path ;
    makeFileName IncludeFile__path : $($(<[1])) $(<[2-]) ;
    include $(IncludeFile__path) ;
}

rule addFileName
{
    if ! $(>) {
        EXIT "Second argument in addFileName should have at least 1 component to form a file name" ;
    }
    if ! $(>[2]) {
        $(<) += $(>) ;
    }
    else {
        # In Jam I can not get $(>[all except last]) directly
        local addFileName__list, addFileName__base, addFileName__i ;
        addFileName__list = $(>[1]) ;
        addFileName__base = $(>[2]) ;
        for addFileName__i in $(>[3-]) {
            addFileName__list += $(addFileName__base) ;
            addFileName__base = $(addFileName__i) ;
        }
        local addFileName__dir_path ;
        makeDirName addFileName__dir_path : $(addFileName__list) ;
        $(<) += $(addFileName__base:D=$(addFileName__dir_path)) ;
    }
}

rule makeFileName
{
    $(<) = ; addFileName $(<) : $(>) ;
}
