On a baking hot Monday night, in a landscape dotted with churches and half-timbered medieval towns, a young foreign man — welcomed last year to live in Bavaria — launched a terror attack, trying to behead passengers on a train as he shouted the Islamic phrase ‘Allahu akbar’ (God is great).

It was the first time that a migrant in Germany has carried out such a crime in the name of Islam, and it has left the country feeling very jittery indeed.

After hiding in a lavatory on the commuter train, Riaz Khan jumped out, hacking indiscriminately with an axe and a knife at passengers. He maimed four tourists from Hong Kong, leaving two close to death as their skulls and bodies were smashed to pieces.

Riaz Khan Ahmadzai, who attacked people on a train in Germany earlier this week

As Erik, a 25-year-old paramedic and one of the first to arrive at the scene from the medical centre in the nearby town of Ochsenfurt, told me afterwards: ‘When I climbed into the train, I could see blood everywhere. People lay on the floor. They had gaping holes in their heads, their chests, their stomachs. They are the worst injuries I have ever seen.

‘I began tending to one woman and I saved her life.

‘The police kept telling us to be careful because the attacker might have an accomplice — a brother or cousin.’

By then, Khan had fled from the train through the garden of a nearby house and onto a street. There, he spotted a woman walking her dog.

Shouting at her: ‘I am going to f*** you, b****,’ he attacked her before running to a nearby river and then threatening police with an axe. Eventually, he was cornered and shot dead.

As Erik added revealingly: ‘That day, five hours before, my colleagues had a meeting about security arrangements at the famous music festival in the town due to be held next month. Someone asked: “What happens if there is a terrorist attack?”

‘We all laughed and said: “What here, in this town? That’s impossible.”

‘But now we know differently.’

For the country has become sharply divided over mass migration — 75 per cent expected a further Islamist attack post the train axe horror — and this situation may have produced a terrible kickback.

The Germans have no true idea of the type of people who were allowed into their country as a result of their leader Angela Merkel’s decision to open Germany’s doors wide during the biggest migration crisis in Europe since World War II.

Certainly, the German Chancellor’s highly controversial move provoked a headlong rush, not only from terror-ridden Syria but from the rest of the troubled Middle East, Africa, South Asia and the Balkans, too.

At the time, critics warned — presciently — that Merkel’s eagerness to welcome migrants could wreck the European project.

Germans were reluctant to absorb so many and the move triggered the rise of the anti‑immigrant Alternative for Germany (AfD) party.

Many migrants to Germany, pretending to be Syrian refugees fleeing for their lives, turned up with no documents and succeeded in slipping into the country because officials were overwhelmed by the sheer numbers at the border.

Thousands, including Syrians, lied about their age, saying they were under 18 and without relatives. It was a ruse to get themselves at the front of the queue to claim asylum. At one stage, ten ‘unaccompanied minors’ per hour were entering Germany and the rest of Europe.

But one thing is sure: the vast majority of arrivals were from countries with links to Islamic State (IS), which peddles a hatred of the West and its lifestyle.

German Chancellor Angela Merkel arrives for the last cabinet meeting prior to her summer vacations at the chancellery in Berlin, Germany, this week

This prompted Hans-Georg Maassen, head of Germany’s domestic intelligence agency, to warn that IS had deliberately planted jihadists among the migrants flowing into Europe. ‘The terror risk is very high,’ he said in February.

A few weeks later, he added: ‘I am concerned about the high number of migrants whose identities we don’t know because they had no official documents when they entered the country.’

So is Merkel’s grand, liberal gesture being exposed as damaging to her people?

Certainly, in Ochsenfurt, where train attacker Riaz Khan lived in a 36-bed migrant hostel until two weeks ago, many residents are asking why their leader was so foolish as to let someone like him settle among them.

For quickly after the attack, it turned out that Khan was a fraud. He said he was 17, but investigators believe he was much older. And though he claimed to be an Afghan refugee fleeing violence, it is thought he was a Pakistani economic migrant.

But in the overwhelmed German migration system, his asylum application was not processed, which could have established his identity.

He was simply allowed in — and given sanctuary — with few checks made, Bavarian officials have admitted. He had moved from the hostel to live with Roman Catholic foster parents at their detached home in a village near Ochsenfurt earlier this month.

There, police have discovered a hand-painted Islamic State flag and a note in an exercise book saying: ‘Pray for me that I can take revenge on these infidels and go to paradise.’

A Pakistani document was found that gave advice on areas to head for in Germany — once he had crossed the border — which were accepting more migrants than others.

And a home video showed him brandishing a knife and boasting he was an IS soldier preparing for a suicide mission.

Crucially, it gives clues to his true origins. When speaking of Syria, Khan used ‘Sham’ — a word used in Pakistan, not Afghanistan. He chose the term ‘Fauj’ for Army, which is common in Pakistan, but unheard of in Afghanistan.

So why did he bite the generous German hand that fed him?

Khan was about to start an apprenticeship at a baker’s in the Ochsenfurt area. His upkeep and pocket money were paid for by the German state.

Before he moved to the foster family, he was one of 250 migrants living in three hostels in the town of 11,000 people, which has done its best to welcome the newcomers. A fourth hostel is due to open next month. Sebastian, a 41-year-old bookbinder who lives in Ochsenfurt, told me the town has a long history of accepting refugees. There is a Syrian Orthodox chapel in the town catering for migrants who arrived 20 years ago.

But the train attack has altered the local mood. Sebastian explained: ‘We have tried to do our best for people like Khan. But already the attitude to migrants is changing.

