I think it's a good idea to use awk to do this. But I don't like it that you are _allways_ generating code, even if there is nothing to do. We should try really hard to keep the generated code dumb. We should not push to much logic into the generated code. You have all you need to know in the gencode-remote script to decide what should be done. Personally I also don't like 'large' here docs - difficult to read.

As you use the environment to pass variables could you not create 2 small awk scripts in say $__type/files/, one for adding, one for removing, which you then cat to stdout after exporting the env vars?
Could you maybe y