#!/bin/bash
#. /usr/local/lib/desktop-session/lib-desktop-session.sh

conf_dir="$HOME/.desktop-session"
wallpaper_pid_file="$conf_dir/wallpaper.pid"
wallpaper_conf="$conf_dir/wallpaper.conf"
wallpaper_list_conf="$conf_dir/wallpaper-list.conf"
wallpaper_conf_etc="/etc/desktop-session/wallpaper.conf"
wallpaper_list_conf_etc="/etc/desktop-session/wallpaper-list.conf"
mime_array=("image/png" "image/jpeg" "image/gif" "image/bmp" "image/tiff" "image/x-icon" "image/x-xbitmap" "image/x-xpixmap")


random_select() {
    wallpaper_folder="$(cat $wallpaper_conf | grep '^FOLDER' |cut -d '=' -f2 |cut -d " " -f2)";
    alist=( $(find $wallpaper_folder -type f ) );
    range=${#alist[*]};
    loop=0
    while : ; do
        show=$(( $RANDOM % $range ));
        file=""$wallpapers/${alist[$show]}"";
        mime_file=$(file -b --mime-type $file)
        if [[ " ${mime_array[@]} " =~ " $mime_file " ]]; then
            loop=0            
            wallpaper=""$file"";
            sedfile=${wallpaper//\//\\\/};
            sed -i "s/^$code=.*/$code=$sedfile/" $wallpaper_list_conf;
            break;
        fi
        if [ "$loop" -gt "20" ]; then
            echo "Exit: Stuck in a loop looking for image files"
            exit
        fi
        loop=$((loop+1))
    done
}

wallpaper_set() {
    
    wallpaper=${wallpaper:-$(cat $wallpaper_list_conf | grep "^$code" |cut -d '=' -f2 )} #|sed "s/\ /\\\ /ig")}
    
    if [ ! -f "$wallpaper" ]; then
        echo "Exit: The configured wallpaper is either not a file or does not exist.";
        echo "configured wallpaper: $wallpaper";
        exit
    fi
    
    case $im in
        rox)
            Rox-Wallpaper "$wallpaper" &
            feh --bg-$style "$wallpaper" &
            ;;
        space)
            spacefm --set-wallpaper "$wallpaper" &
            feh  --bg-$style "$wallpaper" &
            ;;
        *)
            feh  --bg-$style "$wallpaper" &
            ;;
    esac
}

#######START########
IFS='
'
disp=${DISPLAY#:}
disp=${disp%.[0-9]}
if [ ! -f "$wallpaper_conf" ]; then
    wallpaper_conf="$wallpaper_conf_etc"
fi
if [ ! -f "$wallpaper_list_conf" ]; then
    wallpaper_conf="$wallpaper_list_conf_etc"
fi
type="$(cat $wallpaper_conf | grep '^TYPE' |cut -d '=' -f2 |cut -d ' ' -f2)"
style="$(cat $wallpaper_conf | grep '^STYLE' |cut -d '=' -f2 |cut -d ' ' -f2)"
code=${1:-$(cat $conf_dir/desktop-code.$disp)}
wm=${code#*-}
im=${code%-$wm}

case "$type" in
    random)
        random_select
        wallpaper_set 
        ;;
        
    random-time)
        if [ ! -f "$wallpaper_pid_file" ] || [ ! -f "/proc/$(head -1 $wallpaper_pid_file)/exe" ]; then
            echo "$$" > $wallpaper_pid_file
            until [ "$(cat $wallpaper_conf | grep '^TYPE' |cut -d '=' -f2 |cut -d ' ' -f2)" != "random-time" ]
            do
                delay="$(cat $wallpaper_conf | grep '^DELAY' |cut -d '=' -f2 |cut -d ' ' -f2)"
                random_select
                wallpaper_set 
                sleep $delay
            done
            rm "$wallpaper_pid_file"
        fi
        ;;
        
    static)
        wallpaper_set 
        ;;
        
    color)
        imported_color=$(cat $wallpaper_conf | grep '^COLOR' |cut -d '=' -f2 |cut -d ' ' -f2)
        xsetroot -solid "#$imported_color" &
        ;;

    *)
        say "There was no valid style for wallpaper setting found, defaulting to grey background";
        xsetroot -solid "#8a8a8a";
        ;;
esac
