The check for the exit status of the 'stage1' script was broken. Change "X$?" != "X0" to "X?" != "X0"