In his first week in office, President Obama signed an executive order that would shut down the notorious U.S. military prison at Guantanamo Bay, Cuba, within a year. But as the United States moves to end this shameful episode, it's worth reflecting on the untold story of the very beginnings of Guantanamo.

The following account, which draws on dozens of interviews I conducted over the past few years, tells the startling tale of a period shortly after Sept. 11, 2001, when military officers on the ground tried to do the right thing with the recently captured detainees but were ultimately defeated by civilian officials back in Washington. Those early days — back before Gitmo became Gitmo — strongly suggest that the damage the prison inflicted on America's honor and security could have been avoided if policymakers had been willing to follow the uniformed military's basic instincts.

It may be too late for these revelations to help redeem Guantanamo in its waning days. But those crafting U.S. detention policy in the years ahead could still benefit from learning about these small initial efforts at decency.

The story begins in the first week of January 2002, when Joint Task Force 160, led by Marine Brig. Gen. Michael Lehnert, dutifully landed at Guantanamo Bay. Lehnert's approximately 2,000 troops were fired up about their mission: building the first detention facility for prisoners taken from the Afghan battlefield. The unit had a 96-hour deadline, according to Lehnert, and they were told that about 300 detainees were already en route to Cuba.

As Col. William Meier, Lehnert's chief of staff, explained it, the task force had to help build hundreds of cells and the massive tent city needed to house the U.S. troops coming in to guard them.

But it wasn't the logistics that most worried Lehnert. It was the policy vacuum into which he and his troops had been thrown.

"We are writing the book as we go," one officer said at the time. Lehnert said he had been told by the Joint Chiefs of Staff that the Geneva Conventions would not technically apply to his mission: He was to act in a manner "consistent with" the conventions (as the mantra went) but not to feel bound by them. The Joint Task Force, advised by U.S. Southern Command, was essentially left on its own to improvise a regime of care and custody for the allegedly hardened al-Qaida terrorists — whom the Bush administration famously called "the worst of the worst" — who would be coming their way. The idea, as Lehnert told me he understood it, was to detain them and wait for a legal process to begin.

In the absence of new policy guidance about how to treat the detainees, Lehnert told me that he felt he had no choice but to rely on the regulations already in place, ones in which the military was well schooled: the Uniform Code of Military Justice, other U.S. laws and, above all, the Geneva Conventions. The detainees, no matter what their official status, were essentially to be considered enemy prisoners of war, a status that mandated basic standards of humane treatment. One lawyer for the Judge Advocate General Corps, Lt. Col. Tim Miller, told me that he used the enemy-POW guidelines as his "working manual."

The task force set to work around the clock, processing the detainees upon arrival, administering medical treatment and providing general care in the cells of the newly built Camp X-Ray. Lehnert's lawyers studied the 143 articles of the Geneva Conventions, paying particular attention to Common Article 3, which prohibits "humiliating and degrading treatment." The head of the operation's detention unit, Col. Terry Carrico, summed up the situation to a team of Marine Corps interviewers several weeks into the mission: "The Geneva Conventions don't officially apply, but they do apply."

But there were early signs of trouble. Lehnert told me that his request to bring representatives of the International Committee of the Red Cross (ICRC) to Guantanamo — something international law requires for all prisoners being held in war-related situations — was, as he heard it, shunted aside somewhere up the chain of command.

"The initial request," he recalled, "was turned down." He persisted. Even if he obviously could not implement some of the Geneva Conventions requirements — the right to musical instruments, for instance, or the right to work for payment — he wanted advice from ICRC professionals to help him ensure the prisoners' safety and dignity.

Exasperated by repeated attempts to find out which guidelines to apply to the detainees, Col. Manuel Supervielle, the head JAG at Southern Command, picked up the phone and called the ICRC's headquarters in Geneva. As one member of the Southern Command staff remembers the episode, the Joint Chiefs of Staff had warned the Gitmo task force that Defense Secretary Donald Rumsfeld's office opposed getting involved with the ICRC. But now, according to Supervielle, a U.S. officer was asking the ICRC to help out at Guantanamo. The ICRC answered with an immediate "yes."

It was a pivotal moment in the history of Guantanamo. Once Supervielle's call had been made, the civilian policymakers around Rumsfeld could not undo what the uniformed military had done — although, according to Supervielle, an irritated team of lawyers, including Pentagon general counsel William "Jim" Haynes, asked the Southern Command lawyer days later whether there was "a way to back out of it now."

The ICRC arrived at Guantanamo on Jan. 17, 2002 — six days after the detainees did. Thus began what amounted to a period of subtle defiance of Washington's lack of direction. The ICRC worked with Joint Task Force 160 to create a rational, legal detention operation.

ICRC representatives immediately began to help Lehnert's troops improve the grim physical situation of the hastily constructed camp: the open-air cages in which prisoners were held, the cells without toilets, the constant exposure to heat and rain.

To intensify his efforts, Lehnert told me, he requested a Muslim chaplain, Navy Lt. Abuhena Saifulislam. "Saif," as the Bangladeshi American imam was known throughout the camp, became a fixture inside the blocs of cages at Camp X-Ray. Task force members recall him strolling daily through the camp, sometimes accompanied by Lehnert, and conversing with the detainees — some of whom were in no mood to chat, some of whom had stories to tell. Lehnert tried to assure them that some form of legal remedy or transfer home was in the works, as one former detainee, British citizen Shafiq Rasul, told me.

Lehnert had built his own Guantanamo, one with ICRC oversight, a Muslim chaplain and an overriding ethos that stressed codified law and the unwritten rules of human decency.

Lehnert's team let the detainees talk among themselves; it provided halal food, an additional washing bucket inside cells that lacked toilet facilities, a Quran for each detainee, skullcaps and prayer beads for those who wanted them, and undergarments for the prisoners to wear at shower time, in accordance with Islamic laws that proscribe public nakedness.

Perhaps Lehnert's Guantanamo could have been sustained. But Rumsfeld wanted something else: He expected to get valuable, actionable intelligence from the detainees.

By late January 2002, according to Brig. Gen. Galen Jackman, Lehnert's chief contact at Southern Command, the defense secretary told officers on a video conference call with Southern Command that he was frustrated by the absence of such information.

A displeased Rumsfeld seems to have decided to create a second command, one that would exist side by side with Lehnert's. It would be devoted solely to gathering intelligence and would be headed by a reservist major general, a former U.S. Army interrogator during the Vietnam War named Michael Dunlavey. Jackman told me that he considered the idea of two parallel commands a "recipe for disaster."

At the same time, Navy Capt. Robert Buehn, the commander of the naval base at Guantanamo, recalled, the Gitmo task force's initial expectations of orders to build a courtroom began to fade.

As Dunlavey's command took shape in late February and early March, the fabric of prisoner's rights that Lehnert had woven was beginning to unravel. By the end of February, nearly 200 detainees had mounted a hunger strike to protest their treatment. Interrogations, not trials, had become the future of Guantanamo.

But Lehnert did not concede defeat. In later accounts, several detainees described the surprise they felt watching the general walk through the camp in response to the hunger strike. As these prisoners remembered it, Lehnert would sit on the ground outside the wire-mesh cells, hat in hand, and make promises to prisoners in exchange for their agreement to eat.

According to these detainees, he promised to remove a guard who they said had kicked a copy of the Quran and to find a way to reduce the chafing of the ankle shackles they wore during transport.

One German detainee, Murat Kurnaz, was among the detainees who watched Lehnert negotiate with the prisoners. "Was he trying to signal that ... he wanted to speak to the prisoner as a human being?" Kurnaz said he wondered. Lehnert admitted to me that, with the help of Saif, the chaplain, he even put in a call to a detainee's wife to find out whether she had safely delivered the baby they were expecting — a boy, it turned out.

Above all, the U.S. general hoped to avoid having to feed the prisoners by force.

Thanks in large part to Lehnert's efforts, the hunger strike dwindled to a couple of dozen fasters by the first week of March. But as much as he might have championed the need to respect the detainees as individuals — albeit allegedly dangerous terrorists — Guantanamo's future had been decided.

As the hunger strike wound down, Lehnert said, he and his unit were given notice that they would soon be leaving.

Once Lehnert's troops departed, a new Guantanamo took shape — the Guantanamo that an appalled world has come to know over the past seven years. Inmates were kept in isolation, interrogation became the core mission, hunger strikers were regularly force-fed, and above all, the promise of a legal resolution to the detainees' cases has eluded hundreds of prisoners.

As Obama moves to close Guantanamo down, the story of Joint Task Force 160 takes on new significance. Had the United States been willing to trust in the professionalism of its superb military, it could have avoided one of the most shameful passages in its history.

Lehnert still regrets the legal limbo that Guantanamo became — and the damage that did to America's "stature in the world." As he put it, "the juice wasn't worth the squeeze."

And there is a final irony on the horizon.

One of the places now being considered as a new U.S.-based destination for the remaining Gitmo detainees is Camp Pendleton, a Marine Corps base in Southern California. The base's commanding general is none other than Michael Lehnert, now a major general.

The detainees might well be returned to his custody. In several senses, we could wind up right back where we started. This time, however, we should have the law on our side — not to mention a conscience.

Greenberg is the executive director of New York University's Center on Law and Security and the author of the forthcoming "The Least Worst Place: Guantanamo's First 100 Days." Author' e-mail: kjg550@gmail.com.