The facility spread out below him, row after row of neatly aligned white aluminum roofs, looking like Chiclets set against the endless beige of the desert floor.

It was called Camp Bucca. To coalition forces in Iraq, it was the primary detention facility for enemy prisoners of war. To Mitchell Gray, then 48 and serving his country for the third time, it was simply the place where the US Army had decided his skills, which included a law degree and a fluency in Arabic, were needed most.

He and the rest of his unit, the 45th Infantry Brigade of the Oklahoma National Guard, were flying helicopters in from Kuwait. It was shortly after landing that he got a first glimpse at a few of the 26,000 detainees, staring at him from the other side of the concertina wire.

“You never see hatred on the faces of Americans like you saw on the faces of these detainees,” Gray remembers of his 2008 tour. “When I say they hated us, I mean they looked like they would have killed us in a heartbeat if given the chance. I turned to the warrant officer I was with and I said, ‘If they could, they would rip our heads off and drink our blood.’ ”

What Gray didn’t know — but might have expected — was that he was not merely looking at the United States’ former enemies, but its future ones as well. According to intelligence experts and Department of Defense records, the vast majority of the leadership of what is today known as ISIS, including its leader, Abu Bakr al-Baghdadi, did time at Camp Bucca.

And not only did the US feed, clothe and house these jihadists, it also played a vital, if unwitting, role in facilitating their transformation into the most formidable terrorist force in modern history.

Camp Bucca started, as so many policy blunders do, with nothing but the best intentions. The Army simply needed a place to stick bad actors where they could not harm US troops.

The 800th Military Police Brigade, a reserve unit based on Long Island, were the ones who christened it Camp Bucca. It was fitting symbolism for a place designed to hold terrorists: Ronald Bucca was an FDNY marshall who died on 9/11.

For much of the war, Bucca might as well have been invisible to folks back home. The war correspondents focused their attention on Mosul or Fallujah, places where the bullets were flying and the blood was flowing. The only detention facility to gain real notice was Abu Ghraib, where abuses against Iraqi prisoners made headlines around the world.

If you were a jihadist, Bucca became the place to be. - Michael Weiss, co-author of “ISIS: Inside the Army of Terror.”

Bucca had no such scandal. It is only now, six years after it was shuttered for good, that Bucca is starting to gain its own infamy.

The dilemma of Camp Bucca began almost immediately after the invasion. During that chaotic time, coalition forces — unable to distinguish friend from foe — were sweeping up huge numbers of military-aged males and warehousing them at Bucca.

“We knew there were some bad guys in there somewhere,” said a former officer at Bucca, whom I’ll call Greg, who asked for anonymity due to his ongoing work with the Defense Department. “The question was which ones? It was a constant game between the guards and the detainees.”

The camp was divided into compounds of roughly 1,000 inmates. The Americans knew Sunnis and Shiites, the two main factions of Islam in Iraq, could not be incarcerated in the same compound if the camp was to remain peaceful.

They also quickly learned moderate Sunnis and extreme Sunnis could not be kept together. The extremists instituted Sharia Law, the canonical law of Islam, and either radicalized the moderates or punished them for failing to toe the line, gouging their eyeballs or cutting out their tongues.

Jennifer Stephens, who was with the 320th Military Police Battalion, arrived at Bucca in March 2003. Now a Sheriff’s Deputy in Wyoming, she sees parallels between Bucca and the jail where she works in Laramie County.

“It’s just like it is here with the gangs,” Stephens said. “They might not be gang members when they go into jail, but they sure are by the time they get out.”

Greg, who was in charge of one of the compounds, said the guards began to get more savvy about identifying hardcore jihadists. One method was to bring in a detainee for interrogation and leave him alone in a room that had a Maxim magazine lying around. The inmates who resisted looking at it would be shunted off with the other zealots.

It was a sound strategy for keeping peace at a prison. For the larger war on terror, it turned out to be a disaster. By putting the worst of the worst together, the US was essentially hosting a terrorist convention. “Bucca didn’t create the problem of anti-American sentiment, but it exacerbated the problem by localizing it and concentrating it,” said Michael Weiss, co-author of “ISIS: Inside the Army of Terror.” “If you were a jihadist, Bucca became the place to be.”

Early in Bucca’s existence, the most extreme inmates were congregated in Compound 6. There were not enough Americans guards to safely enter the compound — and, in any event, the guards didn’t speak Arabic. So the detainees were left alone to preach to one another and share deadly vocational advice.

Adel Jasim Mohammed, a former detainee, once described the scene to Al Jazeera. “Extremists had freedom to educate the young detainees,” Mohammed said. “I saw them giving courses using classroom boards on how to use explosives, weapons and how to become suicide bombers.”

They were also networking in ways that never would have been possible outside the wire. A few months back, The Guardian published an extraordinary interview with an ISIS leader it called Abu Ahmed, who described his years at Bucca in glowing terms.

