DAKAR, Nov 2 (Reuters) - In the build-up to this past week’s mass protests in Burkina Faso that ended Blaise Compaore’s 27-year rule, statesmen from French President Francois Hollande to former U.N. Secretary-General Kofi Annan sent him messages with increasing urgency.

The meaning was clear: step aside with reputation intact and a high-profile international job, or risk an undignified exit.

But Compaore stood firm. Even with hundreds of thousands protesting his plan to rejig the constitution to extend his rule, he still hoped to outmanoeuvre his rivals one more time.

The former soldier had survived many attempts to unseat him since he seized power in a 1987 coup that killed his former brother in arms Thomas Sankara, a leftwing hero.

In doing so, Compaore gradually reinvented himself from a notorious backer of rebel groups and ally of the late Libyan leader Muammar Gaddafi to a wily, regional peace-broker.

In the end, however, the man popularly known as ‘Beautiful Blaise’ spectacularly misjudged the people he had ruled with a mixture of democracy and repression for nearly three decades.

“He played for many years and he won. This was one game too many and he lost,” Gilles Yabi, an independent West African political analyst, told Reuters. “He miscalculated and didn’t think there would be this level of protest.”

Realising his mistake too late, Compaore withdrew on Thursday his plan to change Burkina Faso’s 1991 constitution so he could stand for re-election. But protests intensified and he had no choice but to flee the Kosyam presidential palace and seek haven in neighbouring Ivory Coast, governed by his firm ally Alassane Ouattara.

His fall revealed the gulf between those he ruled and Western governments, who saw him as a useful ally against Islamist forces in the turbulent Sahel.

Before the celebratory horns and whistles had died down, the military had stepped in. The army chief of staff announced he had taken control, only to be ousted in his turn by a senior figure in the presidential guard hours later.

Despite condemnation of the military’s intervention from the opposition, the United States and the regional Africa Union bloc, Compaore’s fall was still hailed as a warning to several longtime African presidents mulling moves to stay in power.

“If Blaise had been allowed to stay it would have sent a message to the old club of African leaders that they can do whatever they want,” said Rinaldo Depagne, head of the West Africa Project for the International Crisis Group (ICG). “Now they see that they have to listen to the streets.”

IN SANKARA’S SHADOW

The death of Sankara - whom Compaore had helped to sweep to power in a 1983 leftwing revolution - cast a shadow upon his regime. Compaore went on to win a series of elections, initially unopposed but then against an opposition that cried foul.

The 1998 murder of Norbert Zongo, a journalist who was investigating corruption and the mysterious death of the driver for Compaore’s brother Francois, underscored darker methods employed by the state apparatus to maintain authority.

“The country was peaceful but people have never forgotten the bloody episodes,” Yabi said.

In her farewell cable to Washington in 2009, outgoing U.S. ambassador Jeanine Jackson described a president taking the country in the right direction through hard work.

Others, who saw the Compaore machine from the inside, remember differently. “He managed things through intimidation, killings and corruption,” said one former official.

Compaore sailed to victory in the last presidential election in 2010 with 80 percent. The result masked anger over scant improvement in the lives of Burkina Faso’s 17 million people.

Perched on the Sahara’s southern rim, Burkina Faso has long been one of the least developed nations on earth. It is a cotton producer and has attracted several foreign gold miners. Aid funds, however, still cover 80 percent of government spending.

In 2011, soldiers mutinied over unpaid housing allowances. Angry students and ordinary people joined them, protesting at rising food prices, police brutality and lack of development.

Compaore survived and swiftly reshuffled his military. But it showed that rivals could capitalise on anger at inequality.

“A once-admired focus on grassroots development has faded, while a well-connected elite has grown prosperous,” said Paul Melly, associate fellow of the Africa Programme at Chatham House.

While most battled to eke out a living, the wealth on show in the Ouaga 2000 neighbourhood - home to sprawling, high-walled villas of Compaore’s inner circle - fuelled popular anger.

His decision to seek reelection exposed rifts in the regime, prompting defections in the ruling CDP party. Tapping into youth frustration, a rapper and a reggae singer set up Balai Citoyen, a movement at the heart of this past week’s protest.

The homes of government figures - including Compaore’s hated brother Francois - were looted. Protesters carried off furniture and goods as well as documents on the workings of the regime.

“DEAR BLAISE”

With few serious challengers during the last decade at home, Compaore thrived in his role as mediator in a turbulent region. In August, U.S. Secretary of State John Kerry praised Compaore’s role in advancing “regional peace and security”.

It was a remarkable turnaround for a man who spent years as a troublemaker, often as the intermediary for Gaddafi, and supported rebels in the 1990s in Liberia and Sierra Leone’s brutal civil wars.

“His government emboldened these groups and their abuses by facilitating arms transfers and in some cases, providing fighters safe haven,” said Corinne Dufka, senior researcher for Human Rights Watch in West Africa. “He later fashioned himself as a peacemaker, which was rather ironic.”

Alongside the public role of supporting U.S. and French counter-terrorism efforts, behind the scenes, senior advisors negotiated the release, often for multi-million dollar ransoms, of numerous Western hostages seized in the region.

His allies sought to warn him about the shifting public mood. In an Oct. 7 letter, addressed “Dear Blaise”, France’s Hollande appealed to him to take a decision that would make his nation a model of the region. In return, France offered to support Compaore if he wanted a job in international diplomacy.

But, seemingly fearing justice might catch up with him or merely unable to consider a life outside power, he pushed on.

“Blaise made a massive mistake. He lost touch with reality,” said ICG’s Depagne. (Additional reporting by Daniel Flynn in Dakar, John Irish in Paris and Joe Bavier in Abidjan; Editing by Daniel Flynn and Stephen Powell)