Coup attempt and purge are tearing Turkey apart. The Turkish armed forces, for long the backbone of the state, are in a state of turmoil. Some 40 per cent of its generals and admirals have been detained or dismissed, including senior army commanders.

They are suspected of launching the abortive military takeover on 15-16 July, which left at least 246 people dead, saw parliament and various security headquarters bombed and a near successful bid to kill or capture President Recep Tayyip Erdogan.

In response, Erdogan and his government are carrying out a purge of everybody from soldiers to teachers connected in any way to the movement of the US-based cleric Fethullah Gulen accused of organising the coup attempt.

Among media outlets closed in the past few days are 45 newspapers, 16 TV channels – including a children’s channel – and 23 radio stations. People fearful of being implicated in the plot have been hurriedly disposing of Gulenist books and papers by burning them, throwing it into rivers or stuffing them into rubbish bins.

Five years ago, Turkey looked like the most stable and successful country in the Middle East – an example that its neighbours might like to follow. But, instead of Iraq and Syria becoming more like Turkey, it has become more like them in terms of political, ethnic and sectarian division.

Erdogan’s personal authority is being enhanced by his bravery and vigour in defeating the coup attempt and by the removal of remaining obstacles to his rule. But the failed putsch was also a sign that Turkey – a nation of 80 million people with an army 600,000-strong – is becoming weaker and more unstable.

Its leaders will be absorbed in the immediate future in conducting an internal purge and deciding who is loyal and who is not. While this is going on, the country faces pressures on many fronts, notably the war with Kurdish guerrillas in the south east, terror attacks by the Islamic State and diplomatic isolation stemming from disastrous Turkish involvement in the war in Syria.

In pictures: Turkey coup attempt Show all 17 1 /17 In pictures: Turkey coup attempt In pictures: Turkey coup attempt Turkish President Erdogan attends the funeral service for victims of the thwarted coup in Istanbul at Fatih mosque on July 17, 2016 in Istanbul, Turkey Burak Kara/Getty Images In pictures: Turkey coup attempt Soldiers involved in the coup attempt surrender on Bosphorus bridge with their hands raised in Istanbul on 16 July, 2016 Gokhan Tan/Getty In pictures: Turkey coup attempt A civilian beats a soldier after troops involved in the coup surrendered on the Bosphorus Bridge in Istanbul, Turkey, 16 July, 2016 REUTERS/Murad Sezer In pictures: Turkey coup attempt Surrendered Turkish soldiers who were involved in the coup are beaten by a civilian Reuters In pictures: Turkey coup attempt Soliders involved in the coup attempt surrender on Bosphorus bridge Getty In pictures: Turkey coup attempt Supporters of Turkish President Recep Tayyip Erdogan wave flags as they capture a Turkish Army vehicle Getty In pictures: Turkey coup attempt People pose near a tank after troops involved in the coup surrendered on the Bosphorus Bridge in Istanbul, Turkey, 16 July, 2016 Reuters In pictures: Turkey coup attempt Turkish soldiers block Istanbul's Bosphorus Brigde Getty In pictures: Turkey coup attempt A Turkish military stands guard near the Taksim Square in Istanbul Reuters In pictures: Turkey coup attempt Pierre Crom/Twitter In pictures: Turkey coup attempt Turkish soldiers secure the area as supporters of Recep Tayyip Erdogan protest in Istanbul's Taksim square AP In pictures: Turkey coup attempt People demonstrate in front of the Republic Monument at the Taksim Square in Istanbul Murad Sezer/Reuters In pictures: Turkey coup attempt Turkish soldiers detain police officers during a security shutdown of the Bosphorus Bridge Reuters In pictures: Turkey coup attempt Turkish Army armoured personnel carriers in the main streets of Istanbul Getty In pictures: Turkey coup attempt Chaos reigned in Istanbul as tanks drove through the streets EPA/TOLGA BOZOGLU In pictures: Turkey coup attempt Turkish President Tayyip Erdogan speaks to media in the resort town of Marmaris Reuters In pictures: Turkey coup attempt Supporters of President Erdogan celebrate in Ankara following the suppression of the attempted coup Reuters

The destabilisation of Turkey is good news for Isis because Turkish security organisations, never very assiduous in pursuing salafi-jihadi rebels, will be devoting most of their efforts to hunting down Gulenists. Both Isis and other al-Qaeda-type movements like al-Nusra Front will benefit from the anti-American atmosphere in Turkey, where most believe that the US supported the coup attempt.

The Turkish armed forces used to be seen as a guarantee of Turkey’s stability, inside and outside the country. But the failed coup saw it break apart in a manner that will be very difficult to reverse. No less than 149 out of a total of 358 generals and admirals have been detained or dishonourably discharged. Those arrested include the army commander who was fighting the Kurdish insurrection in south east Turkey and the former chief of staff of the air force.

Many Turks have taken time to wake up to the seriousness of what has happened. But it is becoming clear that the attempted putsch was not just the work of a small clique of dissatisfied officers inside the armed forces; it was rather the product of a vast conspiracy to take over the Turkish state that was decades in the making and might well have succeeded.

At the height of the uprising, the plotters had captured the army chief of staff and the commanders of land, sea and air forces.They were able to do so through the connivance of guards, private secretaries and aides who occupied crucial posts.

