DES MOINES, Iowa — A Palestinian man who was jailed in Iowa for two years while immigration authorities tried to secure permission to deport him has been released after a federal judge ruled he had been held for too long without a reasonable expectation of being repatriated soon.

Advocates for immigrants' rights say Hasan Salama Dibai Ghithan's case is not unusual and that far too many immigrants awaiting deportation are jailed indefinitely because they don't know how to navigate the U.S. legal system.

"I haven't seen the sun in two years," Ghithan told The Associated Press on Friday, just hours after his release from the Pottawattamie County Jail. "Man, it was just a nightmare. A nightmare. I'm glad it's kind of over now."

Ghithan, 33, was trying to renew his expired green card in Omaha, Nebraska, in January 2017, when U.S. Immigration and Customs Enforcement agents discovered he had been convicted on a drug charge in Texas a year earlier. They locked him up in Council Bluffs, Iowa.

Months passed as Ghithan sought his release or deportation to a part of the world he hasn't even visited in 14 years. He became so desperate in jail that he even offered to pay for his own flight.

Complicating his departure, Ghithan can't be flown direct to his homeland because the U.S. doesn't recognize Palestine as a sovereign, independent state. Immigration officials said they must first obtain permission from Israel, transport him to Jordan and get him permission to cross the Allenby Bridge from Jordan into the West Bank, although Jordan rescinded a memorandum of understanding that allowed such crossings in September. Thus, the U.S. government has been unable to get the required permissions.

Judy Rabinovitz, deputy director of the American Civil Liberties Union's Immigrant's Rights Project, says she doesn't know how many people are jailed on indefinite immigration holds, but she believes Ghithan's situation isn't unusual. Many immigrants don't know how to navigate the U.S. courts, said Rabinovitz, who is not involved in Ghithan's case.

"Something's wrong with a system that allows people to be warehoused for years and years and years unless they're able to go into court on their own," she says. Rabinovitz says she has worked with immigrants who were jailed in the U.S. for even much longer than Ghithan before being deported.

A report by U.S. Immigration and Customs Enforcement indicates a sharp increase in the arrest and deportation of immigrants who have broken U.S. laws. More than 396,000 people were booked into ICE detention facilities during the fiscal year that ended in September, an increase of 22.5 percent from the previous year. More than 250,000 immigrants were deported , and more than half of them were convicted of a crime .

Ghithan was born in Ramallah in the West Bank and arrived in the U.S. on a student visa in 2005. He married an American woman and received a green card, but allowed it to expire in 2012.

He went to college in Texas where, Ghithan says, friends led him astray. He was convicted on a gambling-related charge in Waco in June 2013, and possession of drug paraphernalia in May 2016. He was released on probation and given a deferred judgment.

He then separated from his wife and moved to Omaha to live with his uncle and cousins. It was there, as he sought to renew his green card in early 2017, that ICE officials uncovered his criminal record and jailed him in Iowa.

Ghithan was told he would see a judge within two weeks, and he expected to post bail and be released. But it was two months before a judge considered his case, and that judge denied him bail because his crime was drug related.

An Omaha immigration judge meanwhile rejected Ghithan's request for asylum based on his claim that he was fleeing violence in the Middle East. The judge ordered him to be deported to Israel, but nothing happened.

After calling the American Bar Association from jail, he challenged the constitutionality of his imprisonment but mistakenly filed the document in Omaha because he thought he was being held in Nebraska. Four months passed before a judge wrote to him, telling him that because he was in Iowa, he needed to file in federal court in that state.

He filed another petition in September 2018. After months of exchanges between U.S. District Judge Robert Pratt in Des Moines and federal officials, it became clear Ghithan's release was not imminent.

On Dec. 10, federal officials suggested holding Ghithan until at least March to give them time to get the paperwork in order.

Ghithan's court-appointed attorney, Kevin Hobbs, argued that Ghithan's indefinite detention was "in violation of due process under the United States Constitution."

Pratt granted Ghithan's release under supervision of immigration authorities Jan. 2, saying there was no significant likelihood of his removal in the foreseeable future.

The government filed documents Friday confirming his release.

Ghithan, who is staying with relatives in Omaha, is unsure about his future. His attorney has told him he likely won't be deported if he avoids trouble. But the immigration officer supervising his release told him not to bother looking for a job because he will be deported within a month or two.

"He's like, 'I'm sorry you were in jail this long, but if it was up to me I'd keep you in there because there is a good chance I'm still going to deport you,'" Ghithan said.