What's ailing Walgreen (WAG, news)?

The nation's largest pharmacy has run into a competitive wall that's hard to miss if you're a shopper or a shareholder.

First, many faithful customers whose families have shopped at the retailer for generations have learned over the past few months that they can no longer fill their prescriptions at Walgreen's -- at least, not if they want their insurance to cover it.

Second, Walgreen shareholders need a prescription for pain relief. Despite the stock market's recovery, Walgreen shares are down 24% since trouble began last summer.

What's the problem?

In short, behind the scenes there's a big-money battle playing out in the pharmacy world that could change where you get your prescriptions filled. It is reshaping who profits from the huge prescription drug industry. And Walgreen, a storied brand and part of Americana, is at the center of it.

Here's a look inside this the prescription drug fight, and how it might turn out for Walgreen -- which is, after all, the sort of iconic stock that investors often do well buying when down, because the name helps it bounce back.

image: Michael Brush

Michael Brush

The contestants, old and new

In one corner, you have Walgreen's, which has been filling prescriptions since 1901. Over more than a century, it has become one of America's favorite retailers.

In the other corner, you have the relatively new middlemen of meds known as pharmacy benefits managers, which got started in the 1980s. PBMs pool the health insurance needs of private companies, governments and unions to increase their bargaining power with drug companies and lower the cost of medications.

They also bargain with pharmacies to determine whom to invite into their networks -- and at what share of the proceeds. And one in particular, Express Scripts (ESRX), has been squeezing Walgreen.

Last summer, Express Scripts offered the chain a much-less-favorable revenue split than it was used to. Walgreen declined. Shortly after, things got worse for the drugstore chain when Express Scripts announced plans to merge with Medco Health Solutions (MHS). That's another large PBM, and another huge Walgreen partner.

Seeing the writing on the wall -- the potential loss of both Express Scripts and Medco customers when contracts with Walgreen eventually expired -- investors fled Walgreen stock over the summer. From a peak above $45 in May, shares traded into the low $30s.

After that, the stock drifted in the $32 to $34 range as investors braced for what was about to happen: the expiration of Express Scripts' contracts with Walgreen at the start of 2012.

This year, Express Scripts has been shifting customers to other pharmacies -- which means they can no longer have their prescriptions filled at Walgreen's if they want insurance to cover them. If the Medco merger goes through, the same may well happen with Medco customers.

This battle has scared investors and Wall Street analysts away from Walgreen stock. But that makes it an intriguingly inexpensive -- if somewhat risky -- contrarian play. The bottom line is that even though things look bleak, Walgreen has the strength to fight back.

"Walgreen is a top pick," says Andrew Wolf of BB&T Capital Markets, one of the few Wall Street analysts with a "buy" rating on Walgreen stock. Walgreen stock trades at a 20% to 30% discount to CVS Caremark (CVS) and Rite Aid (RAD) by some valuation metrics, he says. "Most of the bad stuff is priced in. If anything good happens, the stock is going up." Wolf has a 12-month price target of $46 on Walgreen stock, which recently traded for about $34.

How Walgreen could win

What could go right from here? Analysts cite three things.

1. Walgreen uses its clout to fight back.

Walgreen still has many strengths, starting with decades of customer loyalty and a huge presence on the retail scene. It fills 21% of prescriptions in the U.S. It is such a fixture in some places, including its home turf of Chicago, that customers refer to it as "my Walgreen's."

Click here to become a fan of MSN Money on Facebook

Walgreen could well draw on that customer loyalty to turn the tables. If enough Walgreen's customers complain about being forced to go elsewhere, and the employers who work with Express Scripts and Medco sound off, too, the PBMs would find themselves in a tough spot.

Thus far, this isn't happening. Since Walgreen and Express Scripts parted ways at the start of the year, Walgreen has retained far fewer pharmacy customers than it said it might.