Let's put ourselves in Iran's shoes for a second:
1. Two countries on each side of them have been ransacked by the US and are now being occupied by them (Afghanistan and Iraq) even though they posed no threat to the US
2. The country with by far the largest military (including nukes) has been threatening them with airstrikes and stuff for years (Israel)
3. They have been effectively ostracized and kicked out of the global diplomatic scene due to US-lead efforts, and they are under heavy economic sanctions.
Iran has three options:
1. Give up its nuclear program. This would make them extremely vulnerable for a US-lead invasion for other reasons (remember, Saddam didn't have nukes, but got invaded anyway.)
2. Pretend that they have nuclear weapons, but only use their nuke programs for energy. This is what Saddam did, trying to deter an invasion. Obviously, it didn't work.
3. Hurry the fuck up and build a nuke ASAP. This would definitely deter a US invasion, and would restore a balance of power in the Middle East. All the US has to do is put a few nuclear silos in Iraq to counter any Iranian nuclear capability. If they were to make a nuke, it would be a single crude one, compared to America's 8,000 tactical nukes. Not really a threat, but it would ensure Iranian security.
Option three is pretty much their only choice. They would NEVER use a nuclear weapon to attack someone, only to deter invasion and as a possible response.
So yeah, Ahmedinijad is right. Attacking Iran now is idiocy and a complete waste. Iran is only acting in its own security.