The gunman who killed four people before turning the weapon on himself inside a Catholic cathedral in southeastern Brazil on Monday was suffering from depression and “mentally ill,” authorities said.

The man, identified as 49-year-old Euler Fernando Grandolpho, was armed with two guns when he entered the Metropolitan Cathedral in Campinas and opened fire. He killed four people and injured another four before killing himself.

“Thanks to the intervention of police, something much bigger was avoided,” police investigator Jose Henrique Ventura told reporters outside the church.

Grandolpho, a systems analyst from Valinhos, a nearby city, was not a member of the church, authorities said. According to public records, he had held various jobs with government entities, including a stint as an assistant to the prosecutor in the public ministry in Sao Paulo.

Authorities said Grandolpho has no criminal record and “no motive except for his own madness,” the BBC reported.

A backpack found near the dead gunman had the man's identification but no note or other clues, Ventura said.

The victims were identified as Sidnei Vitor Monteiro, 39; Jose Eudes Gonzaga Ferreira, 68; Cristofer Gonclaves dos Santos, 38; and Elpidio Alves Coutinho, 67.

The people who knew Grandolpho told reporters he was withdrawn and suffering from depression.

Police said Grandolpho lived with his father and had not worked since 2015.

Brazil has long struggled with gun violence, and is routinely the world leader in total homicides, though mass shootings are rare.

President-elect Jair Bolsonaro, a former army captain, campaigned on promises to crack down on violence, in part by loosening gun laws so more civilians could arm themselves.

In a tweet on Tuesday, Bolsonaro described the shooting as a “barbaric crime.”

"It's so sad," said Wilson Cassante, a press officer with the archdiocese. "It's hard to imagine the pain this has caused."