With the recent more strict checking of shebang, it's more obvious that there are a number of ports where fixing shebangs at patch time is not sufficient. One example is print/tex-xetex which installs files directly from a distribution tarball during the 'stage' phase.
Attached is an update to shebangfix.mk that supports fixing shebangs during the 'stage' phase.
The gating knob to control it is to define SHEBANG_FILES_INSTALL (relative to ${STAGEDIR}${PREFIX} by default) instead of (or in addition to) SHEBANG_FILES.
It also now supports SHEBANG_FILES_PATCH to be more explicit. We could deprecate SHEBANG_FILES, but for now it can be a separate task to replace them.
I wasn't clear whether to name the new variables as *_INSTALL or *_STAGE. Feedback welcome. I opted for _INSTALL since it's generally recommended to use things like post-install rather than post-stage in ports Makefiles.
The premise for the update is pretty simple. Use the same logic at patch time and stage time via .USE. Operate over a different list of files and in a different directory (working directory vs. staging directory).
See also https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=218979