TIJUANA, Mexico A crowd of about 1,000 Haitians shouted and shoved at the door of Mexico’s immigration agency at the U.S. border, which has found itself an unhappy gateway for thousands of would-be migrants in recent months hoping to cross into the United States.

They wrapped their arms around the waists of people in front of them to prevent anyone from cutting in line in their desperation for one of just a few dozen slots granted daily with U.S. immigration authorities about a half-mile away.

Several thousand Haitians have traveled to Tijuana in recent months, overflowing migrant shelters and often sleeping outside next to their backpacks on sheets of cardboard, many after traveling 7,000 miles by foot, taxi and bus from Brazil through eight nations to the threshold of the United States. There have been so many that in August, Mexican authorities imposed a system of appointments in order to keep the Haitians away from the flow of other visitors at one of the world’s busiest border crossings.

Most of the Haitians appear unaware that the trip, and the desperate scramble at the border, has been in vain.

Get Breaking News Delivered to Your Inbox

The U.S. Department of Homeland Security on Sept. 21 began putting Haitians in detention facilities before attempting to send them back to the homeland they fled, a departure from previous practice of freeing them on humanitarian parole. The U.S. softened its posture after Haiti’s 2010 earthquake but now treats them like people from other countries.

Many of the Haitians continuing to arrive in Tijuana have said they were unaware of the change, while those who knew about it said turning back was not an option. Brazil opened its doors to the Haitians after the earthquake devastated their impoverished country, but the South American country later developed its own economic problems, recently prompting many to seek work in the United States.

Antonio Juneiro, 40, is typical. He lived in Sao Paolo for four years until factory work dried up and he decided to join family in Miami. After spending $4,000 to reach Tijuana, the prospect of a job in the United States was worth the risk of getting deported to Haiti.

In this Oct. 3, 2016 photo, Haitian migrants receive food and drinks from volunteers as they wait in line at a Mexican immigration agency in Tijuana with the hope of gaining an appointment to cross to the U.S. side of the border. Many Haitians arriving at the Mexico-U.S. border are unaware of a new U.S. policy of putting them in deportation proceedings and detaining them while making efforts to fly them home. Gregory Bull, AP

“When you have money, you have hope. You have health,” Juniero said at the Padre Chava migrant shelter in Tijuana, where he lived for a month while awaiting his appointment at San Diego’s San Ysidro port of entry.

The exodus from Brazil accelerated in May and has shown no sign of slowing. U.S. officials say about 5,000 Haitians showed up at San Ysidro from October 2015 through late last month, and Immigration and Customs Enforcement Director Sarah Saldana said at a recent congressional hearing that officials told her on a trip to Central America that 40,000 more were on their way. Mexico’s National Human Rights Commission said this week that an average of 300 Haitians and Africans were crossing Mexico’s southern border daily.

On Thursday, Nicaraguan authorities captured smugglers driving two trucks containing 98 migrants from Haiti and a variety of African nations. Authorities said they planned to return them to the border with Costa Rica where hundreds of others are stranded.

With hundreds, perhaps thousands, of Haitian men, women and young children regularly spending the night just outside the busiest United States border crossing, Mexican officials have moved to bring some order to the unruly scene by granting 20-day permits to stay in Mexico while also helping schedule their slots with the Americans on the other side.

U.S. Customs and Border Protection can only handle up to about 75 people a day at San Ysidro, and Tijuana authorities were unhappy about large crowds assembled on the Mexican side of the border crossing. So Mexican officials began distributing paper slips with dates to appear at San Ysidro but the documents were often copied. Now, three days a week, officials stamp dates to appear at San Ysidro on 20-day permits that Haitians receive to stay in Mexico.

Mexico also extends the 20-day permits to smaller numbers of U.S.-bound immigrants from Ghana, Senegal and other African countries.

One morning last week, 50 people who had dates to enter the U.S. quietly lined up at the border crossing. A Mexican official emerged from his trailer to say there was room for five more and was mobbed by about 100 people looking to cross. The official led the group across a bridge to a U.S. inspector, who directed them through a turnstile to an area inside the U.S. border station for questioning.

Once inside the United States, the Haitians cannot be turned back to Mexico. With the previous earthquake-related protections now dropped, they are held in U.S. detention centers pending repatriation.

Mexico’s National Migration Institute in Tijuana on Monday made appointments in the coming weeks for 766 people to enter at San Ysidro, making it one of its busiest days since the influx began. All people in line got a date, with the last ones getting appointments for Nov. 10, stranding them in Tijuana for more than five weeks.

Padre Chava, one of 10 Tijuana shelters that house Haitians, turned away hundreds over the weekend, leading many to sleep outside on cardboard sheets. The shelter accommodated 271 people Saturday, about half of them women - some pregnant - and 34 children. Many slept on floors without mattresses. Shouting matches erupted.

“We are exhausted, completely exhausted,” said shelter administrator Margarita Andonaegui. “When we have more than 200 people, we lose control.”

Rosario Lozada, the city’s director of migrant affairs, was exasperated after the latest arrivals raised her estimate of Haitians stuck in Tijuana to 2,000, half of them in shelters and the rest in hotels or on the streets.

“We’ve been going nonstop for almost five months, 24 hours a day,” she said.

It’s early to say if the U.S. policy shift is deterring Haitians from coming, but challenges lie ahead.

Haiti took back just 433 deportees in the 2015 fiscal year - before the influx, the recent policy shift and damage inflicted this week by Hurricane Matthew - and it’s unclear how many the impoverished nation is willing or able to absorb. The United States has a limited number of beds at its immigration detention facilities to accommodate people while flights and travel documents are arranged.

The U.S. Department of Homeland Security said in a statement that it was monitoring the hurricane and “will assess its impact on current policies as appropriate.” The U.S. State Department said in a statement that it was working with other governments on how to address the Haitian immigrants.

Wilfred Jean-Luis, who moved to Brazil in 2014 and left when construction work dried up, was optimistic that he would eventually join cousins in Miami after a grueling journey that included getting robbed in Nicaragua, a common experience among the Haitians.

“How is Haiti going to able to take us back as deportees?” he asked after a night on Tijuana’s streets. “They don’t have the capacity.”