The interior minister complains that he knew nothing about the coup bid until a very late stage because the intelligence arm reporting to him was manned by coup supporters. Erdogan gave a near comical account of how the first inkling he had that anything was amiss came between 4pm and 4.30pm on the day of the coup attempt from his brother-in-law, who had seen soldiers blocking off streets in Istanbul. He then spent four hours vainly trying to contact the head of the national intelligence agency, the chief of staff and the prime minister, none of whom could be found. Erdogan apparently escaped from his holiday hotel on the Aegean with 45 minutes to spare before the arrival of an elite squad of soldiers with orders to seize or kill him.

There is little question left that the followers of Fethullah Gulen were behind the coup attempt, despite his repeated denials. “I don’t have any doubt that the brain and backbone of the coup were the Gulenists,” says Kadri Gursel, usually a critic of the government. He adds that he is astonished by the degree to which the Gulenists were able to infiltrate and subvert the armed forces, judiciary and civil service. The closest analogy to recent events, he says, is in the famous 1950s film Invasion of the Body Snatchers, in which aliens take over an American town without anybody noticing until it is almost too late.

Turkey shakes up security forces after coup attempt

The coup attempt was so unexpected and unprecedented that Turkey today is full of people asking questions about their future, and that of their country – questions to which there are no clear answers.

Will Erdogan exploit the opportunity offered by the failed coup to demonise all opponents and not just Gulenists as terrorists? Some 15,000 people have been detained of whom 10,000 are soldiers. The presidential guard has been stood down. One third of the judiciary has been sacked. So far most of the journalists and media outlets targeted have some connection with the Gulenists, but few believe that the clamp down on dissent will end there.

“Erdogan’s lust for power is too great for him show restraint in stifling opposition in general,” predicts one intellectual in Istanbul who, like many interviewed for this article, did not want his name published. When one small circulation satirical magazine published a cartoon mildly critical of the government last week, police went from shop-to-shop confiscating copies.

For the moment, Erdogan is benefiting from a degree of national solidarity against the conspirators. Many Turks (and not just his supporters) criticise foreign governments and media for making only a token condemnations of the coup attempt before demanding restraint in conduct of the purge. They point out that, if the coup had more successful, Turkey would have faced a full-blown military dictatorship or a civil war, or both. Erdogan said in an interview that foreign leaders who now counsel moderation would have danced for joy if he had been killed by the conspirators.

Sabiha Senyucel, the research director of the Public Policy and Democracy Studies think tank in Istanbul, says that the evening of the coup attempt “was the worst evening of my life”. She complains that foreign commentators did not take on board that “this was a battle between a democratically elected government and a military coup”.

She has co-authored a report citing biased foreign reporting hostile to Erdogan and only mildly critical of the coup-makers. She quotes a tweet from an MSNBC reporter at the height of the coup attempt, saying that “a US military source tells NBC News that Erdogan, refused landing rights in Istanbul, is reported to be seeking asylum in Germany”.

Turkey is deeply divided between those who adore and those who hate Erdogan. Senyucel says that “there are two parts of society that live side by side but have no contact with each other”.

But, even so, it is difficult to find anybody on the left or right who does not suspect that at some level the US was complicit in the coup attempt. Erdogan is probably convinced of this himself, despite US denials, and this will shape his foreign policy in future.

“The lip-service support Erdogan got from Western states during and immediately after the coup attempt shows his international isolation,” said one observer. The Turkish leader is off to see Vladimir Putin on 9 August, though it is doubtful if an alliance with Russia and Iran is really an alternative to Turkey’s long-standing membership of Nato.

Erdogan can claim that the alternative to him is a bloody-minded collection of brigadier generals who showed no restraint in killing civilians and bombing parliament. But the strength and reputation of the Turkish state is being damaged by revelations about the degree to which it has been systematically colonised since the 1980s by members of a secret society.

Gulenist candidates for jobs in the Foreign Ministry were supplied with the answers to questions before they took exams, regardless of their abilities. The diplomatic service – once highly regarded internationally – received an influx of monoglot Turkish-speaking diplomats, according to the Foreign Minister. “The state is collapsing,” says one commentator – but adds that much will depend on what Erdogan will do next.

In the past he has shown a pragmatic as well as a Messianic strain, accompanied by an unceasing appetite for political combat and more power. His meeting last week with other party leaders, with the notable exception of the Kurds, may be a sign that he will be forced to ally himself with the secularists. He will need to replace the ousted Gulenist officers in the armed forces and many of these will secularist victims of past purges by the Gulenists.

Turkey is paying a heavy price for Erdogan’s past alliances and misalliances. Many chickens are coming home to roost.

The Gulenists were able to penetrate the armed forces and state institution so easily because between 2002 and 2013 they were closely allied him and his ruling Justice and Development Party (AKP) in opposition to the secularists. Isis has been able to set up a network of cells in Turkey because, until recently, the Turkish security forces turned a blind eye to salafi-jihadis using Turkey as a rear base for the war in Syria. Erdogan arguably resumed confrontation and war with the Kurdistan Workers Party (PKK) as an electoral ploy to garner nationalist support after his failure to win the general election on 7 June last year.