How Pakistan's Musharraf shook off legal cases
When Pakistan's former military ruler, Pervez Musharraf, ended his four-year exile and returned to the country to participate in May's elections, he took many people by surprise.
He had made many powerful enemies during his eight-year rule, and some of them had been in power or were about to be.
Besides, a slew of criminal charges had been lying dormant against him in various courts, awaiting his return.
But on Monday, a court granted him bail in the last of the four criminal cases instituted against him, paving the way for his release.
How he managed to turn what many had considered a miscalculation, into a legal triumph can be partly explained by the shifting nature of the country's political and judicial systems.
He obviously returned to Pakistan thinking he had done no wrong.
He also believed that his party could win some seats in parliament on the strength of an economic bubble his policies had created in the services sector in urban areas.
In addition, he knew that the country's powerful military, of which he had been the head until 2008, would not allow the politicians and the judges to drag him through the courts and convict him as a criminal.
Many thought he had miscalculated because the judges that he sacked in 2007 had been swept back to their positions by a wave of popular agitation that forced him out of power in 2008.Insurgent judiciary
Since then, the judges, led by Chief Justice Iftikhar Chaudhry, have used a policy of "judicial activism" to encroach significantly on the sphere of the executive.
It is generally believed that what happened to Mr Musharraf was largely the result of an insurgent judiciary wanting to make history by arraigning a former army chief, something that no-one had done before.
The May elections also restored a political leadership that had been ousted from power by Mr Musharraf in a military coup in 1999. The expectation was that Prime Minister Nawaz Sharif's government would go the whole way to bring him down.
It mostly seemed that way in the beginning.
Days after landing in Pakistan in March, Mr Musharraf suffered the insult of having a shoe thrown at him in a crowded corridor of a court building. It narrowly missed.
A little more than a week later, the Supreme Court instituted hearings to explore if he could be put on trial for treason for suspending the constitution and imposing emergency rule in November 2007.
In April, an election tribunal, comprising high court judges, declared he could not be a candidate.
He was arrested later that month for putting nearly 60 senior judges under house arrest in November 2007, when he was president.
Over the next few months, his re-arrest was ordered in two high-profile murder cases; the 2006 killing of a rebel Baloch politician, Akbar Bugti, in a military operation, and the 2007 assassination of former Prime Minister Benazir Bhutto in a gun and bomb attack.Public interest waning
Since April, when he was first detained, he has remained under house arrest at his residence in Islamabad.
During this period, public interest in his fate has been overtaken by other events, and the high pressure of the initial days has gradually eased.
He managed to get bail in the judges' detention case when the complainant, a lawyer, decided he did not want to pursue the matter. Whether he was persuaded by someone from the military remains a matter of speculation.
He was also granted bail in the Benazir Bhutto murder case on 20 May.
The bail came two weeks after the chief prosecutor in the case, Chaudhry Zulfiqar, was gunned down in Islamabad, ostensibly by militants with alleged links to the 2008 Mumbai attacks - another case that he had been investigating.
On 9 October, Mr Musharraf was given bail in the Akbar Bugti case, clearing him of all charges pending against him.
But before he could be set free, he was re-arrested in the case of the 2007 Red Mosque siege in Islamabad in which more than 100 people, many of them armed militants, were killed.
With the bail granted in this case as well on Monday, Mr Musharraf is now set to walk free.Signs of retreat
Legal experts say there was no evidence linking him directly to any of the offences for which he was charged.
But there are also those who point out that if he were not a former army chief, he would have still be in the dock, given the vast powers the judiciary has arrogated to itself.
The government of Prime Minister Sharif is also showing signs of a retreat.
The only case in which Mr Musharraf could have been pinned down legally - the high treason case - failed to take off because the government, which alone can initiate a treason case under the law, has been procrastinating despite a commitment it made in the court in August.
So unless he is charged in a fresh offence, Mr Musharraf cannot be kept in detention any more or banned from travelling abroad, experts say.
The question is, will he opt to leave the country, which most elements in the government and the army would like him to do, or stay on to carve a political future for himself in Pakistan?
The situation should become clear over the next few days.