Ayman al-Zawahiri, the Egyptian-born leader of al Qaeda, has seen this movie before: An Islamist party does well at the polling booth only to be overthrown by a military coup that then plunges the country into chaos.
This is what happened in Algeria in 1991. Tens of thousands died in the subsequent Algerian civil war that ripped the nation apart during the 1990s.
The lesson that Zawahiri drew from the Algerian war was that participating in democratic elections was strictly for suckers; far better to seize power through violence and then impose Taliban-style sharia law because "the crusaders" and their allies in the Arab world would never allow the emergence of a true Islamist state.
In 1991, the same year that the Algerian civil war began, Zawahiri released his first book, "The Bitter Harvest." The book was a vicious diatribe against the Muslim Brotherhood and other similar Islamist parties for participating in "democracies, elections and parliaments."
Now Zawahiri gets to say "I told you so." Earlier this month Zawahiri posted a 15-minute recording on militant websites. In the recording Zawahiri explained that the military coup that deposed Egypt's elected president, Muslim Brotherhood leader Mohamed Morsy, proved that democracy had failed.
Until recently Zawahiri has been largely overshadowed by his charismatic former boss Osama bin Laden who was killed in a U.S. Navy SEAL raid in Abbottabad, Pakistan, two years ago.
Now the 62-year-old Zawahiri has dramatically stepped into the spotlight as the leader of al Qaeda and its several affiliated groups around the world.
The message traffic during the past weeks between Zawahiri and the commander of al Qaeda's virulent Yemeni affiliate contained code words suggesting some kind of attack was potentially imminent.
Those messages were intercepted by American intelligence and led to the unprecedented closure last week of some two-dozen U.S. embassies and consulates across the Middle East, South Asia and Africa. Germany, France and Britain also closed their embassies in Yemen because of the threat.
Zawahiri's success or failure as the head of al Qaeda will be crucial to how the terror network moves forward as it marks its first 25 years of existence this weekend.
A recent Harvard Kennedy School study of 131 terrorist groups that have gone out of business found that they averaged 14 years of existence.
Al Qaeda has already shown an ability to survive well beyond the life of an average terrorist group. The big question is whether Zawahiri can successfully lead the al Qaeda network so it can thrive in today's chaotic conditions in the Middle East and can last another 25 years.
Zawahiri is a humorless religious fanatic whose forehead is marked with a prominent black scar known in Arabic as a zabiba or "raisin," the result of the many decades he has spent touching his head repeatedly to the floor as he prostrated himself in prayer.
Despite his dour personality, Zawahiri has proven to be more capable as the leader of al Qaeda than many analysts -- including myself --- had initially thought.
In the past year Zawahiri has brought two new official affiliates into the al Qaeda network: Somalia's militant al-Shabaab group and Syria's Jabhat al-Nusra, which is widely regarded as the most effective force fighting the Assad regime.
Zawahiri also had no problem transferring already existing al Qaeda affiliates' allegiances from bin Laden to himself. In the three months following bin Laden's death, the leaders of al Qaeda in Iraq, the Yemeni-based al Qaeda in the Arabian Peninsula and the North African al Qaeda in the Islamic Maghreb all pledged their allegiance to Zawahiri as their new overall commander.
Zawahiri has been preparing for this moment all his life, first setting up a jihadist cell in his native Cairo when he was only 15. Like many revolutionaries before him Zawahiri is the son of privilege -- he's from a prominent Egyptian family of ambassadors, lawyers and clerics.
As a teenager Zawahiri believed that the Egyptian government led by Anwar Sadat had abandoned Islam and therefore had to be overthrown. Sadat signed Egypt's ceasefire agreement with Israel in 1979, effectively signing his own death warrant.
In 1981, now trained as a surgeon, Zawahiri was arrested along with hundreds of others for his alleged role in Sadat's assassination and was imprisoned and tortured by Egyptian authorities, an experience that further radicalized him.
Once he was out of jail in the mid-1980s, Zawahiri made his way to Peshawar, Pakistan, where thousands of militant Arabs were then gathering to support the Afghan jihad against the Soviet Union.
It was there in 1986 that Zawahiri first met bin Laden, at that time the shy, monosyllabic son of a Saudi billionaire. For bin Laden the slightly older, cerebral Zawahiri presented an intriguing figure, someone far more experienced politically than himself. For Zawahiri, bin Laden also presented an interesting opportunity: someone who was on his way to becoming a genuine war hero in the jihad against the Soviets and whose deep pockets were well known. They would go on to embark on a marriage of convenience that would have hellish consequences.
During the late 1980s Zawahiri gradually won over bin Laden to his more expansionist view of jihad. Faraj Ismail, an Egyptian journalist who covered the Afghan war against the Soviets, recalls that it was Zawahiri "who got Osama to focus not only on the Afghan jihad, but regime change in the Arab world."
Bin Laden then took the next step, urging Zawahiri to see that the root of the problem was not simply the Arab "near enemy" regimes, but the "far enemy," the United States, which propped up the status quo in the Middle East, a shift in strategy that took place when al Qaeda was based in Sudan in the early 1990s. Bin Laden lectured to his followers there about the necessity of attacking the United States without which the "near enemy" regimes could not survive. Noman Benotman, a Libyan militant who was then close to bin Laden and Zawahiri, recalled that, "Osama influenced Zawahiri with his idea: Forget about the 'near enemy;' the main enemy is the Americans."
When Zawahiri first arrived in Taliban-controlled Afghanistan in 1997, following a six-month spell in a Russian jail, his relations with bin Laden were on a quite different footing than they had been a decade earlier. Bin Laden was now the head of a fast-expanding terrorist organization while Zawahiri was the penniless leader of an obscure Egyptian terrorist group.
It was now bin Laden who took Zawahiri under his wing. And even then bin Laden kept Zawahiri at some distance. It was only in the summer of 2001 that al Qaeda's leader told Zawahiri the details of the coming attacks on New York and Washington, and that was only after Zawahiri's Egyptian Jihad Group had formally merged with al Qaeda in June.