#
#   Derived from compmenu.awk, this script forms a new install template
#   using CompSelect (from parse.awk)
#

#   File order to this script:
#
#   1. title table
#   2. current install template
#   3. CompSelect file


BEGIN {
     cnum = 0
     menustate = 0
}

/^linktext / {
     linktext = $2
}

/^titles/,/^endtitles/ {
     if ($2 != "") {
        titletab[$1] = substr($0,length($1)+1)
        cnum++
        clist[cnum] = $1
        next
     } else {
        next
     }
}

# install template lines. store the link text

/^install / {
     if (menustate == 0) {
        reqtab[$2] = "C"
        next
     }
}

/^link / {
     if (menustate == 0) {
        reqtab[$2] = "L"
        lttab[$2] = $3
        next
     }
}

# CompSelect lines

/^CompSelectStart/,/^CompSelectEnd/ {
     if (clist[$0] != "") {
        comp = clist[$0]
        if (reqtab[comp] == "") {
           if (linktext == "") {
              reqtab[comp] = "C"
           } else {
              reqtab[comp] = "L"
              lttab[comp] = linktext
           }
        }

        if (reqtab[comp] == "C") {
           print "install " comp
           reqtab[comp] = "N"
        }

        if (reqtab[comp] == "L") {
           print "link " comp " " lttab[comp]           
           reqtab[comp] = "N"
        }
     }
}
