From a quick glance it looks like you are missing a } before the until. The } you have before until closes the foreach loop rather than the do {} . The } after exit is one too much.
I would strongly recommend using a proper indentation/bracket style, it tends to make reading and debugging code a bit easier :)
Comment on Re: Syntax error in using do-until loop: How can I fix it?