Firstly, the line number mentioned in the error message is just where you start looking for a problem. You then work BACKWARDS through your script. The error message is saying it found an unexpected IF statement. This means the error is just before the IF statement. Look closely at the line above it (line 2 in this case).