How Iran fell out with the West
It was October 1979, a full eight months after Shah Mohammad Reza Pahlavi had fled the Iranian revolution, leaving Ayatollah Ruhollah Khomeini to fly back from France and assume power as the Supreme Leader of the Islamic Republic that was declared on 1 April.
A senior CIA officer, George Cave, flew to Tehran, and held two meetings with the interim Deputy Prime Minister, Abbas Emir Entezam, and the Foreign Minister, Ebrahim Yazdi.
He passed them a warning, backed by top-secret hard intelligence, that Saddam Hussein in neighbouring Iraq was quietly and methodically preparing to invade Iran.
By that stage, the Americans had given up any hope of reversing the revolution and restoring the shah to power.
But they still hoped to establish links with the new order in Tehran, encourage moderate elements within the emerging establishment, and above all, to retain Iran as a listening post for monitoring developments across the border in the Soviet Union, the main US preoccupation at the time.
None of that was to be.
'The Great Satan'
Just two days after Mr Cave's second meeting that October, President Jimmy Carter bowed to irresistible pressure to allow the mortally ill shah to seek treatment in New York for his cancer.
Furious demonstrations erupted in Tehran, culminating in the storming of the US embassy on 4 November and the seizing of 61 diplomats and embassy staff, triggering a hostage drama that was to last 444 days and plunge the two countries into an obsessive crisis in relations from which they have yet to emerge, despite the possibilities raised by the nuclear deal agreed on Tuesday.
For the Iranian revolutionaries, the US was, and still is, the Great Satan.
The same shah whom Washington was now trying to shun had been lifted to power in a 1953 coup engineered by the CIA and the British, displacing the elected Prime Minister, Mohammad Mossadegh, who had had the temerity to nationalise the Iranian oil industry.
Throughout the shah's rule, with its trappings of ostentatious megalomania and over-rapid Westernisation, he and his hated and feared secret police, Savak, enjoyed strong US support as he became more and more disconnected from the masses.
Just two years before the revolution, President Carter stood at the shah's side on New Year's Eve and said: "Iran, because of the great leadership of the shah, is an island of stability."
The embassy seizure was a watershed moment, not only in US-Iranian relations, but also in the course of the Iranian revolution, whose multi-stranded nature was reflected in the variegated make-up of the interim government led by Prime Minister Mehdi Bazargan.
Ayatollah Khomeini backed the militant "students" who had taken over the embassy - including, it has been alleged, a certain Mahmoud Ahmadinejad, who was to become president in 2005. The Bazargan government had no option but to resign as the Islamist trend strengthened its grip.
Exporting the revolution
The following year, true to the American warning, Saddam's Iraq invaded western Iran, triggering the 20th Century's longest war. It only ended in 1988 when Ayatollah Khomeini "drank the poisoned chalice", as he put it, and accepted a peace deal.
The war provided cover for the Islamists to suppress all domestic competition, including the communists and Mujahideen-e Khalq (People's Mujahideen).
As the war ground on, Iran's Islamic militants tried increasingly to export their revolution.
In Lebanon, for example, they were instrumental, with their strategic Syrian partners, in setting up the Shia movement Hezbollah after the 1982 Israeli invasion.
They were widely seen, and blamed by the Americans, as being behind the deadly bomb attacks on the US embassy and US Marine Corps barracks in Beirut in 1983, and the seizure of American hostages in Lebanon later in the 1980s.
Having tried to curry favour with post-shah Iran by tipping Tehran off about the planned Iraqi invasion, the US switched sides and began providing Iraq with intelligence on Iranian troop movements, with the strategic aim of containing the Iranian revolution.
But the two sides remained capable of extraordinary pragmatism in their less public dealings.
In November 1986, an obscure Lebanese magazine, al-Shiraa, came out with a complicated and incredible tale of American officials turning up in Tehran with arms deliveries to heavily-embargoed Iran in exchange for the freeing of US hostages in Lebanon.
It turned out to be true. The US was selling arms to Iran, via Israel of all people, to secure the hostages' freedom. The proceeds were used to fund clandestinely the Contra rebels in Nicaragua.
But on the official level, relations remained bitterly hostile, envenomed by such incidents as the shooting-down by the USS Vincennes of an Iranian civilian airliner over the Gulf in 1988, with the loss of 290 lives.
The landslide election of the genial moderate Mohammad Khatami as president in 1997, with his talk of a "dialogue among civilisations", finally brought hope of a breakthrough, coinciding as it did with the presidency of Bill Clinton.
But Mr Khatami was stymied by hardliners at every step, and the moment was lost.
The arrival at the White House of George W Bush in 2001 brought hardliners into the ascendant in the US too.
After 9/11, Iran co-operated with the US against their mutual Taliban foes in neighbouring Afghanistan.
Khatami was rewarded by having his country dubbed part of the "Axis of Evil" by Mr Bush in January 2002.
Later that year, Iran's secret nuclear programme suddenly broke into the limelight, revealed by exiles from the Mujahideen-e Khalq.
That provided a chance for what Europe called "constructive engagement" with Iran.
British, French and German foreign ministers flew repeatedly to Tehran seeking an enrichment freeze. The Americans held back.
Iran did agree in late 2004 to halt enrichment. But once again the hardliners intervened.
Mahmoud Ahmadinejad was elected president in 2005, and enrichment resumed the following year.
The man then ultimately in charge of Iran's nuclear file, Supreme National Security Council chief Hassan Rouhani, a pragmatic centrist, learned two things from that failed episode and everything that preceded it.
Firstly, for it to work, Iran had to engage with the Americans, not just Europe. Secondly, the hard-liners in Tehran had to be on board, or they would wreck any deal.
Mr Rouhani had to wait another eight years to try his hand again - two terms for the tough, doctrinaire Mr Ahmadinejad.
Mr Rouhani's election as president in 2013, after he advocated a resolution of Iran's nuclear crisis and dialogue with the West, triggered scenes of elation similar to those attending the arrival of Mr Khatami in 1997.
Mr Ahmadinejad and his policies had brought the country to its knees.
His populist economic moves had bankrupted the public coffers. The resumption of nuclear activity had triggered international sanctions that were biting deeply. On top of that, the slump in world oil prices slashed Iran's oil revenues drastically.
The danger of desperate popular upheavals was highlighted by the eruption of implacable revolts in many Arab countries, including Iran's ally Syria.
Another outburst in Iran might be less easy to suppress than the "Green Revolution" that followed Ahmadinejad's disputed re-election in 2009.
Something had to be done. And Mr Rouhani was the man to do it. All the elements were finally in place for him to come into his own.
That it was not just his own work, but was something the system needed, was made clear by the fact that secret talks with the Americans had already started in Oman in 2012, a year before Rouhani's election.
In the Middle East, politics is often not just the art of the possible, but of the necessary.
In nearly two years of tortuous negotiations, Mr Rouhani and his skilled and charming Foreign Minister Mohammad Javad Zarif played a blinder.
Above all, they ensured that the Supreme Leader, Ayatollah Ali Khamenei, was kept fully engaged and persuaded of the need for the many concessions Iran had to make.
This ensured that the deal when it was announced was politically unassailable in Tehran.
It was greeted ecstatically by liberals and reformists, and with sullen resignation by hardliners.
Everybody agrees that it was "historic", though in different ways.
Mr Rouhani himself said it was "a historic deal of which Iranians will be proud for generations".
Iran's ally Bashar al-Assad in Syria said it was a "historic turning-point for Iran, the region and the world." For Benyamin Netanyahu in Israel, it was "a stunning historical mistake".
Just how far-reaching it will turn out to be, remains to be seen.
Potentially, it could mark a strategic sea-change in the affairs of a dramatically turbulent region. That is what Iran's friends hope, and its enemies fear.
Although the text of the deal is strictly confined to the nuclear issue, circles on both sides seem to think that it will provide the basis for a gradual warming of US-Iranian ties, and the exploration of co-operation on stabilising the region's many conflicts where Iran is a key player, directly or by proxy, including those in Iraq, Syria, Lebanon, and Yemen.
In Iraq, disgruntled Sunnis have for at least a year been referring to "the American-Iranian alliance" because of the convergence of interests there.
US jets have several times bombed in support of Shia militias on the ground, where only a few years ago they were kidnapping and killing American soldiers.
In Lebanon, Western diplomats have for several years made no secret of the fact that they see Iranian-backed Hezbollah as a stabilising factor, given the more deadly threat they perceive from radical Sunni Islam, especially the self-styled Islamic State.
There are many imponderables, and many pitfalls lurking on the road ahead.
Hardliners on both sides will be doing their best to derail the deal.
Mr Rouhani and Mr Zarif may be hailed as heroes by many in Tehran today, but knives are certainly being sharpened in the background.
Like his predecessor in 1988, Ayatollah Khamenei drank his poisoned chalice because he had to, not because he wanted to.
He and the hardliners will above all be watching out for the social, cultural and political consequences of opening up to the outside world.
Rapid movement should not be expected. Ideally, they would like to give just enough to obtain sanctions relief, but to limit the impact and depth of the move.
It should also be borne in mind that the Islamic Republic is not a monolithic power system. It is a collection of vying power circles and trends which sometimes come together, as they have had to for this deal, but are more often sharply at odds.
Much power is in the hands of the hardline Revolutionary Guards, whose influence extends not just to politics and Iran's military involvements abroad in Iraq, Syria, Lebanon and elsewhere, but also deep into many parts of the economic system.
Will they end up being partners with the US and its allies in a regional drive against IS? Or, as the Saudis and others fear, will they simply use the funds eventually released under the deal to step up their activities abroad?
That is only one of many questions that cannot yet be answered.
But a lot went into the deal, from both sides. And until further notice, yes, it is historic.