President Trump was frustrated and fuming. Again and again, in the windowless Situation Room at the White House, he lashed out at his national security team over the Afghanistan war, and the paucity of appealing options gnawed at him.

Last month, as Trump mulled over a new strategy in a 16-year conflict that bedeviled his predecessors, he groused that sending additional U.S. troops to Afghanistan could have a negligible impact. He threatened to fire the current commander there. He flirted with privatizing the military effort. He even considered pulling out. Declaring victory seemed all but impossible.

Five weeks later, at a Camp David summit, the commander in chief arrived at his decision. A president obsessed with winning has now settled on simply trying not to lose.

Trump decided to escalate troop levels, but only after protracted deliberations that deeply divided the administration. Lobbied by rival advisers, the president pinballed between his militaristic and anti-interventionist impulses. Impatient during classified briefings, Trump longed to reimagine U.S. policy in South Asia under his “America first” banner.

Ultimately, however, Trump took a more conventional route. He tilted toward the generals who now dominate his inner circle and had urged a large-scale troop expansion, although he did not opt for the tens of thousands of troops they advocated initially.

President Trump addressed U.S. troops and the nation from Fort Myer in Arlington, Va., on Aug. 21 to announce his plan going forward in Afghanistan. (The Washington Post)

Trump’s private deliberations — detailed in interviews with more than a dozen senior administration officials and outside allies — revealed a president un­attached to any particular foreign-policy doctrine, but willing to be persuaded as long as he could be seen as a strong and decisive leader.

“This has been many months in the making,” said Kellyanne Conway, counselor to the president. “The hallmark of leadership is a deliberative process, not an impulsive reaction, and that is precisely the protocol he followed here.”

Part of that listening included hearing out the military about sharing the burden in the region and getting Pakistan more involved in managing the war.

“When Secretary [Jim] Mattis said this would be a South Asia strategy, that tells you a lot,” said John Bolton, a former U.S. ambassador to the United Nations, referring to recent remarks by the defense secretary. “The big issue wasn’t land-war tactics. The big issue is Pakistan.” He called Trump’s Monday speech the “defining moment of the Trump policy seven months into the administration.”

Years before running for president, Trump had a clear message on Afghanistan: It was time to get out. In 2012, he said the war was “wasting our money.” In 2012, he called it “a total disaster.” In 2013, he said, “We should leave Afghanistan immediately.” Trump continued his criticism of the war during the year and a half he campaigned for the White House.

But since becoming president, he has faced a different set of opinions. Defense Secretary Mattis and national security adviser H.R. McMaster, both generals with extensive battlefield experience in Afghanistan, warned Trump about the consequences of withdrawal and cautioned that any move in Afghanistan would have ripple effects throughout the region.

One of the ways McMaster tried to persuade Trump to recommit to the effort was by convincing him that Afghanistan was not a hopeless place. He presented Trump with a black-and-white snapshot from 1972 of Afghan women in miniskirts walking through Kabul, to show him that Western norms had existed there before and could return.

President Trump addressed U.S. troops and the nation from Fort Myer in Arlington, Va. On Aug. 21 to announce his plan going forward in Afghanistan. (The Washington Post)

Another key voice in Trump’s deliberations — especially in guiding the president to make a decision in recent weeks — was John F. Kelly, the newly installed White House chief of staff. A retired four-star Marine general, Kelly had a deeply personal understanding of the stakes: His son, 2nd Lt. Robert M. Kelly, 29, was killed there in 2010 when he stepped on a land mine while leading a platoon of Marines.

“Talking to generals, he realized, you pull out completely and this is what happens: You endanger lives, you endanger American interests, allies, troops, Afghanis who are our friends, and it’s not a stable government,” said a senior administration official.

Trump has nurtured a lifelong infatuation with military culture, going back to his youth at a military academy. One of his favorite movies is “Patton,” the 1970 Hollywood biopic of Gen. George S. Patton’s exploits during World War II.

Thomas J. Barrack Jr., a longtime Trump friend and chairman of his presidential inauguration, said Trump “views generals with a special respect and admiration that allows him to defer to and consider their judgment and expertise in a different light than with his business or political peers who may be Cabinet members or other trusted advisers.”

By summer, the policy review process Trump initiated soon after taking office had grown sclerotic. Hovering over everything was the legacy of former president Barack Obama and his management of the war — a series of decisions that Trump found objectionable. Trump voiced frustration to his advisers about having to clean up somebody else’s mess.

Former House speaker Newt Gingrich described the administration’s view of Afghanistan as one of “patience” about the time it will take to stabilize the region.

