#!/bin/sh
set -e


handle_triggers () {
    local trigger
    local dirs

    for trigger in "$@"; do
        if ! [ -d $trigger ]; then
            continue
        fi
        case $trigger in
          /usr/share/glib-2.0/schemas)
            # This is triggered everytime an application installs a
            # GSettings schema
            "/usr/lib/x86_64-linux-gnu/glib-2.0/glib-compile-schemas" /usr/share/glib-2.0/schemas || true
            ;;

          "/usr/lib/x86_64-linux-gnu/gio/modules")
            # This is triggered everytime an application installs a GIO
            # module into /usr/lib/x86_64-linux-gnu/gio/modules

            "/usr/lib/x86_64-linux-gnu/glib-2.0/gio-querymodules" "/usr/lib/x86_64-linux-gnu/gio/modules" || true
            ;;
        esac
    done
}

if [ "$1" = triggered ]; then
    handle_triggers $2
    exit 0
fi



# This is shipped in the .deb (see debian/libglib2.0-0.dirs) but would
# be removed if empty during upgrade by old versions of the postrm (#987913).
# This workaround can be removed after Debian 12 and Ubuntu 22.04 are released.
install -d "/usr/lib/x86_64-linux-gnu/gio/modules"

"/usr/lib/x86_64-linux-gnu/glib-2.0/glib-compile-schemas" /usr/share/glib-2.0/schemas || true
"/usr/lib/x86_64-linux-gnu/glib-2.0/gio-querymodules" "/usr/lib/x86_64-linux-gnu/gio/modules" || true

# Clean up pre-multiarch giomodule.cache.
# This workaround can be removed after Debian 12 and Ubuntu 22.04 are released.
if [ -d /usr/lib/gio/modules ]; then
    rm -f /usr/lib/gio/modules/giomodule.cache
    rmdir -p --ignore-fail-on-non-empty /usr/lib/gio/modules
fi

# vim:set sw=4 sts=4 et:
