Refactor gen-version.sh script

Review 1028 by @tryneus
Closes #1636