“We had so much time to sit and plan,” Ahmed said. “It was the perfect environment. We all agreed to get together when we got out. The way to reconnect was easy. We wrote each other’s details on the elastic of our boxer shorts. When we got out, we called. Everyone who was important to me was written on white elastic. I had their phone numbers, their villages.”

“It really was that simple,” Ahmed said later during the interview. “Boxers helped us win the war.”

Al-Baghdadi, the self-proclaimed caliph of the Islamic state, spent 10 quiet months at Bucca. According to Pentagon records, he was released in December 2004. He was such a model inmate, a military review board deemed him not to be a significant threat.

Bucca also housed Haji Bakr, a former colonel in Saddam Hussein’s air-defense force. Bakr was no religious zealot. He was just a guy who lost his job when the Coalition Provisional Authority disbanded the Iraqi military and instituted de-Baathification, a policy of banning Saddam’s past supporters from government work.

According to documents recently obtained by German newspaper Der Spiegel, Bakr was the real mastermind behind ISIS’s organizational structure and also mapped out the strategies that fueled its early successes. Bakr, who died in fighting in 2014, was incarcerated at Bucca from 2006-’08, along with a dozen or more of ISIS’s top lieutenants.

The collusion at Bucca got so bad that when Marine Maj. Gen. Doug Stone was put in command of the camp in 2007, he became aware that some insurgents were allowing themselves to be caught so they could join their comrades.

“They showed up knowing about our intake process,” Stone said. “They would come in and say, ‘I believe this and such and therefore I’d like to get into Compound 34.’ These guys were using detention for their own purposes.”

Stone immediately set about breaking up the 1,000-man compounds into 10-man huts to limit inmate interaction. He released detainees who weren’t a threat, so they couldn’t be indoctrinated by the ones who were. He implemented an ideological re-education program, bringing in local imams to preach a moderate version of the Koran.

“It made a difference. And if we had done some of those things five or six years earlier, it might have made more of a difference,” Stone said. “At that point, it was probably too little too late.”

It was into this environment — a reformed but still teeming Bucca — that Gray, the Oklahoma National Guardsman, found himself in 2008.

His journey there had started three years earlier with, of all things, a Yankees game. A lifelong fan, he was getting his dose of John Sterling and Suzyn Waldman via satellite radio when he heard an ad: the Army needed people who spoke Arabic.

Gray, who had an interest in Islam from his legal work in the oil and gas industry, had started learning the language in the late ’90s. He has since written a book about 9/11 conspirator

Zacarias Moussaoui titled, “I Heard You Were Going on Jihad.”

In early 2008, he arrived at Bucca and was briefed about behavior toward inmates at what the Army called the TIF, the Theater Internment Facility. “They told us, ‘We’ve got to treat all of these guys with respect. Nelson Mandela spent time in prison. The next Nelson Mandela could be in this TIF,’ ” Gray said. “While they were looking for the next Nelson Mandela, they missed the next Osama bin Laden.”

Gray, who did some intelligence work while at Bucca, remembered zeroing in on a contractor who came into the camp to sell DVDs to the troops.

“We knew pretty early on the guy was no good,” Gray said. “He never seemed to be there when the mortars came in, if you know what I mean.” We put together a packet that proved beyond a reasonable doubt — and I use that term as an attorney — that he was associated with some of these radical militias.”

Gray presented the package to his superiors. “The MP Brigade operations officer, a lieutenant colonel who I won’t name, shut it down,” Gray said. “He said, ‘This guy provides a lot of good morale to these troops. He’s going to stay.’ To me, it was a sign we had forgotten 9/11 and lost focus on why we were supposed to be there. At that moment I said, ‘So much for the War on Terror.’ ”

As the battle-weary US began scrambling for the exit in 2009, many former-and-future jihadists held at Bucca were simply let go. Others were released into the hands of Iraqi authorities, who proved inept at keeping them locked up.

At that moment I said, ‘So much for the War on Terror.’ - Mitchell Gray

One of the first priorities of the Islamic State when it began organizing and gaining strength in 2010 and 2011 was to spring its would-be compatriots, which it did through a combination of bribing corrupt Iraqi officials and staging coordinated attacks on the prisons.

“There were a number of detainees that when we left, we said, ‘Whatever you do, don’t let these guys out,’ ” Stone said. “But of course they all got free. There was a lot of talent and expertise there. They really broke out the Who’s Who in the Zoo.”

It is with sadness that Stone, Gray and other soldiers look at the chaos now in the region. They came to help an oppressed people build a new Iraq. The end result, as ISIS rewrites the rules of region, may be that Iraq is wiped off the map altogether.

“There were obviously mistakes made in how we handled Iraq,” said Greg, the ex-officer. “In retrospect, bringing every jihadi and insurgent into the same place and giving them all the time in the world to get to know one another may go down as our biggest mistake.”

Brad Parks is a novelist. His next book, “The Fraud,” will be released from St. Martin’s Press/Minotaur Books in July.