let with_trap ~statement_separator ~exit_with script =
let variable_name = Unique_name.variable "genspio_trap" in
let die s =
sprintf " { printf -- '%%s\\n' \"%s\" >&2 ; kill -s USR1 ${%s} ; } " s variable_name in
String.concat ~sep:statement_separator [
sprintf "export %s=$$" variable_name;
sprintf "trap 'exit %d' USR1" exit_with;
script ~die;
]