Shortly before he died in 1970, the Egyptian president Gamal Abdel Nasser said: "I rather like Gaddafi. He reminds me of myself when I was that age." As a teenager growing up in the desert outside Sirte, Gaddafi had been an avid listener to Nasser's inflammatory Arab nationalist broadcasts on Radio Cairo. His school had even expelled him for organising a student strike in support of the Egyptian leader. Here was the "leader of the Arabs", who had humiliated the old colonial powers during Suez and brought the promise of unity to the region, giving his blessing. To the young colonel, still not 30, there could have been no greater compliment.
Gaddafi seemed worthy of the older man's mantle when he came to power in Libya on 1 September 1969, deposing the weak, pro-western king Idris while the monarch was receiving medical treatment abroad. By the end of 1970, he had expelled between 15,000 and 25,000 of the despised Italians who had occupied Libya from 1911-41, removed the US and British military bases, and turned Tripoli's Catholic cathedral into the Gamal Abdel Nasser Mosque.
Forty years on, Gaddafi is the object of international vilification once again. Yet America's fury at the Lockerbie bomber's triumphant repatriation does not change the fact that the Libyan leader is now a friend of the west. He has held meetings with Tony Blair and Gordon Brown, and Silvio Berlusconi greeted him with a warm embrace when his plane touched down at Ciampino Airport in Rome in June. The former "mad dog of the Middle East", as Ronald Reagan called him, is even due to address the UN General Assembly in New York on 23 September. He has stopped offering sanctuary to and sponsoring terrorists, and traded his WMD programme for the normalisation of relations with the west.
None of this would have been conceivable during Gaddafi's early years in power. By the late 1960s, oil revenues were rapidly increasing - Libya overtook Kuwait as the world's fifth-largest exporter in 1969 - and Gaddafi played an important role in the 1973-74 oil crisis in which Opec cut production and raised prices, by leading the embargo on shipments to the US. At the same time as making good on his promises to provide free education and health care (as well as subsidised housing) for Libya's small population, he could back his ambition for regional hegemony with money, providing subsidies to Egypt and to others he saw as allies in the fight against Israel.
But Gaddafi did not limit his aid to Israel's enemies. Over time, it seemed any group that styled itself as a freedom movement could call on the Libyan state purse, from the IRA to the Moro National Liberation Front in the Philippines. Although his dreams of a pan-Arab merger with Tunisia, Egypt and Syria failed, Gaddafi's influence was felt far and wide. This frequently alarmed his neighbours, as did his erratic behaviour. In 1973, for instance, the QEII set sail from Southampton to Haifa full of Jewish passengers celebrating the 25th anniversary of the State of Israel. According to Nasser's successor Anwar al-Sadat, Gaddafi ordered an Egyptian submarine temporarily under his command to torpedo the liner: a directive countermanded only when Sadat ordered the sub to return to base in Alexandria.
Those who have met the "Brother Leader and Guide of the Revolution" over the decades describe him as "dramatic", "charismatic", "camp" (a television reporter who interviewed him in the 1970s told me he was convinced Gaddafi was wearing eyeliner) and always "unpredictable". He surrounds himself with female bodyguards, and broke wind noisily throughout an interview with the BBC's John Simpson. In March, he stormed out of an Arab summit in Qatar, declaring himself "the dean of the Arab rulers, the king of kings of Africa and the imam of all Muslims". Such behaviour can, but should not, obscure the reality that he presided over a police state that dealt brutally with anyone perceived to pose a threat. By 1975, Sadat was already describing him as "100 per cent sick and possessed by the devil".
But for all Gaddafi's rashness during this decade (he also launched abortive invasions of Chad in 1972 and 1980), initially at least the west gave the young colonel's new regime the green light. "We thought he was a bit left-wing," says a British source, "but not too bad, and that we could deal with him." The US even supplied him with intelligence support. Very soon after the coup that brought him to power, the CIA warned him of a plot within the Revolutionary Command Council, Libya's supreme authority, allowing him to arrest and imprison the ringleaders. News travelled, and Gaddafi gained a reputation in the region for enjoying America's favour. Although this had mostly evaporated by the end of the decade, Billy Carter, brother of the US president Jimmy Carter, still attended celebrations marking the tenth anniversary of Gaddafi's accession on 1 September 1979. In one of the many embarrassments he caused his brother, it was later revealed that Billy had received a $220,000 loan from the Libyan government.
The change was decisive once Ronald Reagan entered the Oval Office in 1981. That August, the US air force shot down two Libyan fighter planes in disputed waters in the Mediterranean. Reagan ordered US citizens to leave the country and refused US passport holders permission to travel there. By the end of the year, his administration was claiming that Libya had plans to assassinate the president and, if that failed, would target other senior officials such as the vice-president George H W Bush, the secretary of state Al Haig and the defence secretary Caspar Weinberger.
After four more years of skirmishes and ineffective sanctions, Reagan seized on a specific incident that he felt could justify a forceful strike on the Libyan regime: the bombing in April 1986 of a West Berlin disco packed with off-duty US servicemen. The US reprisal, in which Gaddafi's adopted daughter Hanna died, was controversial. There were suggestions - since given more credence - that Syria or Iran was behind the disco bombings. No European ally apart from Britain would give permission to the US to use its bases to launch the attack. Today, the Tory MP Daniel Kawczynski, chairman of the parliamentary all-party Libya group and author of a forthcoming biography of Gaddafi, says: "More questions should have been asked in parliament. We were rather gung-ho in supporting the attack."
As far as Britain was concerned, two incidents confirmed Gaddafi as the leader of a terrorist state: the fatal shooting of PC Yvonne Fletcher by a gunman inside the Libyan embassy in London in 1984, and the 1988 downing of the Pan Am jet at Lockerbie. These continue to be the main stumbling blocks to Gaddafi's final rehabilitation in the eyes of the west, as the international row over the repatriation from a Scottish prison of Abdelbaset Ali al-Megrahi has demonstrated. "The man who shot PC Yvonne Fletcher has been identified in Tripoli," says Kawczynski. "For us to let them have al-Megrahi without insisting on a statement about her is ludicrous." The Tory MP is also working with the Northern Irish Democratic Unionist Party to try to secure compensation for the victims of Libyan-funded IRA atrocities. He says he has repeatedly raised these issues with government ministers, but has been rebuffed. "'Don't rock the boat,' was what one of them said to me."
The story of how the "mad dog" came in from the cold goes back to the 1990s, when Kofi Annan and Nelson Mandela persuaded the Libyan leader that the two Lockerbie suspects should stand trial (al-Megrahi's co-defendant was acquitted). The UN immediately suspended sanctions it had imposed in 1992 and 1993. When Gaddafi was quick to condemn the attacks of 11 September 2001 as acts of terrorism, urging Libyans to donate blood for use by American victims, it seemed another remarkable volte-face by a man who would once have been expected to revel in US misfortune.
In fact, it was a sign that Gaddafi was never the irrational maverick some liked to say he was. Sanctions had hit the Libyan economy hard, depriving the country of the specialists and the markets it needed to exploit its oil wealth; and two other factors had left him short of allies. As the diplomat and Middle East specialist Sir Mark Allen, who was one of the UK's negotiators in the talks that led to Britain's rapprochement with Libya, writes in his book, Arabs: "At the end of the cold war, the Arab left was stranded . . . The region was retuning . . . The reference points were not left or right, monarchical tradition or the promises of socialism, but fidelity to the example of the early Muslim community."
After Egypt and Israel made peace at Camp David, Gaddafi turned ever closer to the Soviet Union, which stationed thousands of military advisers inhis country and from which he bought billions of dollars of arms. But once the USSR collapsed, says Oliver Miles, a former British ambassador to Libya, "he saw that if Uncle Sam was going to give him a kick, there was no one there to protect him". Nor was it conceivable that he could embrace the Islamists who, in fact, posed a threat to his rule. "He was deeply concerned about the threat from al-Qaeda," says Mike O'Brien, who as a Foreign Office minister was the first member of a British government to meet Gaddafi in 2002. "He had always promoted a more secularist, nationalist agenda."
He had set out his views at great length during his first decade in power, in the three volumes of his Green Book. His "Third Universal Theory" supposedly combined Islam with socialism - though the loose structure he presided over, which allowed for relatively free discussion by his associates before the leader took the final decision and retired to his tent in the desert, could be viewed as owing just as much to Arab, tribal forms of decision-making. Yet however one views Gaddafi's philosophy, he has long set his face against the Islamists, and he acted against ex-mujahedin fighters returning from Afghanistan in the mid-1990s when other Arab states welcomed them home. Indeed, Gaddafi was the first leader to call for an international arrest warrant for Osama Bin Laden in 1998.
Once Gaddafi took the step to open up and dismantle his WMD programme, and then agree compensation for victims of Lockerbie, the way was open for the international community to welcome Libya back. Gaddafi's son and possible heir, Saif, is clear about the path Libya is now taking. "The future is with more liberalism, more freedom, with democracy," he said in an interview with Time magazine. "This is the evolution of the entire world, and you either go with it or be left behind."
O'Brien, for one, is convinced. "Gaddafi is an intelligent guy who has been in control for 40 years," he says. "He realised that the only way to extradite himself from his difficulties was to use Libya's oil and gas wealth. This was realpolitik. He recognises that the world has changed and that he has to change with it."
For those who believe the west made a disastrous mistake in opposing the wave of nationalist politicians who came to power in the Middle East from the 1950s onwards, there is an irony. Gaddafi is the last of that generation, and while others who cloaked themselves in the rhetoric of Nasser have fallen, failed or died, it is the young man once praised by the Egyptian president who now appears to be becoming the kind of Arab leader with whom we can, and with whom we wish, to do business.