Turkish President Recep Tayyib Erdogan is upping the ante in Libya, shipping drones, tanks, special forces and commando units to the conflict-ravaged North African country. It seems he’s bidding to create a single strategic deadlock by merging two brewing conflicts: one in Libya, and one in the Eastern Mediterranean.

As Turkey’s military incursion against the Kurds in Northern Syria loses momentum, political tensions are rising on the domestic front. To help defuse them, Erdogan is looking for a joker in the pack in the form of a foreign enemy. And now, he seems to have chosen one.

On Sunday, Erdogan announced that Turkey is ready to send troops to Libya to prop up the UN-recognised government against a military offensive by general Khalifa Haftar that started last week.

But by acting out of his growing sense of insecurity and isolation, Erdogan hasn’t made just one overseas adversary; he’s infuriated most of his neighbours too.

Haftar’s fresh campaign was triggered by a new security deal signed by Ankara and the Tripoli government. The two sides have drawn up a memorandum of understanding to secure drilling rights in the Mediterranean.

Over the last five years, the Eastern Mediterranean has witnessed major discoveries of gas fields in Greece, Cyprus and Israel. But it was the discovery of the Egyptian Zohr field, the biggest gas field in the Mediterranean, that was seen as a game changer, paving the way for Egypt to be the region’s energy hub.

The Eastern Mediterranean Gas Forum was duly formed – and while it includes most of the region’s states in its membership, Turkey is excluded. For Erdogan, this was a move to stack the cards against his country, and he felt eager to retaliate.

The new agreement sets out a peculiar process for mapping out maritime boundaries between Turkey and Libya, which are neither neighbors nor have boundaries to share. The lines now drawn encroach on the economic zone of the Greek island of Crete, as well as violating the Sukhairat Agreement that shores up the Tripoli government’s legitimacy.

In response to Turkey’s latest move to trample over other countries’ rights in the Mediterranean, Greece has filed a complaint to the Security Council, while Egypt has carried out live-fire military manoeuvres near its Mediterranean coast. And at their summit last week, the EU Leaders declared the agreement a violation of international law.

Turkey hardly seems inclined to back down. After Erdogan pledged to send his troops to Libya, Turkish-backed northern Cyprus received drones to help Turkey’s gas excavation projects near the island.

But establishing a military base in Libya could prove to be Erdogan’s biggest strategic blunder. In particular, it goes against Egypt and the UAE, who have for years backed Haftar in his efforts to capture Tripoli.

All the while, Turkey’s old man is going through his biggest domestic political struggle yet.

Ahmet Davutoglu, a former prime minister and former ally to the president, launched last week a new political party, which many in Erdogan’s ruling Justice and Development Party (AKP) view as a direct threat. And now, former deputy prime minister and ex-heavyweight Erdogan ally Ali Babacan is expected to announce his own breakaway party soon.

Meanwhile the opposition mayor of Istanbul, Ekrem Imamoglu, has seen his popularity skyrocket. Many in Turkey believe that Abdullah Gul, the former president and founder of the AKP, might step in soon to help turn the tide against the president.

When the Turkish military invaded northern Syria, Erdogan cleverly deployed nationalistic rhetoric to win domestic support for his shady business in the neighbouring country. But he should be under no illusion that his people will be easily sold on this latest venture. He is risking the deaths of possibly hundreds of Turkish soldiers to defend an Islamist-backed government thousands of miles away. It’s hard to see the appeal.

Yet for now, Erdogan seems content to put Turkey’s turbulent politics out of his mind. For him, the lack of domestic legitimacy for his ambitions in Libya can always be subject to a trade-off with another kind of legitimacy in the west.

Reports have recently surfaced that Russia has increased its military support for Haftar’s National Army, which controls most of Libya’s strategic oil fields in Libya. President Vladimir Putin’s audacious move dropped the jaws of Trump’s administration.

Erdogan is playing on this very American fear of the heavy Russian military presence in North Africa. The Americans know that Russia is trying to copy president Trump’s strategy of keeping American troops in Syria in order to maintain control over the oil fields, this time in Libya.

Erdogan also understands that Haftar’s assault on Tripoli doesn’t have the West’s support. For that, he is hoping to present himself as a reliable, legitimate and a western-backed counterbalance.

Erdogan may not be bluffing about deploying troops to Tripoli and flexing his muscles against Cyprus, but upping the stakes in the backyard of such powerful players like the EU, Egypt and Israel is a risky business.

Turkey seems wounded and desperate. Erdogan is aware of the risks of punching above his country’s weight, yet he seems ready to risk a war that Turkey might lose. Egypt and Greece must not show any sign of weakness. This is the time to hold their nerve – and not to get dragged into what could become a major conflict.