#!/bin/bash
SOURCE="$0"
source /usr/lib/elive-tools/functions
EL_REPORTS="1"
el_make_environment

# Lock system (good one) {{{
# lockfile="/tmp/.$(basename $0)-${USER}.lock"
#
# exit_ok(){
#     set +e
#     rm -f "$lockfile"
#     if [[ -s "$tmpimage" ]] && ! [[ -d "$tmpimage" ]] ; then
#         rm -f "$tmpimage"
#     fi
#     rmdir "$tmpdir"
# }
# exit_error(){
#     set +e
#     rm -f "$lockfile"
#     if [[ -s "$tmpimage" ]] && ! [[ -d "$tmpimage" ]] ; then
#         rm -f "$tmpimage"
#     fi
#     rmdir "$tmpdir"
# }
#
# if [[ -r "$lockfile" ]] ; then
#     PROCCESS="$(cat $lockfile)"
# else
#     PROCCESS=" "
# fi
# if (ps up $PROCCESS) 1>/dev/null 2>&1 ; then
#     # el_error "$(basename $0) already running"
#     zenity --error --text="$( eval_gettext "The wallpaper tool is still doing some tasks, please wait a few seconds..." )"
#     exit
# else
#     echo $$ > "$lockfile"
# fi
#
# # traps needs to be after the lock verification, in order to not remove it when we are already running
# trap "exit_ok" EXIT
# trap "exit_error" 1 3 5 6 14 15 ERR TERM
#
# # SET the lock file
# echo "$$" > "$lockfile"


# end lock system }}}

main(){
    # pre {{{
    local file


    # }}}
    # e16
    if [[ -n "$EROOT" ]] ; then
        backgrounds_dir="$HOME/.e16/backgrounds"
    fi
    # E newer
    if [[ -n "$E_START" ]] ; then
        backgrounds_dir="$HOME/.e/e/backgrounds"

        if [ -n "$E_START" ] && [ -z "$E_HOME_DIR" ] ; then
            E_HOME_DIR="$HOME/.e/e17"
        fi
        backgrounds_dir="$E_HOME_DIR/backgrounds"
        # E_VERSION="$( enlightenment --version | grep "^Version: " | sed -e 's|^Version: ||g' | tail -1 )"
        # case "$E_VERSION" in
        #     0.17.*)
        #         backgrounds_dir="$HOME/.e/e17/backgrounds"
        #         ;;
        #     *)
        #         # zenity --warning --text="Wallpapers configurator for newer version of Enlightenment is not yet implemented, try to do it manually from its configurations"
        #         backgrounds_dir="$HOME/.e/e/backgrounds"
        #         ;;
        # esac
    fi

    mkdir -p "$backgrounds_dir"


    # add wallappers
    for file in "$@"
    do
        if ! [[ -s "$file" ]] ; then
            el_warning "No wallpaper file set: $file"
            continue
        fi

        file="$( readlink -f "$file" )"

        # e16
        if [[ -n "$EROOT" ]] ; then
            name="$( echo "$file" | sed -e 's|^.*/||g' -e 's|\.*$||g' -e 's| |_|g' )"

            # add it
            if grep -qs "boot=live" /proc/cmdline ; then
                ln -s "$file" "$backgrounds_dir/$name"
            else
                cp -a "$file" "$backgrounds_dir/$name"
            fi
            file="$backgrounds_dir/$name"

            # set it
            eesh bg xset "$name" 0 0 0 "$file" 0 0 0 0 1024 1024 "" 0 0 0 0 0
            if eesh bg list | grep -qs "^${name}$" ; then
                for desk in $( seq $( eesh desk list | wc -l ) )
                do
                    eesh bg use "$name" $(( $desk - 1 ))
                done
            fi
        fi

        # e17
        if [[ -n "$E_START" ]] ; then
            enlightenment_remote -desktop-bg-add-set "$file"
        fi

    done

    # make a blurred version for the login manager
    if touch /var/tmp/wallpaper/wallpaper.jpg 2>/dev/null ; then
        if ! el_dependencies_check "magick" ; then
            if ! el_dependencies_install "imagemagick" ; then
                el_error "unable to install imagemagick"
            fi
        fi
        # killall magick || killall -9 magick
        pids="$( ps u -C magick | grep -E "(blur|var/tmp/wallpaper|$(basename $SOURCE)-${USER})" | awk '{print $2}' | tr '\n' ' ' )"
        if [[ -n "$pids" ]] ; then
            kill -9 $pids 2>/dev/null || true
        fi

        if [[ "$( identify "$file" | tr ' ' '\n' | grep -E "[[:digit:]]+x[[:digit:]]+" | sort -u -V | head -1 | sed -e 's|x.*$||g' )" -gt 2000 ]] ; then
            magick "$file" -limit time 300 -resize "2000x2000" "/tmp/.$(basename $SOURCE)-${USER}1.jpg"
        else
            cp "$file" "/tmp/.$(basename $SOURCE)-${USER}1.jpg"
        fi



        magick "/tmp/.$(basename $SOURCE)-${USER}1.jpg" -blur 0x30 -limit time 600 "/tmp/.$(basename $SOURCE)-${USER}2.jpg"
        magick "/tmp/.$(basename $SOURCE)-${USER}2.jpg" -brightness-contrast -8x2 -limit time 300 "/tmp/.$(basename $SOURCE)-${USER}3.jpg"
        mv -f "/tmp/.$(basename $SOURCE)-${USER}3.jpg" "/var/tmp/wallpaper/wallpaper.jpg"
        chmod a+w "/var/tmp/wallpaper/wallpaper.jpg"
        rm -f "/tmp/.$(basename $SOURCE)-${USER}"*jpg
    fi

}

#
#  MAIN
#
main "$@"

# vim: set foldmethod=marker :
