A knife-wielding man, shot dead by Spanish police, had come out as gay and was contemplating suicide, reports quoting his ex-wife's testimony say.

Algerian Abdel Wahab Taib, 29, shouted in Arabic as he targeted a police station in a town south of Barcelona.

Police are treating the incident as a "terrorist attack" but a source close to the investigation told AFP they do not suspect any links to jihadism.

Police have searched Taib's home and questioned his former wife.

Relatives of the attacker are now considering suing the Catalan regional police for negligent conduct, their lawyer said.

'Suicide by cop'
The ex-wife "said they had separated, that he had revealed his homosexuality and that he was disturbed about how that fits in with the Muslim religion" the source in the investigation said.

"We are putting this down to confusion, a suicide attempt. There are some who do it throwing themselves off a bridge, he opted for this method."

The source said, "We're basing ourselves on his wife's testimony, who says he had not veered towards extremism."

Spanish media report the separation began a year ago, and divorce papers were signed last Tuesday.

The woman's lawyer told reporters Taib was due to leave the family home soon. He also denied terrorism as the motive behind the attack, saying the Algerian was going through a personal crisis.

What happened?
Taib approached the police station in the town of Cornellà de Llobregat at 05:45 (03:45 GMT) on Monday, buzzing the intercom and demanding to be allowed in.

When a woman officer behind a reception window opened the door, the 29-year-old lunged at her "with a clear intent to kill", Police Commissioner Rafel Comes told reporters.

He shouted "Allahu Akbar" (God is greatest) as he rushed forward, and other Arabic words the police did not understand.

The officer opened fire three times, fatally wounding the attacker, the commissioner said. The officer had used a gun to save her life, he said, although it was unclear if she was behind a glass partition at the time.

"For now we are treating it as a terrorist attack," he said, although he stressed there was nothing at this stage to link him to the jihadist cell involved in the attacks on Barcelona's Las Ramblas venue on 17 August last year and Cambrils hours later.

What do we know about the attacker?
Abdel Waheb Taib had been living in Spain for several years.

He had no criminal record and lived with his ex-wife, a Spanish citizen who had converted to Islam after they met, just 150m (492ft) from the Cornella station.

He was not under surveillance or suspected to be at risk of radicalisation, police said.

His ex-wife reportedly said Taib feared shame and disgrace should the Muslim community ever discover his homosexuality.

Local media say the night before the attack, he left a note to his ex-wife, saying "I am leaving, insha'Allah [God willing], to the great place that is above."

Police reportedly found other letters written by the Algerian, one in particular begging Allah (Arabic for God) for forgiveness.

Prosecutors said Taib attacked the police "with the intent to kill, or die trying", newspaper El Pais quoted.

They said they believe he sought a kind of religious redemption for what he considered the sin of being gay.