{{- if eq .host.distro.family "linux" -}}
#!/usr/bin/env bash

{{ includeTemplate "universal/profile" }}
{{ includeTemplate "universal/logg" }}

### Backup system settings
DCONF_TMP="$(mktemp)"
dconf dump / > "$DCONF_TMP"
logg info 'Backed up system dconf settings to '"$DCONF_TMP"

### Reset system settings / load saved configurations from ~/.config/dconf/settings
if [ -d "$XDG_CONFIG_HOME/dconf/settings" ]; then
    find "$XDG_CONFIG_HOME/dconf/settings" -mindepth 1 -maxdepth 1 -type f | while read DCONF_CONFIG_FILE; do
        if [ "$DEBUG_MODE" == 'true' ]; then
            logg info 'Dconf configuration file:'
            echo "$DCONF_CONFIG_FILE"
        fi
        DCONF_SETTINGS_ID="/$(basename "$DCONF_CONFIG_FILE" | sed 's/\./\//')/"
        if [ "$DEBUG_MODE" == 'true' ]; then
            logg info 'Dconf settings ID:'
            echo "$DCONF_SETTINGS_ID"
        fi
        # Reset dconf settings if environment variable RESET_DCONF is set to true
        if [ "$RESET_DCONF" == 'true' ]; then
            logg info 'Resetting dconf settings for `'"$DCONF_SETTINGS_ID"'`'
            dconf reset -f "$DCONF_SETTINGS_ID"
        fi
        logg info 'Loading versioned dconf settings for `'"$DCONF_SETTINGS_ID"'`'
        dconf load "$DCONF_SETTINGS_ID" < "$DCONF_CONFIG_FILE"
        logg success 'Finished applying dconf settings for `'"$DCONF_SETTINGS_ID"'`'
    done
else
    logg warn '~/.config/dconf/settings does not exist!'
fi

{{ end -}}
