{{- 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
        DCONF_SETTINGS_ID="/$(basename "$DCONF_CONFIG_FILE" | sed 's/\./\//')"
        # 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 -}}