Forced into battle against Islamic State as it presses on Turkey's borders, President Tayyip Erdogan is seizing the chance to keep another foe in check, bombing Kurdish militants he sees as a threat to the integrity of the Turkish state.
Casting the operations as a war on terrorist groups "without distinction", Turkey launched air strikes against Islamic State in Syria for the first time last week and granted the U.S.-led coalition access to its air bases after years of reluctance.
It also bombed camps in northern Iraq belonging to the militant Kurdistan Workers Party (PKK) for the first time in at least three years. Hundreds of suspected Islamic State and PKK members have been rounded up in raids across Turkey.
Launching wars on two fronts is a high-risk strategy for the NATO member, leaving it dangerously exposed to the threat of reprisals by jihadists and at risk of reigniting a Kurdish insurgency that has cost 40,000 lives over three decades.
Turkey has been a conduit for foreign jihadists, with thousands thought to have crossed its borders to join Islamic State in Syria and Iraq, many concealed among the millions of tourists who flock to Turkey's shores each year.
They have often been aided by Turkish smugglers linked to the Islamist insurgents; a network Turkey has been trying to dismantle but which could retain capacity to launch attacks on Turkish soil after the fashion of last week's suicide bombing, blamed by Ankara on the militants, that killed 32 people.
Western diplomats have long feared that Istanbul, one of the world's most visited cities, or Turkey's Aegean or Mediterranean coastal resorts could be soft targets. Attacks that killed dozens of foreign tourists in Tunisia earlier this year served only as a reminder of the risks.
"Ankara's recent adoption of aggressive policies towards both the PKK and the Islamic State has considerably raised the risk of terrorist attacks and sustained civil unrest inside the country," Wolfango Piccoli of risk research firm Teneo Intelligence said in a note.
Yet on both fronts, Erdogan looks to be hoping to seize opportunity out of crisis. He is reviving Turkey's international standing with the more robust stance on Islamic State, but also undermining the pro-Kurdish opposition and bolstering nationalist support at home with the attacks on the PKK.
Smarting from an election setback in June, when the AK Party he founded lost its majority and the pro-Kurdish opposition HDP secured enough votes to enter parliament for the first time, Erdogan is keen to win back nationalist support.
"The likely target here is instead the HDP. By striking hard at the PKK, the Turkish government is pressuring the HDP to pick a side," said Erik Meyersson, an assistant professor at the Stockholm School of Economics, in an article on his website.
"Either it denounces PKK to end violence, risking political blowback among its Kurdish base, or it adopts a more pro-Kurdish rhetoric, risking the ire of the Turkish public as well as the judiciary, which has a long history of banning Kurdish parties and politicians."
A collapse of the Kurdish vote and fears over security could, in the event of an early election, revive the AKP vote and with it Erdogan's ambition to change the constitution, investing his presidency with broad new powers.
The decision to actively join the campaign against the Islamist militants flowed directly from the suspected Islamic State suicide bombing that killed 32 in the southeastern town of Suruc. The attack led to days of violent backlash from Kurds, who accuse Erdogan and the Islamist-rooted AKP of covertly supporting Islamic State against Syrian Kurds.
Ankara denies this. It is, however, uncomfortable with the steady advance of Syrian Kurdish PYD forces, helped by U.S. air strikes, against Islamic State. Around half of Syria's 900 km (560 mile) border with Turkey is now controlled by Kurds.
Erdogan and the AKP worry that those advances will embolden Turkey's own 14 million Kurdish minority and rekindle a three-decade insurgency by the PKK, deemed a terrorist organisation by Turkey, the United States and Europe.
"The dynamic in Syria was going against Turkish interests," said Sinan Ulgen, visiting scholar at Carnegie Europe and chairman of the Istanbul-based EDAM think-tank. "First, Islamic State was expanding northwest and continuing to take ground along the border.
"Secondly, there was the fear that the expansion of the PYD could ultimately establish a Kurdish territory stretching from Iraq to the Mediterranean. These two dynamics forced Turkey's hand," he told Reuters.
LEVERAGE IN WASHINGTON
Ankara also realised that the PYD, in proving a reliable partner on the ground for U.S.-led air strikes, was "gaining an undue degree of leverage in Washington", Ulgen said, prompting Turkey to realise that it needed to step up and move against Islamic State if it was to avoid being sidelined.
"It has been very costly both security-wise and in terms of the national interest for Turkey to prolong this delay in fully engaging with the anti-Islamic State coalition," he said.
"Had Turkey been able to position itself here a year ago, things would be very different."
Ankara's decision to act was also partly a result of a realisation that its efforts to win U.S. support for a "buffer zone" in northern Syria were not gaining traction, said Yusuf Muftuoglu of Macro Advisory Partners, a London-based political risk consultancy.
Turkey wants the safe zones on the Syrian side of the border to keep Islamic State and Kurdish militants away from its soil, and to help prevent a further influx of refugees on top of the 1.8 million which it is now sheltering.
Soon after the strikes started, Foreign Minister Mevlut Cavusoglu raised the issue again, telling a news conference on Saturday that "safe zones will be formed naturally" as swathes of northern Syria are cleared of Islamic State militants.
What Turkey does appear to have won from Washington, however, is tacit support for its campaign against the PKK esconsed in the mountains of northern Iraq, even as the U.S.-led coalition works alongside Kurds in Syria.
"There is no connection between these air strikes against PKK and recent understandings to intensify U.S.-Turkey cooperation against ISIL," Brett McGurk, the deputy special presidential envoy for the coalition to counter Islamic State, said on Twitter, using one of the jihadist group's acronyms.
But he did not criticise Turkey's air strikes in Iraq.
"We fully respect our ally Turkey's right to self-defence," he wrote.