A Lisp implemented in AWK
# SPDX-License-Identifier: BSD-2-Clause

BEGIN {
    if(!BUILD_AWK || !TARGET_AWK_F) {
        print "tmpl.awk: you must set BUILD_AWK and TARGET_AWK_F variables" >"/dev/stderr"
        exit 1
    }
    BAWK = BUILD_AWK " -v BUILD_AWK=\"" BUILD_AWK "\" -v TARGET_AWK_F=\"" TARGET_AWK_F "\" "
}

/^@target/ {
    next
}

/^@shebang/ {
    print "#!" TARGET_AWK_F
    next
}

/^@include-commented/ {
    fn = $2
    # setting a variable to the command whose output is to be read,
    # then using the variable, sets mawk at ease; not doing so seems
    # to put it in an infinite loop. i have not diagnosed this.
    command = BAWK " -f tmpl.awk " fn
    while((command | getline) > 0)
        print "# " $0
    next
}
    
/^@include/ {
    fn = $2
    print "# ---     " fn
    command = BAWK " -f tmpl.awk " fn
    while((command | getline) > 0)
        print
    print "# ---     " fn " ends"
    print ""
    next
}



{ print }