‘Now we know that atrocities can happen not just in other parts of Europe, but in Germany, too. It is sad for our town, but who can blame the residents for feeling this new way?’

As we sit talking at a bar where locals gather not far from Ochsenfurt centre, he introduces me to a 34-year-old teacher who works at a migrant hostel in another town a few miles away. The man won’t give his name, but says he wants the truth to come out.

‘A lot of the migrant kids have no identity papers. They say they are aged 14 or 15, but it isn’t true. They are older, with full beards, but pretend to be teenagers.

‘When we workers get suspicious, we tell the people who run the hostel. But they order us to keep quiet because each “child” is worth money to them, paid by the German government to look after unaccompanied minors.

‘It makes you feel a fool. Among Germans there is a huge sense of disappointment about what is happening. We feel we are being taken for a ride by the migrants, and the migration system is making that worse.

‘Of course, not all migrants are bad. But some are here under false pretences and could be dangerous.’

At his hostel there are 15 young migrants, three of them Syrian and the rest Afghan or Somalian. The Germany state pays ¤120 (£100) a day for them to be fed, housed and educated.

‘The Syrians are well-behaved: they learn the language and are focused. But some grown-up Afghans claiming to be migrant children have been here for a year and don’t know one word of German,’ he says.

Just where all this will end is anyone’s guess. For, every day, more migrants arrive in Europe, intent on getting to the prosperous nations of northern and western Europe, including Germany and the UK, to start a new life.

On Tuesday and Wednesday, the Italian navy and coastguard service rescued 3,100 Africans from rickety boats sailing across the Med from Libya.

Nearly 3,000 migrants and refugees have already perished in the Mediterranean this year, while almost 250,000 have reached Europe, the International Organisation for Migration said yesterday.

On Thursday, in a clear criticism of Mrs Merkel, Hungary’s Prime Minister Viktor Orban warned the link between illegal immigration and terrorist attacks on the Continent is as ‘plain as day’, adding: ‘If someone denies this connection then, in fact, this person harms the safety of European citizens.’

The day before, Bavarian Interior Minister Joachim Herrmann had called for stricter controls at Germany’s borders, as it emerged the train axeman had not even got a passport and was allowed through without being fingerprinted.

Anyone who enters the country without papers and who cannot prove their identity must first be detained at the border and checked,’ he said. ‘There are still thousands in our country for whom the procedures aren’t properly carried out.’

However, there is another side to this story. For while many in Germany are growing anxious over the numbers of migrants, there is growing discontent among the migrants themselves. Typical of these is Samir Musa, a 17-year-old Syrian boy who arrived in Berlin last September after excitedly answering Mrs Merkel’s invitation to live in Germany.

Now his hopes have been dashed because of the sheer scale of migration into the country.

As he told me: ‘I am despairing, depressed and so disappointed. It is unbearable in Germany. I can’t see a future. I want to go back home. My journey has been a mistake.’

Samir, the son of a retired garage owner, lives in a hostel for 70 young migrants in Spandau, a suburb of Berlin. He gets given ¤1 (83p a day) pocket money and must obey a 10pm curfew.

A few weeks ago, he slashed his arms, went on hunger strike and took five pills he found on the floor.

It was a protest about his living conditions and the cooking at the hostel which, he says, is ‘bad macaroni’ made every night by a ‘hopelessly bad’ Afghan chef. He was taken by ambulance to hospital where the doctors asked him why he wanted to die.

‘I said that I did not want to live any longer in this way in a strange country,’ he explained to me.

‘The Germans look at me suspiciously on the bus or the train. I only see their faces peering. We are not friends because we cannot talk to each other anyway.’

I first met Samir last autumn. He had walked into Berlin with his hopes high after paying a trafficker for a place on a boat from Turkey to Greece.

He was given a camp bed in a sports hall along with hundreds of others. Outside, Berliners threw a ‘refugees welcome’ party with balloons and songs.

Obviously excited, he told me he expected to get a place at college to do an electronics course and a room in a flat where he could study quietly.

What pipe dreams. After a month, in October last year, Samir had heard nothing from the immigration authorities in Germany. He was left to rot. ‘Every day I was bored. I was getting suspicious that Mrs Merkel’s promises were not true,’ he says now. ‘All over Berlin there were Africans, Pakistanis and Afghans pretending to be Syrians. Everywhere was overcrowded.

‘The officials could not cope. We Syrians, who had been invited, were not given a fair chance.’

He filled in his forms to claim asylum earlier this year, providing the papers that prove he is Syrian. Since then he has heard nothing and officials say it may be December before he gets an answer one way or the other.

Meanwhile, in Deir Al Zour, an IS‑riddled city in eastern Syria, his family live in a rented first-floor flat and wait for news of their eldest son.

‘I dare not tell them how bad it has been for me in Germany,’ he says. ‘I am squashed in with five other young Syrians in one room, and we all feel the same sorrow about coming here.’

Samir is a genuine refugee. He has shown me his papers and given me the names of his parents. But it is clear that he feels burning resentment about his plight.

Miles away in Ochsenfurt, no one really knows what transformed Riaz Khan into a would-be assassin.

Was he radicalised by Islamic State before he set off from Pakistan or turned into a ‘sleeper’ for the terrorist outfit during his long journey?

Or was it in Germany, as he idled his time away in a hostel, that his mind warped to murderous intent?

He stole the axe from the outhouse of his foster parents’ home — so could it have been opportunism that prompted him to act?

One thing is certain. With hostility growing among genuine refugees and an increasing number of Germans, Mrs Merkel’s grand project isn’t looking so grand these days.