#!/bin/bash

# This script is run when the application starts
# Its purpose is to obtain the current wallpaper as an absolute path, or file URI (e.g. file://a/b.jpg)
# This is needed by Variety at start to ensure History->Back works OK to revert back to the pre-Variety wallpaper.
# You need to edit this if you use something other than Gnome 3 or Unity for a desktop environment

desktop=$DESKTOP_SESSION

# Gnome 3, Unity:
if [ "$desktop" == "ubuntu" ] || [ "$XDG_CURRENT_DESKTOP" == "Unity" ]; then
        gsettings get org.gnome.desktop.background picture-uri

# XFCE
elif [ "$desktop" == "xubuntu" ] || [ "$XDG_CURRENT_DESKTOP" == "XFCE" ]; then
        xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitor0/workspace0/last-image

# LXDE/PCmanFM
elif [ "$XDG_CURRENT_DESKTOP" == "LXDE" ]; then
        grep wallpaper=/ ~/.config/pcmanfm/lubuntu/pcmanfm.conf | sed -e 's/wallpaper=//g'

# LXQt/PCmanFM-qt
elif [ "$XDG_CURRENT_DESKTOP" == "LXQt" ]; then
        grep wallpaper=/ ~/.config/pcmanfm-qt/lxqt/settings.conf | sed -e 's/wallpaper=//g'

# MATE
elif [ `env | grep MATE_DESKTOP_SESSION_ID | wc -l` -gt 0 ]; then
        version=`mate-about -v | sed s/MATE\ Desktop\ Environment\ //`
        if [[ "$version" > "1.6.0" || "$version" == "1.6.0" ]]; then
            gsettings get org.mate.background picture-filename
        else
            mateconftool-2 --get /desktop/mate/background/picture_filename
        fi

# Cinnamon after 2.0
elif [ "$desktop" == "Cinnamon" ] || [ "$XDG_CURRENT_DESKTOP" == "X-Cinnamon" ]; then
        gsettings get org.cinnamon.desktop.background picture-uri

# All above fails => fallback to the Gnome 3 way
else
        gsettings get org.gnome.desktop.background picture-uri
fi

# Feh
# sed "s/\ /\n/g" ~/.fehbg | grep \'

# Gnome 2
# gconftool-2 --get /desktop/gnome/background/picture_filename
