#!/bin/bash

if type update-alternatives 2>/dev/null >&1; then
    # Remove previous link if it doesn't use update-alternatives
    if [ -L '/usr/bin/thorium' -a -e '/usr/bin/thorium' -a "`readlink '/usr/bin/thorium'`" != '/etc/alternatives/thorium' ]; then
        rm -f '/usr/bin/thorium'
    fi
    update-alternatives --install '/usr/bin/thorium' 'thorium' '/opt/Thorium/thorium' 100 || ln -sf '/opt/Thorium/thorium' '/usr/bin/thorium'
else
    ln -sf '/opt/Thorium/thorium' '/usr/bin/thorium'
fi

# Check if user namespaces are supported by the kernel and working with a quick test:
if ! { [[ -L /proc/self/ns/user ]] && unshare --user true; }; then
    # Use SUID chrome-sandbox only on systems without user namespaces:
    chmod 4755 '/opt/Thorium/chrome-sandbox' || true
else
    chmod 0755 '/opt/Thorium/chrome-sandbox' || true
fi

if hash update-mime-database 2>/dev/null; then
    update-mime-database /usr/share/mime || true
fi

if hash update-desktop-database 2>/dev/null; then
    update-desktop-database /usr/share/applications || true
fi
