#
#   awk merge sort
#
#   Feed this awk script:
#
#        0. Install template
#        1. installation manifest
#        2. media manifest
#        3. component manifests for each selected component
#
#   It outputs a script of things to do (what to install in what order).


BEGIN {
          print "#!/bin/sh"
          print "# install order and methods"
          print "# "
          print "# arguments: $1 install phase (pre/post/safe)"
          print "#            $2 verbose flag"
          print " "
          statelen = 0;
      }

# install template tags

/^install/ {
          reqinstalltable[$2] = 1 
        }

/^link/ {
          reqlinktable[$2] = $3
}

# component manifest tags below

/^name/ {
          inlen = 0;
          comp = $2;
        }

/^nolink/ {
         reqlinktable[comp] = ""
         linktable[comp] = ""
}

/^requires/ {
         inlen++;
         inarray[inlen] = $2;

         # cascade the link request

         if (reqlinktable[comp] != "") {
            reqlinktable[$2] = reqlinktable[comp]
         }

         if (reqinstalltable[comp] == 1) {
            reqinstalltable[$2] = 1
         }
        }

/^endrequires/ {
         inlen++;
         inarray[inlen] = comp;

         if ((reqinstalltable[comp] == "") && (reqlinktable[comp] == "")) {
            next;
         }

         # Suppose this is an install. Then none of the requires can be a link.
         # If they exist already as links, they have to be removed. How to code
         # this...hmm....

         holdlen = 0;
         if (statelen == 0) {
             for (i=1; i <= inlen; i++) {
                 statelen++
                 statearray[statelen] = inarray[i]
                 if (reqlinktable[inarray[i]] != "") {
                    linktable[statearray[statelen]] = reqlinktable[inarray[i]];
                 }
             }
         } else {
             smark = 1
             imark = 1
             for (p1 = 1; p1 <= statelen; p1++) {

                 for (p2 = 1; p2 <= inlen; p2++) {
                     if (statearray[p1] == inarray[p2]) {

                         if (reqlinktable[inarray[p2]] != "") {
                             if (linktable[inarray[p2]] != "") {
                                if (linktable[inarray[p2]] != reqlinktable[inarray[p2]]) {
                                   print "# link-text-warning " inarray[p2] " " linktable[inarray[p2]] " " reqlinktable[inarray[p2]]
                                }
                             } else {
                                for (x=1; x<=p2; x++) {
                                   reqlinktable[inarray[x]] = ""
                                   linktable[inarray[x]] = ""
                                }
                             }                                   
                         } else {
# this is a weird situation. suppose that we clear a link here, and something
# later needs to be a copy...the link method needs to take care of this.
                             if (linktable[inarray[p2]] != "") {
                                linktable[inarray[p2]] = ""
                             }
                         }
                         for (t = smark; t < p1; t++) {
                             holdlen++;
                             holdarray[holdlen] = statearray[t];
                         }
                         smark = p1 + 1
                         for (t = imark; t <= p2; t++) {
                             holdlen++;
                             holdarray[holdlen] = inarray[t];
                         }
                         imark = p2 + 1
                     } else {
                         linktable[inarray[p2]] = reqlinktable[inarray[p2]]
                     }
                 }
             }
             for (t = smark; t <= statelen; t++) {
                 holdlen++;
                 holdarray[holdlen] = statearray[t];
             }
             for (t = imark; t <= inlen; t++) {
                 holdlen++;
                 holdarray[holdlen] = inarray[t];
             }
             for (i = 1; i <= holdlen; i++) {
                 statearray[i] = holdarray[i];
             }
             statelen = holdlen;
         }
    }

# ----------------------------------------------
#  installation manifest tags go below
# ----------------------------------------------

/^from/ {
        next;
        }
  

/^installed / {
        vers[$2] = $3
        if (tlinktable[$2] != "") {
           print "# debug: replace link with copy warning"
        }
}

   # syntax for linked is:  linked <cname> <version> <layout>
   # do not set or clear version number here
   # tlinktable is used when calculating methods. 

/^linked / {
        linktable[$2] = $4
        tlinktable[$2] = $4
}

#--------------------------------
#   media manifest tags go below
#--------------------------------

/^media/ {
         next
}

/^productcode/ {
         next
}

/^component/ {
        if (vers[$2] <  $3) { method[$2] = "copy"; }
        if (vers[$2] >  $3) { method[$2] = "conflict"; }
        if (vers[$2] == $3) { method[$2] = "update"; }
        version[$2] = $3;
        }

/^set/ {
        next
}

# for each component, print installation method.

END {
        for (i = 1; i <= statelen; i++) {
            ccomp=statearray[i]
            if ( method[ccomp] == "" ) {
               method[ccomp] = "error"
            }

            if ( linktable[ccomp] != "" ) {
               method[ccomp] = "link"
            }
            print "instmethod $1 $2 " method[ccomp] " " ccomp " " version[ccomp] " " linktable[ccomp];
        }
}
