Few it seems saw it coming. To that end Turkey’s coup plotters certainly had the element of surprise working in their favour.

As the tanks and troops rolled into major cities across the country, locking down roads and government installations, their goal was nothing less than the toppling of President Recep Tayyip Erdogan, and launching the fourth successful military coup in Turkey’s history.

“They bombed places I had departed right after I was gone,” Erdogan said late on Friday night speaking from the resort town of Marmaris where he had been on holiday. “They probably thought we were still there.”

Erdogan however was already en-route to Istanbul in an effort to rally his supporters and head off the attempts to overthrow him and his government.

By early yesterday the coup had all but been crushed and Erdogan was in altogether different mood.

“They will pay a heavy price for this,” he said. “This uprising is a gift from God to us because this will be a reason to cleanse our army.”

Had the coup been successful it would have marked one of the biggest shifts in the Middle East in years. As it is, however, Turkey is still set on a very uncertain and unstable path - not least because this coup looks unlike any other the nation has seen before. Previous coups saw a secular military moving to stop any shift towards Islamism, this coup, however, looks more likely to have been carried out by an Islamist faction of the army.

The coup began with warplanes and helicopters roaring over Ankara and troops moving in to seal off the bridges over the Bosphorus Strait that links Europe and Asia in Istanbul. It was an operation that displayed a high degree of organisation and efficiency.

Shortly after its start, a faction of the army released a statement saying that a “peace council” was running the country, and there would be a curfew and martial law. The group said it had launched the coup “to ensure and restore constitutional order, democracy, human rights and freedoms”.

Broadcaster CNN Turk was temporarily taken off air after soldiers entered the building and tried to take it over. This was in response to a broadcast by Erdogan who bizarrely was reduced to speaking via FaceTime on a news presenter’s mobile phone.

“I am calling on our nation. Go to the squares, let us give them the best answer,” the president rallied.

By the early hours of yesterday many of the country’s politicians were hiding in shelters inside the parliament building, which was being fired on by tanks, the smoke from shellfire billowing into the night sky.

Gruesome footage that was broadcast showed corpses dismembered and blown apart by tank shells, and the parliament was left with charred walls and smashed windows.

A warplane reportedly attacking the parliament in Ankara was shot down as was a helicopter carrying pro-coup soldiers.

In central Istanbul’s Taksim Square there were fierce clashes with gunfire and explosions. Pro-coup demonstrators stood around a statue of Turkey’s secular founder, Kemal Ataturk, as they chanted calls for Erdogan’s resignation.

“Turkey has been polarised and brought to the brink of war by one man, Erdogan,” said Halil Aktas, a protester who had taken to the square. “This will not continue a single day more.”

As protesters called for the government’s ousting in Taksim, hundreds of Erdogan supporters took to the streets of the nearby district of Kasimpasa, the president’s blue-collar birthplace.

“Erdogan is the honour of Turkey,” shouted protesters near a military checkpoint. “Revenge. We will take our revenge,” they warned.

In a matter of hours these pro-government supporters were to prevail - and revenge, some of it brutal, was being meted out on pro-coup troops, with reports that one was beheaded.

In the end, though, the coup was brought to a halt by an unholy alliance of Erdogan supporters with the help of others who far from being allies of the president simply didn’t want to see Turkey once again under military rule.

Yesterday as the last skirmishes died down, Turkish police and security services embarked on a series of mass arrests with some 2,839 soldiers, including high-ranking officers being detained.

Turkey's PM Binali Yildirim in a night he called a “black stain on Turkish democracy,” said 161 people had been killed and 1,440 wounded.

There were reports also that a Turkish military helicopter had landed in northern Greece carrying eight men who requested political asylum.

The identity of these men might help provide some answer to the pressing question now being asked across Turkey and beyond: Who was behind the coup?

If exactly who launched the attempted takeover remains unclear, then the reasons as to why it happened are perhaps more apparent.

As Erdogan and his Islamist AKP party has won election after election, he has dropped any pretence of governing for all Turks.

“I will raise a religious generation,” he declared, turning his back on the secularism that Kemal Ataturk, the founder of the Turkish republic, imposed on the country more than 90 years ago.

During his own time in office Erdogan has purged institutions in the state and instituted changes within them to strengthen his hold on power.

His loyalists have been moved into key positions in Turkey's intelligence agency, police, justice system, education system and the army.

He has harassed the media, trying to take it over and marginalised business leaders who he saw as hostile to his rule.

For most of this year the debate over whether the country’s constitution should enshrine secularism or whether it should be an Islamist one has intensified.

The current constitution made under military rule in 1982, holds up the principle of secularism in Article 2. To that end the army continues to see itself as the defender of secularism.

In turn Erdogan’s opponents have again taken issue with some of his recent moves that they see aimed at constitutionally boosting his presidential powers.

They cite too other signs that Erdogan is implementing measures that are effectively Islamist policies. His railing against abortion as a violation of God’s will and his efforts to impose alcohol-free zones, criminalise adultery and insistence that women stay at home and have three children are all, opponents say, proof that Erdogan wants to move towards an Islamist state.

