CAIRO (Reuters) - When President Hosni Mubarak was toppled in a 2011 revolt and later jailed, Egyptians who rose up against him hoped he would be a convict for life, or executed.
However, an Egyptian court ordered Mubarak's release on Wednesday, his lawyer said, meaning he could leave prison on Thursday as there are no longer any legal grounds for his detention.
Mubarak is being retried on charges of ordering the killing of protesters during the uprising, but has already served the maximum amount of pre-trial detention permitted in that case.
The surprise move could deepen political turmoil in Egypt, where the army-backed government is cracking down hard on Mubarak's old Islamist enemies in the Muslim Brotherhood.
Mubarak never expected to be president. But when a burst of assassins' bullets thrust him into the job in 1981, he made it his life's mission never to give it up.
His story became Egypt's story for the next 30 years until, finally, his people found they could write it themselves, in a revolution that shook the world and consigned him to history.
Unremarked vice president to the mercurial Anwar Sadat, he was a safety-first stopgap after Islamists gunned down the president beside him. Few thought he would last. Yet slowly, surviving attempts on his own life, he became "Pharaoh", to preside over decades of stagnation and oppression on the Nile, offering his people a mantra - either Mubarak, or mayhem.
Many believed him, not just in Egypt. Successive U.S. administrations, from Reagan to Obama, showered him - and the biggest army in the Middle East which kept him in his palaces - with billions of dollars in gratitude for his loyalty to Sadat's Cold War switch of allegiance and historic peace with Israel.
But it was Mubarak's struggles with the Islamists, who by killing Sadat accidentally handed power to a man who would spend decades suppressing them. This defined his politics, and the legacy his downfall bestowed on Egypt and the world at large.
When Soviet communism evaporated, and militant Islam emerged as the West's great enemy, Mubarak - familiar, reliable, a sphinx in his thoughts - was there, weathered by a lifetime in Egypt's endless war between army and religion, to steady American nerves.
An immoveable object at the helm of Egypt for almost 30 years, he finally met an irresistible force in his own people.
In two terse sentences, his vice-president Omar Suleiman declared on February 11, 2011 that the 82-year-old leader had stepped down, after 18 days of mass protests against his rule.
Egypt erupted in joy, humiliating a man who always posed as a benign, tireless father figure.
His downfall, under fierce pressure from pro-democracy protesters across Egypt, was apparently orchestrated by the military after it lost confidence he could weather the storm.
But the confrontation between secular might and popular Islam went on after Mubarak's demise.
His old enemies in the Muslim Brotherhood propelled their man, Mohamed Mursi, to office with socialists, leftists, Christians and more liberal-minded Muslims ranged against him.
Fears that Mursi was trying to monopolize power and his mismanagement of the economy led to mass protests.
The armed forces took over on July 3, led by Mubarak's former head of military intelligence, General Abdel Fattah al-Sisi, now Egypt's de facto ruler.
Sisi announced a political road map he promised would deliver democracy. He also revived the state's crackdown on the Brotherhood, and raised it to unprecedented levels of ferocity.
Like Mubarak, Sisi branded his Islamist enemies "terrorists" and waged a propaganda campaign again to persuade Egyptians that they must be defeated at all costs. The Brotherhood accused him of trying to revive the old regime.
NO ANSWER BEYOND "MUBARAK"
The rise of political Islam in the rubble of Western-backed Arab dictatorships is one Mubarak long predicted. He told U.S. President George W. Bush his idealism was misplaced in ousting Saddam Hussein, however odious Mubarak also found the Iraqi leader. Only their mutual enemies in al Qaeda and Iran would benefit from Saddam's downfall, Mubarak said.
Yet to the question how Egyptians should be governed in future, he never had an answer beyond "Mubarak" - to the point of refusing even in his 80s to indicate a successor. Washington expected him to go on rigging elections till he died, when his son Gamal would possibly carry on the dynastic name.
A new generation challenged Mubarak's pessimistic view of Egypt's future, inspired by events in little Tunisia, sickened by the most recent round of flagrant electoral fraud and fed up with their low horizons as the wider world moved on. When they took on his security thugs, the president had so far imbibed his own propaganda he seemed more sorry than angry.
A man who cast himself as father of the nation - "governing Egypt is not a picnic", he said - Mubarak spoke of his own self-sacrifice, even as his family helped themselves to riches, and went on patronizing Egypt's 82 million people to the end.
Only when his generals began to desert him, fearful their own privileges should be swept away in the flood, and Americans sided with the popular will, did he relent. At first Mubarak insisted he would retire only later, but finally he was flown off to his Red Sea retreat, true to his refusal to take the route of ignominious exile demanded by some of the millions on the streets.
With characteristic grandeur, he would not leave his country. "Egypt and I shall not be parted until I am buried in her soil," he said - a decision that cost him his liberty and, friends said, his health. Remarkable while in office for his robustness in old age, even if hair dye played its part in the image, friends told of calamitous ailments once he was put on trial and convicted.
Many Egyptians, including those angry that he was not condemned to death, were skeptical of that, though they saw him wheeled into the dock on a stretcher, sunglasses shielding his inner thoughts.
It was a power that came unbidden, as Mubarak and Sadat watched a military parade in Cairo on October 6, 1981, the eighth anniversary of an Egyptian attack on Israeli forces in Sinai.
Islamist militants, furious with Sadat for making peace with the Jewish state in defiance of his Arab counterparts, leapt from an army truck, lobbing grenades and spraying the reviewing stand with automatic rifle fire. Within two minutes, Sadat and 11 others were dead. Mubarak, remarkably only slightly hurt, was head of the most populous Arab state, a nation 7,000 years old.
Mubarak had risen from humbler beginnings than many in a military which took power from King Farouk in 1952, when he was a 24-year-old pilot, flying Spitfires by some accounts. In time the system of military rule let Mubarak govern Egypt longer than anyone since the 19th century Ottoman general who founded the royal house.
There was some irony that the Brotherhood and other Islamists were largely absent in the early days of the "Facebook Revolution". Instead it was an unfamiliar enemy, in jeans and Prada T-shirts, that Mubarak faced. Baffled, he had no response.
Jailed for life on June 2, 2012, he was sent to Tora Prison in a Cairo suburb, where many of his enemies were jailed.
(Writing by Alastair Macdonald; Editing by Michael Georgy and David Stamp)