An ex-soldier linked to terror group IS has been arrested attempting to buy guns and ammunition following a sting operation at the Edinburgh hostel where he worked.

Muslim convert Gavin Rae, 36, a farmer's son and former infantry soldier, came to regard Britain as "horrible" and "full of paedophiles" saying "the woman are filthy" and "disgusting".

He told an undercover security operative who befriended him: "It's not gonna be long now before Islam will come to the shores of this country."

Rae, who prefers to be known as Yaqub Rae after converting to Islam in 2012, was brought back with his family to the UK after being denied entry to Turkey, which neighbours war-torn Syria, in July 2015.

Four months later he was arrested by armed officers in a hotel room in Crewe, Cheshire, trying to buy pistols and machine guns during the police sting, Preston Crown Court heard.

He told one of the undercover police officers that the authorities thought him a "danger to this country" who believed his intentions were to "go and join Isis and do jihad".

Rae denies five charges all related to attempting to possess or encouraging others to sell or transfer weapons and ammunition, including a Baikal semi-automatic pistol and silencer, a Glock handgun, an Uzi machine gun and ammunition for the weapons.

Anne Whyte QC, opening the case for the prosecution, told the court that on July 3 2014, the defendant was stopped at Manchester Airport trying to board a flight to Cyprus after his suitcase was searched and the contents "were not obviously related to tourism or a holiday".

A year later he took his children to Paris, Marrakech and Casablanca, and then on to Turkey.

But he was stopped by the authorities in the Turkish capital Istanbul and sent back to the UK.

Two months later he was working in Edinburgh at a backpacker's hostel when an undercover police operative, known by the pseudonym "Tony" and posing as a fellow Muslim from Eastern Europe, befriended him.

The day they met, Rae told his new friend: "It's not gonna be long now before Islam will come to the shores of this country...and if they reject it we'll fight them. But we want to live under sharia not democracy."

He said of Britain: "It's full of paedophiles, sex people. It's horrible. Filthy mate. Filthy. Females, the women, the women are filthy you know. Disgusting."

Miss Whyte added: "What he said also, you might think, revealed a man who was plainly very much out of love with life in a modern western democratic country and very much in love with the idea of living in a Muslim country with his family, within the operation of sharia law."

Within a week he told Tony that, once his family were in a Muslim country, he would "go then and sacrifice my life for Allah..."

He also began asking about getting his hands on weapons, the court heard.

Rae, using encrypted messages and being careful over how he communicated on the phone by using a special security app called Surespot, was told by Tony he had a contact, Hamza, who could source guns.

Hamza, also an undercover operative, met the defendant at retail park Bicester Village, near Oxford, near where Rae was working.

He told the defendant he knew someone called Vik, who could convert de-activated weapons, citing a price of £850 for one gun.

A meeting was set up for November 3 last year at a Travelodge near Crewe where Vik was awaiting with the weapons to handover to Rae and Hamza.

Rae was shown the Baikal gun, which had been deactivated, along with ammunition and a silencer, and a Glock pistol.

The defendant allegedly asked for more ammunition and enquired of Vik if he could also get hold of an Uzi machine gun, adding: "Can you do shotguns?"

But after the cash and the gun, silencer and ammunition were exchanged, armed police burst in and Rae was arrested.

The defendant joined the King's Division of the British Army in February 1997 but was discharged 14 months later without going on operations.

He was convicted in 2004 of four robberies of betting shops in Manchester using an imitation handgun.

Rae denies five counts, all dating between October 9 and November 4 2015. They are attempting to possess a weapon with intent to endanger life and attempting to possess a prohibited weapon, both counts relating to the Baikal gun, silencer and weapons.

Rae also denies three counts of encouraging or assisting the commission of an offence, by trying to get "Vik" to source a Glock, Uzi and ammunition.

The trial continues.