By M Ilyas Khan
BBC News, Karachi
The Pakistan Supreme Court's decision to allow exiled former Prime Minister Nawaz Sharif to return to the country leaves Gen Pervez Musharraf between a rock and a hard place.
Mr Sharif's supporters offered prayers of thanks
On the one hand, there is a new grouping of opposition parties, the APDM, determined to throw the military ruler out of power by street protests and through the courts.
On the other, there is the PPP, the largest political party in the country, which says it is willing to work with him - but only as a civilian president under an overhauled parliamentary system in which the prime minister dominates.
Many say this is exactly the situation that Gen Musharraf was trying to prevent when he "suspended" the country's top judge in March.
But unexpectedly for most observers, Chief Justice Iftikhar Mohammad Chaudhry decided to fight back, and started a process of mass mobilisation that now threatens to rip through the army-led political set-up in the country.
In July, the Supreme Court restored Justice Chaudhry, greatly embarrassing the government.
Now, as some observers put it, it has helped "resurrect" Gen Musharraf's bitterest rival.
Nawaz Sharif sacked Gen Musharraf as army chief in October 1999, and ordered an aircraft carrying him and more than 200 other passengers back from Sri Lanka to be prevented from landing at Karachi airport.
Mr Sharif was deposed in a coup
The military reacted by staging a coup, deposed Mr Sharif and charged him with endangering the lives of the passengers in what came to be known as the "hijacking case". He was sentenced to life in prison.
In 2000, Gen Musharraf pardoned him and sent him into exile in Saudi Arabia for 10 years under a controversial deal whose details are still not fully known.
But last month Mr Sharif approached the Supreme Court, pleading his right under the constitution to return to the country. It agreed.
Just as pro-Sharif activists celebrated outside the Supreme Court in Islamabad, Gen Musharraf was chairing a meeting of top aides in nearby President House to discuss the emerging scenario.
Analysts say one option the government may exercise to contain Mr Sharif is to arrest him as he enters the country.
Corruption cases have been reopened against Mr Sharif and his family. Government lawyers have also indicated that his conviction in the "hijacking" case may be "revived".
Independent legal experts say the higher courts are unlikely to agree because the conviction ended with a presidential pardon, as provided for in the constitution.
In the corruption cases, experts say, the government can only hope to confine Mr Sharif for a limited period of time as he could well be granted bail.
So the prospects for Gen Musharraf are not encouraging.
His term as president comes to an end in November, after which he will need to be re-elected by parliament in order to continue in office.
The term of parliament itself runs out in October, and national elections will have to be held before that to elect a new parliament.
Gen Musharraf wants to be re-elected from the current parliament in which his loyalists have a majority, and also wants to remain army chief.
Mr Sharif has considerable popular support
His plans are not likely to stand the test of law.
In addition, opposition groups united under the newly-formed APDM alliance are threatening street protests if Gen Musharraf tries to have himself re-elected.
Analysts expect this campaign to grow even fiercer when Mr Sharif, who heads the largest party in the alliance, is personally present in the country to lead it.
His return is also likely to spark defections from the pro-Musharraf wing of the Pakistan Muslim League, which contains former supporters of Mr Sharif who the military persuaded to defect after he went into exile.
The only other option left for Gen Musharraf, say analysts, is to strike a deal with another former Prime Minister, Benazir Bhutto, whose PPP has stayed out of the APDM.
But even then, Thursday's court ruling has left Gen Musharraf with fewer bargaining chips against Ms Bhutto than he had earlier.
The president may now work towards a deal with Ms Bhutto
He has been delaying the implementation of a "deal" that Ms Bhutto claims she clinched with the government following year-long negotiations.
In recent interviews, she has indicated broad contours of the deal that include legal re-adjustments to abolish a two-term limit on prime ministerial candidates, re-balancing of powers between the presidency and the parliament, and guarantees for free and fair elections.
She has also been demanding that court cases against her and Mr Sharif be dropped, and has indicated that she will work with Gen Musharraf only if he quits as army chief and gets himself re-elected from the next parliament.
So for many analysts, Gen Musharraf has his work cut out, and there is little he can apparently do about it unless a "surprise court verdict" gives him another lease of life.