What exactly was toppled in Libya with the overthrow of Muammar Gadaffi? A dictator, or a working power structure? It was utterly predictable that military intervention would be a fiasco. Sadly the lessons of the Iraq war did not alert Nato leaders to the disastrous consequences of their punitive mission.

Western military strategists had, of course, identified the nerve centres they would need to target to bring down their enemy. They made detailed appraisals of Gaddafi’s defensive strongholds, his air bases, listening stations, electronic warfare and communications networks, tank regiments, ground troops, planning and command centres, logistics hubs, key infrastructure and administration. Everything.

But they failed to take account of the most essential consideration: the nature of the Arab-Berber world. And it’s this monumental oversight that holds the key to Libya’s current’s chaos, along with the chaos that has been tearing the Iraqi people apart for over a decade.

You cannot simply launch an attack on a country without any knowledge of the mindset or character of its inhabitants. You can destroy every tank and combat aircraft in its arsenal, wipe out its entire strategic networks, but if you don’t know what kind of people you’re dealing with, you are merely opening a Pandora’s box, and every idle peace operation embarked on later is doomed only to throw fuel on the fire.

To understand the Libyan tragedy we must first study the peculiarities of Arab-Berber culture. For centuries Libya languished on the sidelines, resisting the encroachment of the modern world and the perceived dangers of cosmopolitanism.

The Libyan people did not exist as a homogenous nation under one flag and sharing one common ideal. It was a collection of fiercely autonomous, proud and unruly tribes, suspicious of centralised rule (first there had been a substitute Ottoman regency, then a mandated principality, next a short-lived monarchy – the last king of Libya, Idris I, was Algerian), which they saw as a potential threat and to which they would only give allegiance to preserve their own independence.

The history of deeply hostile relationships between Libya’s ethnic groups is littered with violent raids, betrayals, unfulfilled vendettas and long-held frustrations carried like shameful injuries that have festered over the years as each generation is brought up to seek revenge for old sins. The terrible reality of the Libyan situation is precisely what Nato’s generals did not deem it necessary to know, dangerously choosing to ignore the unique combination of factors that make up the Libyan mindset. They failed to consider how Libyans would react to having a war thrust upon them.

You cannot simply launch an attack on a country without any knowledge of the mindset or character of its inhabitants

Gaddafi played a defining role in the rebuilding of the modern Libyan nation. By overthrowing the monarchy and declaring the Jamahiriya (a republic of the masses in which political power was to be passed to the people), the revolutionary army officer achieved what no sovereign before him had accomplished.

Born of the tribes and the outcasts, a wretched child destined for menial tasks and a lifetime of poverty, Gaddafi – thanks, in large part, to his humble roots – immediately won the adoration of the disadvantaged on the fringes of society and rallied the aggrieved and the rejected to his cause. But his greatest feat, after the coup d’etat, was absolutely remarkable: he succeeded in bringing together the intensely opposed ethnic groups of the north and south, who had always despised one another. To the casual western observer this might seem a basic achievement and of little import, but for an inveterate tribalist it is little short of a miracle.

For four decades Gaddafi acted as guarantor of the nation’s stability and a careful moderator between tribal leaders, reconciling warring parties and delicately handling the hangovers of the past that still awoke old demons from time to time. Gaddafi, as vigilant keeper of the flame, kept a weather eye open, heaping privileges on some and prestige on others in order to consolidate alliances and plaster over any cracks that threatened to appear.

‘Gaddafi played a defining role in the rebuilding of the modern Libyan nation … he achieved what no sovereign before him had accomplished.’ Photograph: Mahmud Turkia/AFP/Getty Images

A peerless orator, unparalleled expert in tribal psychology and extraordinary manipulator, he ostentatiously showered gifts on his allies while pitilessly crushing his doubters. Giving with one hand, torturing with the other, he kept the nation on steady footing, as if marching in a parade.

Yet this authority essentially hinged on the precarious support of the ethnic groups whom Gaddafi made dependent on him. Nothing is as unpredictable and dangerous as a tribe turning on you – such sudden revolts are the stuff of legend and the hallmark of the Maghreb and its colonial history.

By toppling Gaddafi, Nato interfered with the order of things. Once the personal guarantor of national unity had been lynched by his compatriots, the Libyan people were left to their own devices in an appalling state of upheaval, with no roadmap to guide them.

Through herd mentality, or pure atavism, the leaderless state was drawn back to its one familiar point of reference, the tribal system of its ancestors, and with it the full force of its legacy: a return to the hatred of the past, to intractable rivalries, violent raids and an unquenchable thirst for vengeance.

After civil war, pillaging, settling of scores, mass rape and destruction on a massive scale, each ethnic group has withdrawn to its own territory and demands autonomy. The bastion Gaddafi built is crumbling. Libyan unity is now no more than an old story, a fairytale no one believes in.

Each militia, each religious community, each ethnic group rebels against the other, every region is a minefield. And into this advanced decomposition other deathly winds are blowing: Islamic State the sirocco of the north; and al-Qaida, the southerly harmattan sweeping up from Niger and Mali.

The noose tightens around Libya as competing ideological and territorial claims are staked on it. Today, no region has been spared violence and mass criminality. NGOs and even the Red Crescent are unwelcome: peacekeepers are rebuffed, hospitals doomed to failure.

In the face of western bewilderment and inaction, the situation is on the verge of spreading throughout the whole of north Africa, turning neighbouring countries into powder kegs whose shock waves could destroy the stability of the entire Mediterranean, possibly even dragging the rest of Europe into a calamitous downward spiral.

It is becoming a matter of urgency that a unity government is imposed on Libya – and with sufficient backing that popular support for Isis and al-Qaida are cut off. Only then can lasting stability in the region be restored. Failure means it is inevitable that the breakdown of law and order will be exported to the rest of the world. A solution to the Libyan crisis, on the other hand, could bring peace to other Arab countries mired in chaos.

Is such a solution possible? I remain convinced it could be, if only the west were to put its mind – and its means – to achieving it.