Adding to these pressure points of course is the contagion of violence from the war in Syria that has also impacted dramatically on Turkey.

There are indications too that the timing of the coup could be related to a yearly summit that Turkey’s military holds, which determines promotions within the top ranks of the armed forces.

In 2011, the entire top brass of the Turkish military resigned over anger at the arrest of senior officers who were accused of plotting a coup.

This year’s summit was supposed to be held on August 1 and some observers have speculated that factions within the military who feared they would be sidelined then decided to pre-empt that development and carry out their own coup

Whoever it was behind the putsch they presumably believed they were saving Turkey from an increasingly out-of-touch and ideological leadership.

Given such motives this once again begs the question as to who precisely was behind the attempted overthrow?

On this Erdogan himself blamed a “parallel structure,” a clear reference to followers of Fethullah Gulen, a powerful but reclusive US based Muslim cleric who he accuses of fomenting unrest.

In a statement Gulen who has been called Turkey’s second most powerful man rejected any suggestion he had links to the coup events, saying he condemned it “in the strongest terms”.

Gulen himself lives in reclusive exile in Pennsylvania, from where he leads a popular movement called Hizmet that at times has appeared cultish. According to some reports, 10 per cent of the Turkish population is estimated to support Hizmet.

From its ranks it has spawned think tanks, businesses, schools and publications across the globe, while building up substantial wealth and influence in the process.

Many regional observers remain convinced of Gulen’s influence or involvement in the latest challenge to Erdogan claiming that it was his followers embedded within the military that spearheaded the coup.

Since the 1970’s the Gulenists, an Islamist movement has built up significant influence in Turkey.

Starting with the gendarmerie, where they could take advantage of lax background checks, the movement’s activists gradually worked their way up the military chain of command.

When Erdogan felt that the Gulen movement had become too powerful, relations started to fray between his ruling AKP party and the Gulenists.

By 2014 massive purges were underway to erode Gulenist influence in the media and government but were never fully effective in the military.

According to the independent US based intelligence and geopolitical monitoring group Stratfor, this may have been because of “the large amount of blackmail that the Gulenists retained on major military figures to prevent their own dismissals”.

Stratfor also points out that many Gulenist sympathisers exist within Turkey’s air force's ranks, which in part might account for the deployment of some fighter aircraft and helicopters during the coup’s execution.

In effect the Gulenists, an Islamist faction within the military that has deeply alienated the secular strongmen within the armed forces, looks to have been the leader of the challenge against Erdogan.

This would make the latest coup attempt unlike those in the past, not one backed by Turkey’s secular political, military and civilian opposition. Evidence for this is born out by the fact that as yesterday’s counter coup got underway it was led by a number of military commanders and the national police, while the main secular opposition Republican People’s Party leader also came out against the coup.

As the putsch unfolded on Friday night, a lawyer for the Turkish government, Robert Amsterdam, said there were “indications of direct involvement” of the Gulenists.

Amsterdam said that he and his firm had “attempted repeatedly to warn the US government of the threat posed” by Gulen and his movement.

He cited Turkish intelligence sources in claiming that “there are signs that Gulen is working closely with certain members of military leadership against the elected civilian government”.

Yesterday, as Erdogan’s position in Turkey became increasingly secure his own denunciation of the Gulenists became more pronounced.

“A minority within the Armed Forces has unfortunately been unable to stomach Turkey’s unity. It was the Gulen Movement itself. This group has penetrated the armed forces and the police among other government agencies over the past 40 years,” Erdogan insisted.

In response the president ordered an immediate clampdown on all those connected with the Gulenists.

Yesterday the government pressed ahead with a purge of judicial officials, with 2,745 judges being dismissed across Turkey for alleged ties to Gulen. Ten members of Turkey's highest administrative court were detained and arrest warrants were issued for 48 administrative court members and 140 members of Turkey's appeals court, state media reported.

As events continue to unfold this weekend, the world looks on nervously. Not least the United States and other western nations who view Turkey as a key bulwark in the fight against the jihadists of the Islamic State (IS) group in neighbouring Syria and Iraq.

In Washington President Barack Obama urged all parties in Turkey to support the “democratically elected government" while European Council President Donald Tusk said the country was “a key partner for the European Union” and called for a “swift return to Turkey’s constitutional order.

Despite the fact that the US and most members of Nato, to which Turkey belongs, condemned the coup and voiced support for Erdogan and the elected government, there is no doubt that there is increasing concern among them about instability in the country.

For the moment it is a given that with the crushing of the coup attempt, Erdogan will immediately increase his efforts to strengthen his hold on power and oppress his opponents. President Erdogan might live to fight again another day but Turkey it far from out of the political woods.

A military coup has been prevented and a democratically elected government survives, but democracy and its underlying principles will be faced with new challenges under Erdogan. Turkey’s people still face difficult and dangerous times ahead.