One way of automating this would be to just do here `source $SCRIPTNAME`. This works just fine because the new script is called and when it's done, the old script exits without executing the old instructions. (and maybe instead of `return 0` it would be better to write `return $?`).

However, this assumes that the script is intended to be sourced and not just executed. Not sure how to fix that yet.