(CNN) If you were writing a "how to" textbook for overthrowing a democratically elected government, the curious coup d'état in Turkey would definitely lead in the "doomed to fail" chapter.
Brief, messy, chaotic and bloody, it lacked broad public support and didn't have a popular figurehead.
With an iPhone and a FaceTime account, Turkish President Recep Tayyip Erdogan easily turned what could have been the end of his public career, and potentially his life, into a political bonanza, a "gift from God," as he said.
In less than 24 hours, he emerged stronger than ever. His government has detained more than 6,000 officers, judges, prosecutors and others over suspected involvement in Friday's failed coup, and it seems the purge is only just beginning.
He has called upon his followers to take to the streets to show their support, and they've responded. The once-powerful Turkish military had been diminished after a series of previous purges, and is now more than ever on the defensive. Its old self-appointed role as protector of the constitution, of secularism, of the legacy of the founder of modern Turkey, Mustafa Kemal Ataturk, is in tatters.
Sharp contrast with Morsy's ouster in Egypt
And in the "how to do it right" chapter, on the other hand, would be the coup that brought President Abdel Fattah el-Sisi to power in Egypt in the summer of 2013. It was well-planned and executed, had broad public backing and featured a popular leader.
Failed military coup in Turkey
Police try to stop people from attacking a judge, suspected in the failed coup plot, in Erzurum, Turkey on Tuesday, July 19. Turkey has fired or suspended about 50,000 people as the government intensifies a crackdown following last weekend's failed coup attempt. Teachers, journalists, police and judges have been affected.
Police escort Turkish soldiers, accused of taking part in the attempted coup, as they leave a courthouse in Istanbul's Bakirkoy neighborhood on Saturday, July 16.
Turkish police officers cover the eyes of soldiers as they are transported in a bus from the courthouse in Istanbul on July 16.
Akin Ozturk, front row, center, a four-star general and former commander of the Turkish air force, is among those in police custody whom President Recep Tayyip Erdogan's government has accused of having led the failed coup attempt.
In a mass detention in Ankara, dozens of detainees are forced to kneel, partially stripped.
A man waves a Turkish flag from a car roof during a July 16 march around Kizilay Square in Ankara after the attempted military coup.
Damaged vehicles are abundant outside the presidential palace in Ankara on July 16.
People kick and beat a Turkish soldier suspected in the attempted coup on Istanbul's Bosphorus Bridge on July 16.
Women react after people took over a military position on the Bosphorus Bridge in Istanbul.
People gather outside the Turkish Parliament in Ankara during an extraordinary session after the failed coup attempt.
Turkey Prime Minister Binali Yildirim addresses the Turkish Parliament after the failed coup attempt.
A protester rests on a bench as smoke billows from the Turkish military headquarters in Ankara.
People stand under a huge Turkish flag during a march around Kizilay Square in Ankara in reaction to the attempted coup.
Members of the Turkish military surrender on Istanbul's Bosphorus Bridge on after a failed coup attempt.
People protesting against the coup wave a Turkish flag on top of a monument in Istanbul's Taksim Square.
Clothes and weapons belonging to soldiers involved in the coup attempt are scattered on Bosphorus Bridge in Istanbul.
A Turkish police officer in Istanbul embraces a man on a tank in the wake of the violence overnight.
Smoke billows from the direction of the Presidential Palace in Ankara on July 16.
People gather on top of a Turkish military tank in Ankara in the morning after the coup attempt. National intelligence officials said the coup was put down and that the government remains in control.
Turkish people wave national flags from a car in Istanbul.
People gather around a car damaged by a tank in Kizilay Square early on July 16.
People take to the streets near the Fatih Sultan Mehmet Bridge during clashes with military forces in Istanbul.
People escape the clashes in Ankara early Saturday.
Tanks move into position as Turkish citizens attempt to stop them in Ankara.
Citizens in Sivas rush to the streets during the chaotic coup attempt.
A man approaches Turkish military with his hands up at the entrance to the partially closed Bosphorus Bridge in Istanbul.
A wounded man is given medical care at the entrance to the Bosphorus Bridge in Istanbul after clashes with Turkish military.
People react in front of the Justice and Development Party's headquarters in Karabuk.
Soldiers secure an area as supporters of Turkish President Recep Tayyip Erdogan protest in Istanbul's Taksim Square.
Turkish military members make their way through the streets of Istanbul.
Supporters of President Recep Tayyip Erdogan protest in front of soldiers in Istanbul's Taksim Square.
President Recep Tayyip Erdogan speaks on CNN Turk via a FaceTime call in Istanbul after members of the country's military attempted to overthrow the government.
Turkish soldiers block Istanbul's iconic Bosphorus Bridge.
Turkish security officers detain police officers, seen in black, in Istanbul, during a security shutdown on the Bosphorus Bridge.
A military airplane is seen flying over Turkey. President Recep Tayyip Erdogan urged people to take to the streets and stand up to the military.
People take cover near the Bosphorus Bridge as military airplanes fly overhead.
Turkish soldiers are seen on the Asian side of Istanbul.
A Turkish security officer stands guard on the side of the road.
Turkish soldiers block Istanbul's Bosphorus Bridge.
In the months leading up to that July coup in Cairo, discontent was on the rise with the rule of Egypt's first democratically elected president, Mohamed Morsy. The coup was proceeded by severe fuel shortages, electricity cuts, mounting public unrest and growing suspicion that Morsy wanted to turn Egypt into an Islamic state. Morsy won the election the year before by a thin margin. His base was shaky from the start, and in the post-revolutionary atmosphere, rebellion was in the air.
The powerful Egyptian private sector feared Morsy was favoring businesspeople affiliated with the Muslim Brotherhood. One anti-Morsy tycoon even confided to me he had spent large sums to finance Tamarod (Arabic for "rebel"), the movement to overthrow Morsy. "I don't make an investment in anything unless I'm absolutely certain I'll get what I want," he said.
Privately owned television stations (owned, that is, by wealthy businessmen) agitated against and ridiculed Morsy's rule. A multitude of brutal Morsy jokes spread around the country.
The Tamarod movement, led by young activists, used social media to mobilize the masses, who on June 30, 2013, filled Tahrir Square and the roads outside the president's palace, Ittihadiya, with Egyptians demanding Morsy's ouster.
Sisi, then the defense minister, had primed the population with dire warnings of chaos if Morsy did not change his ways. When the anti-Morsy protests spread throughout Egypt, Sisi issued a 48-hour ultimatum to Morsy to meet the protesters' demands. When Morsy refused, the army stepped in and deposed him.
Power fell easily into Sisi's lap. To this day many Egyptians bristle at the suggestion that what happened in July 2013 was a coup. Less than a year later Sisi was elected President, winning with a stunning (and questionable) 93% of the vote.
Message control is nearly impossible today
Turkey's would-be putschists, on the other hand, were trying to overthrow a Turkish President who is still popular with his conservative, religious base. Erdogan won three successive elections as Prime Minister between 2002 and 2011, and now has turned the once-symbolic position of President of the republic into a commanding role.
Turkey's President Recep Tayyip Erdogan
Turkish President Recep Tayyip Erdogan addresses a crowd gathered at his palace for Iftar -- the meal eaten after sunset during the Islamic holy month of Ramadan -- in Ankara, Turkey, on Monday, June 27.
President Recep Tayyip Erdogan speaks on CNN Turk via a FaceTime call in Istanbul on Friday, July 15, after members of the country's military attempted to overthrow the government.
Erdogan, his wife Emine, his grandchildren and Istanbul Metropolitan Municipality Mayor Kadir Topbas, left, greet the crowd at a presidential election rally in Istanbul on August 3, 2014.
U.S. President Barack Obama shakes hands with Erdogan during a meeting in the Oval Office at the White House in Washington on December 7, 2009.
Erdogan oversees the rescue efforts in the eastern province of Bingol on May 1, 2003, following a 6.4 magnitude earthquake that rocked the Turkish region.
Russian President Vladimir Putin, left, and Erdogan wear helmets in Durusu village near Turkey's northern city of Samsun, on November 17, 2005, during the inauguration ceremony for the major trans-Black Sea gas pipeline Blue Stream. The pipeline runs from Russia to Turkey.
Erdogan addresses the media after a meeting with his deputies at his ruling Justice and Development Party (AK Party) headquarters in Ankara, Turkey, on May 1, 2007. Erdogan unveiled a reform package, including having future presidents elected by popular vote instead of by parliament.
Britain's Queen Elizabeth II, accompanied by Erdogan and his wife Emine Erdogan, gives a David Beckham signed soccer ball to a Turkish boy during at a garden party held for her birthday at the British Embassy in Ankara, Turkey, on May 16, 2008. It was the Queen's first visit to Turkey in 37 years.
Erdogan attends a swearing in ceremony in Ankara, Turkey, on August 28, 2014. Erdogan was sworn in as Turkey's 12th president at a ceremony in parliament, cementing his position as the country's most powerful modern leader.
Erdogan leaves a polling booth after casting his vote in Turkey's 26th general election at a polling station in Istanbul on November 1, 2015.
Romanian President Klaus Werner Iohannis, right, and Erdogan walk together during an official welcoming ceremony at the presidential complex in Ankara, Turkey, on Wednesday, March 23.
Erdogan delivers a speech during his visit to the Turkish War Colleges Command in Istanbul on Monday, March 28.
Erdogan, right, kicks a soccer ball while Former Turkish President Abdullah Gul watches at Besiktas soccer club's new Vodafone Arena on its opening day in Istanbul on Sunday, April 10.
Erdogan, right, shakes hands with King Salman of Saudi Arabia after the Saudi monarch received Turkey's highest state medal during a ceremony at the presidential complex in Ankara, Turkey, on Tuesday, April 12.
Erdogan, center, chairs the meeting of the 65th Cabinet of Turkey at the presidential complex in Ankara, Turkey, on Wednesday, May 25.
Erdogan, seventh from the left, attends the Kinaliada Corvette Ceremony at Pendik Naval Shipyard in Istanbul on Saturday, June 18.
Erdogan, right, attends an Iftar dinner during his visit to the Tank Battalion campus in the Cizre district of Sirnak, Turkey, on Saturday, June 25.
From left, U.S. President Barack Obama, France's President Francois Hollande, Erdogan and German Chancellor Angela Merkel take their positions for a photo ahead of a working dinner at the presidential palace during the NATO Summit in Warsaw, Poland, on Friday, July 8.
Turkey's President Recep Tayyip Erdogan and former Turkish president Abdullah Gul pray during the funeral of a victim of the coup attempt in Istanbul on July 17. In an interview with CNN's Beckey Anderson Erdogan said, "The people now have the idea, after so many terrorist incidents that these terrorists should be killed, that's where they are, they don't see any other outcome to it. I mean, life sentence, or aggravated life sentence... why should I keep them and feed them in prisons, for years to come, that's what the people say. So, they want a swift end to it, because people lost relatives, lost neighbours, lost children, 8-year-olds, 15-year-olds, 20-year-old young people, unfortunately have all been killed during these incidents. Of course they have parents, mothers and fathers, that are morning, they're suffering so the people are very sensitive and we have to act very sensibly and sensitively."
The faction that led the Turkish coup botched the operation almost from the beginning. It didn't take out or otherwise silence the President. It killed dozens of unarmed protesters and conducted an airstrike on the Turkish Parliament in Ankara.
These plotters had no concept about how Turkish society has changed and the changes that social media has wrought. The old coup d'etat playbook of taking over the only state-run television and radio station, broadcasting "communique No. 1" to the populace and surrounding the presidential palace with tanks just doesn't work anymore. There are dozens of private television stations in Turkey, and every citizen with a smartphone can spread messages to hundreds, who spread them to thousands, who spread it to millions. Trying to monopolize and control the message today is mission nearly impossible.
Erdogan has plenty of opponents and critics who see him as thin-skinned and increasingly authoritarian. But even the opposition parties, without exception, came out quickly in against the coup.
Having survived, Erdogan is pressing his advantage, demanding in undiplomatic terms that the United States hand over the man Turkey accuses of inspiring the coup, Fethullah Gulen, the Islamic cleric who is living in self-imposed exile in Pennsylvania.
Erdogan is strongly suggesting Turkey will reinstate the death penalty to punish those involved in the coup. He may intensity his push to transform Turkey from a parliamentary into a presidential democracy to concentrate power more fully in his hands.
It's a textbook case of how to survive a coup d'etat, and prosper.