When an outgoing president has as many enemies as does Iran's Mahmud Ahmadinejad, there would seem to be little future left for him.

It is not only Ahmadinejad's reputation abroad that plunged during his two terms in office. Statements at the United Nations in which he called the Holocaust a myth and the 9/11 attacks a "mysterious" precursor to the wars in Afghanistan and Iraq helped make him an unpopular figure in many countries.

But more dangerously, Ahmadinejad's standing has also plummeted within Iran's establishment itself. That is due to his efforts to increase his power at the expense of influential figures around Iran's supreme leader. And it suggests that when he leaves office on August 3 he could find a host of enemies gunning for him.

Scott Lucas, an Iranian affairs expert at Britain's Birmingham University, says that Ahmadinejad's second term has seen him transform from a close ally of Supreme Leader Ayatollah Ali Khamenei into a president openly at odds with the ruling clerical establishment.

"The real bust-up between Ahmadinejad and the supreme leader's office was in 2011 because there was a power play for control of the Ministry of Intelligence and before that the Foreign Ministry," Lucas says. "And Ahmadinejad succeeded in part in getting hold of the Foreign Ministry, but the Ministry of Intelligence was a red line -- that's the supreme leader's domain. And since then there has been this increasingly tendentious relationship between Ahmadinejad and not just the supreme leader but different parts of the system."

Signs that many in Iran's establishment would like to punish Ahmadinejad abound.

Well ahead of the June 14 presidential election, Ahmadinejad saw his handpicked successor, Esfandiar Rahim Mashaei, disqualified from running.

Then, during the campaign, all those candidates who were approved to run repeatedly accused Ahmadinejad of mismanagement of the economy. The winner, Rohani, also suggested that Ahmadinejad's administration was responsible for "extremism." Rohani never defined exactly what he meant, but "extremism" is widely understood to be a code word for the brutal crackdown on reformists that followed the Green Movement protests against Ahmadinejad's reelection in 2009.

Not Another Uproar

Yet analysts say that as much as Iran's establishment has attacked Ahmadinejad, there are good reasons why it might stop short of trying to punish him when he leaves office.

A protester wears a mask depicting Iranian President Mahmud Ahmadinejad rallies outside UN headquarters in New York in September 2011, where he said that "arrogant powers" threatened anyone who questioned the Holocaust. Many diplomats walked out.

"At this point, Iran does not want to cause another uproar after the election of Hassan Rohani," says Hossein Askari, the Iran professor of international business and international affairs at George Washington University. "And Hassan Rohani, especially, is a very practical person and I think he has got his plate full with other important things that he wants to do. And he does not want to divert the attention of the nation to giving Mr. Ahmadinejad more importance than he deserves."

Askari says that prosecuting Ahmadinejad risks not just distracting the nation but damaging the regime itself. And that is neither in the establishment's nor Ahmadinejad's best interests.

"We have to really be cognizant of the fact that [Ahmadinejad] does know a lot about various things that have been done by the regime and, of course, he could expose things," Askari says. "But I don't believe he will do that because he has ties to people that he also wants to see succeed in the regime in the future."

Still another reason why punishing Ahmadinejad could damage the establishment is that Iranians remember quite well that Khamenei picked Ahmadinejad to run for the presidency in 2005, at a time when Ahmadinejad was the appointed mayor of Tehran and little known nationally.

Ahmadinejad became Iran's first noncleric to hold the presidency since 1981 and thus a populist symbol for the Islamic Revolution as a continuing grassroots movement. Maintaining that revolutionary myth by glossing over Ahmadinejad's later performance is ultimately more valuable than tarnishing it.

Criminal Court Summons

Still, that does not rule out skirmishes during Ahmadinejad's last days in office and even immediately afterward as some of his sworn enemies try to settle personal scores.

On June 17, the outgoing president received a summons to appear before a criminal court by November 26.

Quiz: How Fluent Are You In Iranian Political Jargon?

The government website dolat.ir said the summons arose from a complaint lodged by parliamentary speaker Ali Larijani and a parliamentary commission but provided no details of the case.

The summons comes after Ahmadinejad shocked parliament by using it as a forum in February to publicly accuse the family of the parliament speaker of corruption.

The enraged parliamentary speaker denied any corruption and lambasted Ahmadinejad for not observing the "basics of proper behavior."

Askari says that the incident might best be seen as warning shots from each side to the other to tread carefully.

"It was unfortunate that [Ahmadinejad] used that in parliament because the Larijani family is very powerful and very close to the supreme leader," Askari says. "But I think he has made his threat and they know that he has got other information that he could use. But he will also be told to watch his step, because in Iran they will not have mercy if he starts to lash out at people. So, I think he will be contained and I think he will go on his way to try to make a fortune for himself."

Ahmadinejad, who initially cultivated an image of a humble man of the people, has shown an increasing taste for luxury in his second term. Askari expects he may seek to become rich as a deal-broker using the close ties he cultivated during eight years in power -- particularly with the Islamic Revolutionary Guards Corps -- and the contacts he retains inside the establishment. That would be an acceptable form of containment for all sides.

Ahmadinejad, 56, has so far given little indication of how he sees his own future. He has only said his immediate plan is to return to university teaching. He holds a doctorate in transportation and was a lecturer prior to his political career.