SAN DIEGO — In Bob Filner's final days as mayor, the city attorney was prepared to do something never before done here: plead with a judge that the mayor posed a threat to women and should be barred from City Hall.

A psychologist retained by City Atty. Jan Goldsmith was set to testify that, in her opinion, Filner fit the characteristics of a sociopath, was "without shame, empathy or compassion," and believed no rules applied to him.

A court hearing was set for Aug. 21.

It was not necessary.

The night before the hearing, after two days of intense negotiations, Filner agreed to resign in exchange for the city paying most of his legal bills in a sexual harassment suit filed by Los Angeles attorney Gloria Allred on behalf of a former mayoral staffer.

For six weeks, Goldsmith had maneuvered to force Filner out of office — by squeezing him financially, bluffing him about state law, embarrassing him by releasing documents showing the anger and dismay of his staff and threatening to force a trial on his alleged misuse of public funds, including for a junket to Paris.

Goldsmith, 62, a former judge and state legislator, said he believes the threat of a restraining order — and the national media storm certain to follow — finally persuaded the normally combative mayor to step down.

Filner's attorneys declined comment for this story. So did Filner, now awaiting a Dec. 9 sentencing for his guilty plea on charges brought by the state attorney general.

In early July, as one woman after another went public with accusations of sexual harassment against Filner, Goldsmith and his staff concluded that Filner was an unrepentant felon and that women at City Hall needed to be protected from him.

But the City Charter contains no provision for removing a mayor except through the difficult, expensive, politically unpredictable process of a recall election.

"We strategized as lawyers: How were we going to remove the mayor?" Goldsmith said in a recent interview. "It was a de facto impeachment."

Filner and Goldsmith had a rocky relationship from the beginning.

Filner, 71, was the first Democratic mayor elected in San Diego in two decades. Goldsmith, a Republican serving his second term as city attorney, had endorsed Filner's GOP opponent.

Filner arrived at City Hall with a long reputation — earned on the school board, City Council and then 10 terms in Congress — as a hardball political player.

At their first meeting, Goldsmith said, Filner announced that he did not plan to take legal advice from the city attorney's office. He threw a sheaf of legal papers in the air.

"Your blow-up yesterday concerns me," Goldsmith wrote in a Jan. 3 memo. "Our office is a law office, and we expected to be treated as professionals. Yesterday's meeting was the first and only meeting in which we (any of my lawyers) will tolerate being yelled at, called names or having things thrown at us.... Now it is zero tolerance."

But the friction continued unabated. By February, months before any sexual harassment allegations appeared, "I understood he needed therapy," Goldsmith said.

On Feb. 15, Goldsmith sent another memo to Filner citing his "abusive conduct" during a meeting with lawyers from Goldsmith's office.

"It did not take you very long to violate the zero tolerance standard," Goldsmith wrote.

Still, Goldsmith suggested the two meet privately and agree to a peace treaty. "I still think we should get together for coffee," Goldsmith wrote Filner on Feb. 26.

There was no coffee.

Filner clashed with Goldsmith over the city's marijuana ordinance, a misdemeanor charge brought by Goldsmith's office against a beach demonstrator in La Jolla and a charge against a protester for chalking anti-bank messages on city sidewalks.

The mayor crashed one of Goldsmith's news conferences. Later he had a police officer escort one of Goldsmith's top aides out of a closed meeting.

After Allred sued on behalf of Irene McCormack Jackson, Filner's former communications director, Goldsmith's investigators examined Filner's finances and concluded that he could not afford lawyers to fight the lawsuit.

One fact seemed telling: Filner had waited months to reimburse the city for $900 in personal expenses on a city credit card.

A decision was made to squeeze Filner, giving him the choice to resign or wage an expensive legal fight.

"We didn't think he had the willingness or [financial] ability to deal with the legal issue," Goldsmith said.

Goldsmith persuaded the City Council to refuse to defend Filner in the Jackson lawsuit and instead force him to hire private attorneys.

"It was a bluff," said Goldsmith, noting that California law requires a public employer to represent an employee, even a mayor, accused of on-the-job misdeeds.

Goldsmith's investigators gathered numerous declarations from women who alleged that they had been sexually harassed by the mayor. The mayor's office was forced to disclose notes taken at staff meetings that showed growing anger at Filner's abusive treatment.

As Filner hid from the media, Goldsmith said in numerous interviews that it was only a matter of time until Filner would be out. By mid-August, all nine City Council members — five Democrats and four Republicans — wanted Filner to resign.

A retired federal judge was persuaded to oversee negotiations between Filner, his attorneys, Goldsmith and his staff, and Councilman Kevin Faulconer and Council President Todd Gloria.

After two days of intense negotiations, one of the last items to be resolved involved money.

Under the agreement, the city would pay up to $98,000 for Filner's private attorneys defending him in the Jackson lawsuit. Those attorneys billed about $28,000 above that amount and would work out the difference with Filner.

The city is not paying any of Filner's legal costs related to the state attorney general's investigation that led to his guilty plea or to any other civil suits that might arise.

Filner signed the agreement Aug. 21, and it was approved by the council two days later. After a farewell speech that was alternately sorrowful and defiant, Filner's resignation was effective Aug. 30.

On Oct. 15, Filner pleaded guilty to felony false imprisonment and two counts of misdemeanor battery, all involving mistreatment of women during his months as mayor.

Under the plea bargain with the attorney general, Filner will not go to jail or prison but will serve three months of home confinement, agree to never again seek public office, have his mayoral pension reduced and undergo mental health counseling.

For the psychological analysis of Filner that was never used, the city paid $4,550 — calculated at 13 hours of consultation at $350 an hour, according to documents obtained by The Times under the California Public Records Act.

Solana Beach psychologist Sage de Beixedon Breslin never interviewed Filner but studied his many public statements and demeanor on Google and YouTube.

An expert witness in numerous cases involving restraining orders and accusations of sexual harassment, Breslin said in an interview that she was "cautiously optimistic that watching someone toppled from power because of these kind of crimes might have a good impact, might instill just a little fear in people like this."

Goldsmith has proposed a charter amendment to allow for a mayoral impeachment process.

Looking back on his long dispute with Filner, he said his years as a judge helped him maintain his composure.

"It was like having the litigant from hell in your courtroom for eight months," he said.