“If we can keep American casualties down, we can have patience. The fact is, if you slow down the casualty rate and you’re not losing young Americans, the American people will support gradually growing allies for a long time,” he said, referring to decades of U.S. troop presences in Korea, Germany and Japan.

Trump’s decisions were put off in part because of infighting in his ranks, chiefly between McMaster and chief strategist Stephen K. Bannon, who departed the White House last week. Tensions between the two erupted in July as they talked through Afghanistan options with colleagues and the president.

When McMaster floated possibly sending tens of thousands of additional troops, Bannon shot back that such a commitment would be a folly in a country where intervention had crippled foreign powers through the centuries, officials said.

McMaster expressed alarm and irritation to confidants that Bannon was tempting the president to drift away from the military leadership with ideas that were not feasible. He was especially bothered by a proposal to hand over much of the military responsibility to private contractor Erik Prince, the founder of the controversial security company formerly known as Blackwater USA.

Mattis heard out Bannon’s pitch during a weekend meeting at the Pentagon in early July but quickly sided with McMaster. He and other military leaders were deeply suspicious of handing over any responsibility to private companies due to the controversies that dogged Blackwater and others in Iraq.

Bannon was undaunted, hoping that even if Trump did not adopt his ideas, he would back away from McMaster’s expansive plan. Meanwhile, Bannon’s allies at Breitbart News and elsewhere in conservative media attacked McMaster as a “globalist” who did not have Trump’s interests in mind.

The anti-McMaster campaign, which Bannon denied orchestrating, infuriated some West Wing colleagues, including Kelly. Instead of marginalizing McMaster, the campaign made him a sympathetic figure to military and administration officials who cringed at the wave of negative stories. Trump signaled which side he was on Aug. 10 when he was asked by a reporter whether he had confidence in McMaster.

“Absolutely,” Trump said. “He’s our friend. He’s my friend. And he’s a very talented man. I like him and I respect him.”

Bannon’s vocal opposition had a cost. He was attending fewer meetings. One of his few allies, White House Chief of Staff Reince Priebus, was pushed out just as Bannon was working to wrangle the Afghanistan decision in his direction. And by mid-August, Kelly, McMaster, Mattis and others planned the Camp David retreat without him.

As Trump began to align with the military establishment, Attorney General Jeff Sessions and other advisers reminded the president of the expectations of his die-hard supporters, who thought they had elected a president who would get the United States out of endless wars.

Breitbart — which Bannon returned to last week as executive chairman — ran several skeptical headlines in recent days and played up an interview with Prince in which the Blackwater founder said that putting “more troops and more money” in Afghanistan would be a mistake.

But some Trump allies predicted the base would respond favorably.

“They trust him on this stuff,” said Ed Brookover, a former Trump campaign adviser. “They know he’s gathered information and talked to a series of experts and reached a conclusion. On security issues, they’re with him and know that he’s certainly not jumping to fight wars everywhere.”

Pollster Patrick H. Caddell, who has done surveys for Breitbart, said, “The whole country is tired of the war that’s been going on 16 years and in general believes we’ve wasted a lot of time and money. But if he sells it as part of the war on terror, he’ll be fine.”

While Priebus was considered a passive voice on Afghanistan, Kelly all but forced a decision from the president with newfound urgency. One adviser called him “the accelerator.”

Kelly summoned the national security team to the Camp David meeting Friday with Trump and Vice President Pence, where the president was presented with his options.

Trump’s decision was foreshadowed by a grimacing pose he and his team struck in a portrait that the president put on his Twitter page. In a wood-paneled room, Trump sat at a table scowling as 13 advisers stood behind him, each of them stone-faced and staring into the camera. The flags of the five military branches filled the background. To Trump, this was the image of strength.

Some of Trump’s critics were relieved that the military prevailed in shaping Trump’s strategy.

“The president doesn’t know anything about war or anything about Afghanistan,” said Eliot A. Cohen, a foreign policy adviser in the George W. Bush administration. “He has a lot of angry instincts, but nothing more than that. So he is to some extent corralled by McMaster, Kelly and Mattis. . . . He is going along with what the generals want.”

Kori Schake, another Bush administration veteran who, like Cohen, opposed Trump’s candidacy, said she was heartened by the president’s decision.

“I don’t think it’s a bad thing that the president took his time, asked first-order questions, and widened the aperture to include outside perspectives and unconventional approaches,” said Schake, a fellow at Stanford University’s Hoover Institution. “It’s a hard problem, and we’ve been at it a long time.”