As thousands of cops hunted for a terrorist who planted 10 bombs from the Jersey Shore to Chelsea and succeeded in detonating two of them, their suspect was hiding in plain sight outside a dank New Jersey pub.

He was “sleeping like a baby,” said mechanic Chris Ricco, who thought Afghan immigrant Ahmad Khan Rahami was a bum when he spotted him in the doorway of Merdie’s Tavern in Linden around 10:30 a.m.

Bar owner Harinder “Harry” Bains roused Rahami, 28, from his slumber — only to get cursed out by the country’s most-wanted fugitive.

“Go f–k yourself! I’m sleeping,” Rahami grumbled.

Bains saw the face of the “vagrant” and became suspicious.

“I was watching CNN news all day,” Bains told The Post. “In my mind I said, ‘Oh, my God, this guy looks so much like the guy.’ ”

Bains tricked Rahami into staying put by assuring him the bar wouldn’t open for another hour, then called the cops, according to a law enforcement source.

One of four cops who responded saw Rahami and immediately recognized him from the FBI’s wanted poster, Capt. James Sarnicki said.

“The officer then told him, ‘Show your hands,’ and the suspect pulled out a handgun and fired a round,” Sarnicki said.

The bullet hit the officer’s bulletproof vest in his abdominal area, and he was not seriously injured, Sarnicki said.

Rahami fired another shot that smashed through the windshield of another cop’s cruiser and “glanced off” the officer’s forehead, Sarnicki said.

“There was a lot of bleeding because it’s the facial area, but he’s going to be OK,” Sarnicki said.

A running gun battle ensued, with Rahami firing shots as he tried to escape on Elizabeth Avenue, said Jack Mazza, who works with Ricco.

“The cops were chasing him and he was shooting while he was running. I guess he was trying to shoot the cops but he was hitting passing cars,” Mazza said.

More cops raced to the scene, “returned fire and were able to take him down,” Sarnicki said.

One officer was shot in the hand, authorities said.

Bains described his act as dumb luck instead of heroism.

“Who in the world would think this wanted terrorist was loitering like a homeless man outside my bar?” he said.

Rahami was shot in the leg and underwent surgery Monday afternoon at University Hospital in Newark, acting Union County Prosecutor Grace Park told NJ.com.

In addition to the Saturday night blast that injured 29 people on West 23rd Street, Rahami is suspected of leaving a pressure-cooker device that failed to detonate four blocks north.

The pressure cooker on 27th Street was in a bag with a letter that made reference to previous terrorists, including Boston Marathon bombers Dzhokhar and Tamerlan Tsarnaev, according to CNN.

The Tsarnaev brothers used similar pressure-cooker devices packed with shrapnel to kill three people and injure 264 others near the race’s finish line in 2013.

He also is suspected of earlier planting three pipe bombs — one of which exploded harmlessly — in a garbage can along the route of a Marine Corps charity run in Seaside Park, NJ.

He left five more devices found in a backpack at the Elizabeth, NJ, train station Sunday night, cops said.

Rahami was charged in Union County, NJ, with five counts of attempted murder of a police officer on Monday evening.

He remained hospitalized and was being held in lieu of $5.2 million bail.

Manhattan US Attorney Preet Bharara said the feds, too, would put together a “comprehensive and thorough” set of charges in the multiple bombing incidents.

FBI Assistant Director William Sweeney said there was “no indication” Rahami was part of “a cell operating in the area or in the city.”

Testing of the unexploded Chelsea bomb revealed it contained the highly explosive compound HMTD, law enforcement sources said.

Instructions for making HMTD can be found online, with one site describing it as “EXCEEDINGLY unstable and dangerous.”

Rahami was first identified as a suspect through the old-school, mobile flip phones that were used as triggering devices on the bombs planted in Chelsea and at the Jersey Shore, sources said.

Rahami bought the cellphones in his own name last year at a Family Dollar store in Perth Amboy, NJ, according to the sources.

“He was dumb,” one law enforcement source said.

A fingerprint lifted from the failed Chelsea bomb was also matched to prints taken when Rahami became a naturalized US citizen, sources said.

Rahami’s brother and sister were among three men and two women who were stopped by the feds Sunday night while traveling through Brooklyn in a car tied to Rahami. They were all released without charges after questioning, sources said.

At least one of Rahami’s relatives was carrying a passport and a plane ticket, and was suspected of being en route to JFK Airport to flee the country for fear of being arrested, the sources said.

Rahami’s sister cooperated with the feds during questioning.

Additional reporting by Philip Messing and Jamie Schram