In one of those ironies so common in the Third Word, King Hassan II of Morocco has his Algerian neighbors and adversaries to thank for solidifying his once-shaky throne.
At no previous point in his checkered, 16-year reign has the monarch appeared stronger than since Algeria started backing the Polisario Front's guerrilla war challenging the 1975 Moroccan-Mauritanian carve-up of the former Spanish Sahara.
Directly or indirectly, Algerian hostility has bolstered the king at home and persuaded conservative oil states to help pay for expanding and modernizing his armed forces. It has helped brighten his image in black Africa as a peace-keeper and failed to stop him from rescuing a diplomatic situation in the Sahara that once seemed a losing one for Morocco.
The only sour note is Morocco's inability to stamp out the Algeria-based and financed hit-and-run raids. Polisario propagandists say the raids refute the king's claims that the Sahara issue is settled once and for all.
In the past month, the king has had to airlift a Moroccan battalion to protect an economically vital iron-mining town in Mauritania, where his allies' small, fledging armed forces proved unable to cope.
Polisario seems capable of continuing to mount pin-prick raids, but its guerrillas, believed to number between 3,000 and 7,000, appear incapable of overthrowing either the Mauritanian or the Moroccan government in what has become a largely political and pyschological war waged from a safe heaven in Algeria.
If Algeria did withdraw its support, according to various diplomatic observers, the Front would either "collapse even worse than the Kurds did when Iran dropped them" or "perhaps not go away, but its military threat would," as one observer said.
No immediate military or diplomatic solution is in sight - if only because both Hassan and Algerian President Houari Boumediene are stubborn men men and ideological rivals.
Although Algerian and Moroccan troop reinforcements have been sent to the northern edge of the Sahara front in the past two or three months, no major convention war is seen likely between the two countries.
The Mauritanian front is much more porous, both because of its enormous areas of desert and because of the potentially conflicting loyalties of many Maritanian and Polisario troops who belong to the same desert normadic tribes.
The presence of Moroccan troops in Mauritania also plays on latent Mauritanian fears of a Moroccon takeover, inherited from the long period - which ended only in 1969 - when Morocco claimed Mauritania as part of a greater Morocco.
If anything, the growing importance of the Mauritanian front has underscored Morocco's apparent relative success in coping with Polisario in southern Morocco and the two-thirds of the former Spanish Sahara that the Rabat government took over.
Early on, the Moroccans floundered around the desert, unused to hit-and-run guerrilla warfare waged by troops who knew every rock and sand dune.
Morocco's failure to buy the services of the thousand-strong former local troops attached to the Spanish army provided Polisario with the opportunity to pick up trained cadres.
But Morocco has since honed its tactics. Learning from the French experience in the Algerian war - and America's in Vietnam - Morocco now uses a helicopter-borne strike force to chase down the guerrillas.
Despite a virtual news blackout by the Moroccan government - in keeping with its contention that the Sahara no longer constitutes a problem - observers believe that Moroccan casaulties are well down from 1976, the peak year.
Last year Morocco was thought to be losing between 30 and 50 men killed every month, mainly victims of Chinese-made plastic anti-personnel mines that proved hard to detect.
In June, Moroccan officials claimed that 1,500 Polisario guerrillas had been killed in the war, compared to 65 to 75 Moroccan dead and between 150 and 200 wounded. A month earlier Poliasrio announced that its forces had killed 14,600 Moroccan and Mauritanian troops.
Moroccan nationalism is so strong that such losses have not diminished the popularity of Hassan's paolicy. The king won an overall parliamentary majority in June in the nearest thing to an honest election that Morocco has known since 1963. Even the opposition - ranging from the Communist to the right-wing Istiqlal - backs him on the Sahara.
The Sahara issue has also allowed Hassan to mend fences with the armed forces. They had been under a cloud after their involvement in 1971 and 1972 in very nearly successful coup attempts. The Sahara mission climaxed their rehabilitation which began with the Moroccan expeditionary force's performance on the Golan Heights in the 1973 Arab-Israeli war and continued this spring when 1,200 troops were airlifted in French transports to help Zaire put down incursions from Angola in Shaba, Zaire's rich mining province.
The Shaba case - and a series of other military confrontations among African neighbors - tended to take the spotlight off the Sahara, which two years ago was the continent's most prominent intra-African squabble.
Diplomatically, Morocco has been instrumental in turning around a potentially disastrous situation that early in the summer of 1976 saw the foreign ministers of the Organization of African Unity vote to order Morocco and Mauritania to withdraw their troops from the former Spanish colony.
Algeria has lost ground diplomatically. Once pressing for the radical leadership of the Third World, Algeria now finds itself outvoted on the Sahara in the OAU, the Arab League and the United Nations.
Fears of Algerian radicalism - and the Soviet threat that rigthests see behind the Polisario Front - have prompted massive investments in Morocco by Saudi Arabia, Iran and other oil states in the Persian Gulf.
Saudi Arabia, especially, has underwritten French, American and Spanish arms orders worth many hundreds of millions of dollars.
Hassan has increased the armed forces' strength from 55,000 men to between 90,000 and 95,000 in little more than a year.
In 1975 the king more than doubled Morocco's military budget from $225 million to $518 million. (Algeria's was $275 million that year.) This year Morocco's military budget rose to $733 million, with $433 million ticketed for equipment purchases.