Logic is getting a bit tangled and bash conditional expressions seem awkward. An improvement would be to write a program in another language (Python? Rust? Go?) that would implement the logic, fill in the substituted variables and write the script to the output as a sequence of commands and with no conditionals. Two scripts would be convenient, the one that runs on the host and the other that runs in the chroot. A third script to perform post-install tasks such as adding a user, swap, etc could also be produced.
Logic is getting a bit tangled and bash conditional expressions seem awkward. An improvement would be to write a program in another language (Python? Rust? Go?) that would implement the logic, fill in the substituted variables and write the script to the output as a sequence of commands and with no conditionals. Two scripts would be convenient, the one that runs on the host and the other that runs in the chroot. A third script to perform post-install tasks such as adding a user, swap, etc could also be produced.