The only person who knows the answers is Erdogan, and he's not saying. But you can work it out if you try.
Erdogan has spent more than a decade subverting a secular and democratic system and establishing his own unchallengeable power. At first he was responding to real popular demands for equal civil rights for religious people and for an improvement in living standards. He delivered on his promises, and won three successive elections by increasing majorities.
But he reduced the once-free mass media to subservience, undermined the independence of the judiciary, and staged show trials of his opponents. He also allowed his own political associates to engage in massive corruption.
As his power grew, moreover, he began to indulge his obsessions. He is a deeply conservative Sunni Muslim who shares the widespread Sunni belief that Shia Muslims are not just heretics, but heretics whose power is a growing threat.
From the start of the Syrian civil war in 2011, therefore, Erdogan supported the Sunni rebels against the regime of Bashar al Assad, which is dominated by the country's Alawite (Shia) minority - and he didn't much mind if the Sunni rebels were head-cutting extremists like Islamic State or not. That's why the Turkish-Syrian border stayed open, and the coalition didn't get access to Turkish airbases.
At the same time, Erdogan opened peace negotiations with the PKK, because conservative Kurds who voted for his party on religious grounds were an important part of his electoral base. But then his party lost its majority in parliament in last month's election (7 June).
What cost him his majority was the new People's Democratic Party (HDP), which seduced most of his Kurdish voters away. It's liberal, pluralistic, all the things that Erdogan isn't. But conservative Kurds had already got the religious freedoms they wanted, and the HDP was also advocating equal political rights for the Kurdish minority. Of course they switched their votes.
So now, if Erdogan wants to form a coalition government (or even win a new election), he needs the support of the hard right - but they are ultra-nationalists who loathe his willingness to make deals with the Kurds. To win them over, therefore, he has started bombing the PKK.
He might be re-starting a Turkish-Kurdish civil war (the last one killed 40,000 people), but that's a risk he's willing to take. And on the side he has dropped a few bombs on Islamic State to make the Americans happy.
Erdogan's problem with Washington was that it finally had the goods on him. A US Special Forces raid in Syria last May killed Abu Sayyaf, the IS official in charge of selling black-market oil from IS-controlled wells into Turkey. The American troops came away with hundreds of flash drives and documents that proved that Turkish officials were deeply involved in the trade, which has been IS's main source of revenue.
Turkey has now bombed a few IS targets to show willing - but if you look at the videos, the Turkish planes are launching missiles at single buildings out in open fields, not exactly where you'd expect IS to have weapons stores and command centres. It's as if the Turkish forces were ordered to hit targets that wouldn't do any real damage. But least the coalition gets to use Incirlik.
Is Erdogan still in cahoots with IS? Maybe. Is he actively supporting the other big Islamist group, the Nusra Front, which dominates the battle in western Syria? Yes he is, quite openly, and the difference between these two terrorist groups is only skin-deep. So if you're expecting a radical change in the military situation in Syria - don't. Assad is still losing slowly, the Islamist extremists are still winning, and Turkey is still playing a double game.
Gwynne Dyer is an independent journalist whose articles are published in 45 countries.