He was never going to bow out quietly or gracefully. But few predicted just how full of sound and fury Mahmoud Ahmadinejad's final months in office would be.
The Iranian president is taking on a host of powerful political enemies, defiantly spurning orders from the country's supreme leader, Ayatollah Ali Khamenei, to end the infighting that threatens to destabilise the regime.
The ayatollah, who once championed the populist president, wants his sanctions-strapped country to show a unified face as it braces for nuclear talks with world powers in Kazakhstan next week. He is also determined that presidential elections in June proceed peacefully without any repetition of mass street protests that were ignited by Mr Ahamdinejad's disputed re-election four years ago.
But the Iranian president is showing no sign of obeying the ayatollah.
Mr Ahmadinejad has been levelling charges of corruption against leading opponents in a hostile parliament who have responded in kind. The feuding conservative factions have also accused one another of plotting to rig the June presidential elections.
"Given Ahmadinejad's belief that going on the offensive is always the best strategy, these conflicts are bound to continue until the day he leaves office," said Farideh Farhi, an Iran expert at the University of Hawaii.
At stake for Mr Ahmadinejad is more than just his legacy. When he steps down, he wants protection for himself and his allies - many of whom face serious legal charges of economic and financial mismanagement as well as of corruption - once he stands down.
But his ambitions go beyond mere survival. He is grooming his closest and most controversial ally, Esfandiar Rahim Mashaie, to succeed him. This, Mr Ahmadinejad apparently hopes, will enable him to make a Vladimir Putin-style comeback in 2017, while he remains a potent behind-the-scenes political force in the interim.
Mr Mashaie is despised by the president's opponents, who accuse him of leading a "perverted", anticlerical current in Mr Ahmadinejad's camp.
There seems little doubt that the president's favoured protégé will be prevented from making it on to the ballot by the Guardian Council, a hardline body that vets candidates. Mr Ahmadinejad, however, has backup alternatives to contest the election.
But Mr Khamenei wants his unruly president to have no influence beyond August, when he is constitutionally bound to step down after two four-year terms.
The ayatollah cannot simply sack him before June's vote because he endorsed Mr Ahmadinejad's disputed re-election four years ago as a "divine assessment".
Meanwhile, the Iranian president, a political streetfighter, has often warned he has incriminating secret files on his enemies that he will unveil if they push him too far.
Mr Ahmadinejad "knows where all the bodies are buried and is very eager to talk", said Suzanne Maloney, an Iranian affairs expert at the Brookings Institution in Washington. "That makes Ahmadinejad the most dangerous man in the Islamic republic, she told AP recently.
The Iranian president proved he was ready to dish the dirt during an extraordinary session of parliament on February 3 when MPs impeached his interior minister.
In retaliation, Mr Ahmadinejad aired an audio tape that purported to reveal corruption and influence-peddling inside the family of the parliamentary speaker, Ali Larijani, who is tipped to be a presidential candidate in June.
He is one of Mr Ahmadinejad's most high-profile critics along with another of five influential Larijani brothers, Sadegh Larijani, who heads Iran's judiciary.
The parliamentary speaker accused the president of "mafia-style" blackmail tactics and actions that "corrupt the integrity of society".
Days later, supporters of Mr Ahmadinejad hurled shoes and prayer tablets at Ali Larijani when he was making a speech in the holy city of Qom.
Appalled by these scenes, Mr Khamenei - who warned in October that public bickering between the branches of government was tantamount to "treason" - scolded both sides last weekend.
The ayatollah branded Mr Ahmadinejad's behaviour in parliament as "inappropriate" and "illegal". In milder terms, he chastised Mr Larijani for showing "extravagance in defending himself".
The Larijanis - loyal acolytes of the supreme leader - together with Iran's parliament, apologised in to Mr Khamenei on Sunday, thanking him for his "compassionate wisdom" and pledging their obedience.
The president also reaffirmed his allegiance to the ayatollah - but notably offered no apology. For many hardliners, such behaviour is akin to apostasy.
The dramatic showdown in parliament on February 3, however, may have marked the climax rather than the beginning of more serious political turbulence.
While Mr Ahmadinejad will remain "provocative", his enemies are more likely to obey the supreme leader's calls for restraint, Ms Farhi said in an interview.
The ayatollah's inability to rein in his wayward president raises questions about his powers as Iran's absolute ruler.
Some suspect Mr Khamenei cannot control the president because Mr Ahmadinejad has incriminating evidence against the supreme leader, his aides and family from secret intelligence files.
But others believe the president used his strongest card during the stormy parliamentary session and is now bluffing. Scott Lucas, an Iran expert at Birmingham University in England, said: "I really doubt Ahmadinejad has many more aces up his sleeve. He's using a poor hand to take it as far as he can to make his opponents back down."