post_install() {
    :
#!/bin/bash

ln -sf /opt/sing-box/sing-box /usr/bin/sing-box

if ! { [[ -L /proc/self/ns/user ]] && unshare --user true; }; then
    chmod 4755 /opt/sing-box/chrome-sandbox || true
else
    chmod 0755 /opt/sing-box/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

if [ -d /run/systemd/system ]; then
    systemctl daemon-reload
    if systemctl cat polkit-agent-helper.socket >/dev/null 2>&1; then
        systemctl start polkit-agent-helper.socket
    fi
    systemctl enable sing-box-daemon.service
    systemctl restart sing-box-daemon.service
else
    echo "systemd is not running, skipping the sing-box service startup"
fi

}
post_upgrade() {
    :
#!/bin/bash

ln -sf /opt/sing-box/sing-box /usr/bin/sing-box

if ! { [[ -L /proc/self/ns/user ]] && unshare --user true; }; then
    chmod 4755 /opt/sing-box/chrome-sandbox || true
else
    chmod 0755 /opt/sing-box/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

if [ -d /run/systemd/system ]; then
    systemctl daemon-reload
    if systemctl cat polkit-agent-helper.socket >/dev/null 2>&1; then
        systemctl start polkit-agent-helper.socket
    fi
    systemctl enable sing-box-daemon.service
    systemctl restart sing-box-daemon.service
else
    echo "systemd is not running, skipping the sing-box service startup"
fi

}
pre_remove() {
    :
#!/bin/bash

if [ -d /run/systemd/system ]; then
    systemctl disable --now sing-box-daemon.service || true
fi

}
post_remove() {
    :
#!/bin/bash

if type update-alternatives >/dev/null 2>&1; then
    update-alternatives --remove 'sing-box' '/opt/sing-box/sing-box'
else
    rm -f '/usr/bin/sing-box'
fi

APPARMOR_PROFILE_DEST='/etc/apparmor.d/sing-box'
if [ -f "$APPARMOR_PROFILE_DEST" ]; then
    if apparmor_status --enabled > /dev/null 2>&1; then
        if ! { [ -x '/usr/bin/ischroot' ] && /usr/bin/ischroot; } && hash apparmor_parser 2>/dev/null; then
            apparmor_parser --remove "$APPARMOR_PROFILE_DEST" || true
        fi
    fi
    rm -f "$APPARMOR_PROFILE_DEST"
fi

if [ -d /run/systemd/system ]; then
    systemctl daemon-reload || true
    systemctl reset-failed sing-box-daemon.service || true
fi
rm -f /run/sing-box.socket

}
