Last Thursday's terrorist attack in Barcelona bore an obvious resemblance to ones in Belgium, France, Germany, Sweden and Britain over the past few years. Nevertheless, there are distinctive Spanish and north African dimensions to this latest case.
Hours after the attack, the Islamic State in Iraq and Syria (ISIS) sought to take credit, but it has made unfounded claims of responsibility before. Caution is essential before jumping to conclusions about a supposed internationally masterminded outrage.
This was the lesson from the probe into the Madrid train bombings of 2004 , which killed 191 people and wounded over 1,800. At first, Spain's ruling conservative party tried to pin the blame on Eta, the Basque separatists - a claim for which there was not a shred of proof. Then, inquiries centred on Al-Qaeda, which was behind the 2001 attacks in the United States. At a trial that ended in 2007, those convicted of the Madrid bombings included a group of young men, mainly north Africans, whom prosecutors identified as a cell of Islamists. But no direct link to al-Qaeda was established.
What is clear, however, is that Islamist militants with north African roots, including Moroccans and Europeans of Moroccan origin, have come to pose a threat to Spain since 2004. In November last year, Spanish police arrested a Moroccan living near Madrid on suspicion of planning a "lone wolf" attack.
The roots of this problem lie in the history of Spain and north Africa. For Islamist extremists, Spain is al-Andalus, the land that Islam conquered in 711, from which it was slowly driven out until its complete expulsion as a political entity in 1492, and which is a legitimate target for revenge or reconquest. The extremist website that seized the minds of the Madrid bombers urged attacks on Spain.
Most of Morocco fell under French control in the early 20th century, but part of the sultanate became a Spanish protectorate. During the 1920s, Spain's armed forces used chemical weapons to quell a rebellion in the Rif region. As was the case with similar interwar episodes involving British and Italian colonial armies, Spain covered up its chemical warfare for decades.
Spain pulled out of Morocco in 1956, but still holds two enclaves on the coast, Ceuta and Melilla. Relations between the two nations are correct rather than warm. Spain appreciates the moderate brand of Islam that King Mohammed VI, Morocco's ruler, favours. However, the Barcelona attack occurred at a delicate moment, in regard both to Europe's migrant and refugee crisis and to internal conditions in Morocco.
Irregular migration in the Mediterranean is moving from the Libya-to-Italy crossing to the shorter route from Morocco to Spain. The International Organisation for Migration says 11,849 irregular migrants entered Spain from Jan 1 to Aug 16, most of them by sea but some by other methods, such as breaching security controls at Ceuta. The figures suggest that, by the year end, the total number of undocumented migrants entering Spain will be significantly higher than last year's 13,246.
Spain is expected to see more arrivals largely because of chronic insecurity in some sub-Saharan countries. Another factor is the European Union's stepped-up financial support for the government of Niger. This has helped choke people-smuggling routes that pass through northern Niger and the Libyan Desert to Libya's coast. Together with Italy's clampdown on charities that run rescue missions in the central Mediterranean, these pressures are causing criminal gangs to focus on the Spanish route.
In some respects, this is a familiar challenge for Spain, which received almost 40,000 irregular migrants in 2006. At that time, it cooperated with Morocco and sub-Saharan countries to cut the flow of people. By and large, that worked. A decade on, the context has changed radically because of the Arab spring, Libya's breakdown and the political unrest that has gripped Morocco since October last year.
Irregular migration into Spain will intertwine with instability in Morocco, just as Syria's civil war generated refugee flows into Greece, and just as anarchic violence expanded Libya's role as a route for migrants to cross into Italy. The Barcelona attack highlights the need for reform in Morocco and EU-wide action in Spain's unfolding migration crisis.