His targets were chosen at random, but the aims of the killer were very deliberate
Tunisian regime has been quick to use the shootings as an excuse to crack down on freedom
Just three months after the shooting of 22 holidaymakers in the Bardo museum in Tunis, the country has been attacked again. On Friday, a gunman opened fire and killed at least 38 people on the beaches of Sousse, on the northern Mediterranean coast.
Most of the victims were foreign tourists, making this both a human tragedy and a profound challenge to Tunisia's fledgling democracy.
This attack is the worst terrorist incident in the country's history and it means that Tunisia's vital tourism industry will now be gravely weakened. The once-hopeful democratic transition of the country after the Arab Spring is also in danger, with growing calls for a tougher security policy.
The attacker opened fire on the beaches of two resort hotels in the main destination for western package tourists in Tunisia.
Tunisia's tourism minister, Salma Elloumi, called the attack a catastrophe and admitted that there was no way to ensure "zero risk". According to one assessment, revenue from the tourism industry and related economic sectors accounted for up to 14.9pc of the Tunisian economy in 2014. Even if the tourist industry had begun to recover four years after the Arab uprisings of 2011, the Bardo shooting in March and this latest attack in Sousse will surely start a long-term crisis.
But despite the record loss of life, the latest attack was not entirely unprecedented.
Sousse has been targeted before, apparently because it attracts so many western tourists. Hotels there were bombed in 1987 at the height of a clash between the Tunisian regime and the Islamist movement in the country.
Then in October 2013 a suicide bomber killed himself on the beach outside a downtown Sousse hotel in a failed terror attack.
Sousse is not just a tourist destination, it has played an important role in the country's history, producing many of the administrative elite who have run Tunisia since independence in 1956. Former president Zine al-Abidine Ben Ali was from Hammam-Sousse, a northern suburb close to the scene of the attacks. The city has a secular middle class, but also significant support for the moderate Islamist movement Ennahda - I spent a year in the city interviewing Ennahda activists on the history and evolution of their movement.
Sousse also has large, impoverished suburbs, filled with Tunisians who have travelled from the poorer towns and villages of the interior in search of work. As in other poor urban areas, there has been significant Salafist radicalisation here, even before the 2011 uprising.
There is no certainty yet who was behind the latest attacks or whether the attacker was from Sousse. But the shootings were probably the work either of an Islamic State supporter or of a smaller Tunisian militant group, Uqba ibn Nafi, which is linked to al-Qaeda in the Islamic Maghreb and which has been fighting an insurgent campaign against the military and security forces since late 2012.
There could be a process of "outbidding" under way between these two groups, which could well escalate the violence inside Tunisia. There are also suggestions that Isil has been trying to step up its activities inside the country as part of its wider campaign of violence across the region.
The decisions the Tunisian government make in the coming weeks will determine the fate of country's already fragile transition to democracy. Although the Ben Ali regime was toppled in a popular uprising in January 2011, many of the political and economic interests of the former regime remain intact and the judiciary and security forces remain largely unreformed. The "deep state" is still at work.
There has been a knee-jerk tendency to resort to authoritarian measures in the name of security and stability in the years since the uprising and a backslide into the state's old ways remains a real risk for Tunisia. That much was made clear when, after the attack, the president, Beji Caid Essebsi, travelled to Sousse to meet the survivors and promised that "painful but necessary" measures would follow.
Mohsen Marzouk, an adviser to Essebsi and now the head of the Nidaa Tounes party, which leads the coalition government, called for the creation of new private sector security forces, and said: "We are at war and we must apply the law firmly." Already, the government has drafted a bill that would jail any Tunisian found to have "denigrated" the armed forces.
Human Rights Watch warned that the bill did not meet international human rights standards and is also concerned about a new law on judicial reform that fails to give the judiciary sufficient independence.
Other politicians have proposed a national congress to discuss a strategy to confront the terrorist challenge and popular marches against terrorist violence. But there is still no clear strategy to confront the worsening security crisis and declining economic situation.
The damage done to the tourism industry follows waves of strikes that have affected the important phosphate mining industry in the south. There is hope that Tunisia will yet overcome these challenges, just as it has overcome many others since 2011. Politicians have always found a way to negotiate consensus solutions every time a crisis has threatened this hopeful and extraordinary transition to democracy, but that task is becoming ever harder.
Rory McCarthy is DPhil candidate in oriental studies at Oxford and a former Middle East correspondent for the Guardian