#
# recursestatic_requires.awk
#
# input:
#   name0 name1 name2 component ...
#
# recursively traverses the component dependency files, putting out all
# the dependencies of the comp<n>
#
# intended for a fully static build
#
# if there're any repetitions, all but the last are suppressed
#
# the component dependency files are found at the concatenation of
#   name0
#   <component without diversity>
#   name1
#   <component without diversity>
#   name2

BEGIN {
    # phase
    #      0:  looking for name0
    #      1:  looking for name1
    #      2:  looking for name2
    #   else:  looking at component names
    phase = 0
}

function doWord(word, file, n, line, words, i)# local: file, n, line, words, i
{
    # wordLast[string word] holds the word sequence number of the last
    # occurence of the string word
    #
    # wordSeq[number] holds the word whose sequence number is number

    nWords++
    wordLast[word] = nWords
    wordSeq[nWords] = word

    # remove trailing diversity stuff then recurse into the file

    # sub(/PROVIDED_BY/, "", word)

    # fileSeen[string word] is 1 if we've already read this file
    #
    # if we've seen it, then:
    #   fileWords[word, 0] is the number of words from the file
    #   fileWords[word, 1]..file are the words from the file

    if (!fileSeen[word])
    {
        # we've never seen this file; load up fileWords

        fileSeen[word] = 1
        fileWords[word, 0] = 0
        res1 = 0

        file = name0 "comps/" word name1 word name2
        
        res = (getline line < file)
        if (res == -1){
        	res1 = -1
        }
        while (res > 0)
        {
            n = split(line, words)
            for (i = 1; i <= n; i++)
            {
                fileWords[word, ++fileWords[word, 0]] = words[i]
            }
            res = (getline line < file)
        }
        close(file)

        file = name0 "intfs/" word name1 word name2
        
        res = (getline line < file)
        if ((res == -1) && (res1 == -1)){
            printf("ERR_R_%s\n",word);
        }
        while (res > 0)
        {
            n = split(line, words)
            for (i = 1; i <= n; i++)
            {
                fileWords[word, ++fileWords[word, 0]] = words[i]
            }
            res = (getline line < file)
        }
        close(file)
        
    }

    # Recursively search for the libraries if there are no circular dependencies.
    

      for (i = 1; i <= fileWords[word, 0]; i++)
      {
       
	 if ((!fileSeen[fileWords[word, i]])) {
	    
	    if ( fileWords[word, i] != "PROVIDED_BY") {
		doWord(fileWords[word, i])
	    }
	 }
      }
    
}

{
    n = split($0, words)
    for (i = 1; i <= n; i++)
    {
        if (phase == 0)
        {
            name0 = words[i]
            phase = 1
        }
        else if (phase == 1)
        {
            name1 = words[i]
            phase = 2
        }
        else if (phase == 2)
        {
            name2 = words[i]
            phase = 3
        }
        else
        {
           if (words[i] != "PROVIDED_BY") {
	    doWord(words[i])
	   }
        }
    }
}

END {
    # walk through the whole sequence, dumping a word if it's the last
    # occurrenc of that word
    for (i = 1; i <= nWords; i++)
    {
        if (wordLast[wordSeq[i]] == i)
        {
            printf("%s\n", wordSeq[i])
        }
    }
}
