#!/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(){
    rm -f "$lockfile"
}
exit_error(){
    rm -f "$lockfile"
}

if [[ -r "$lockfile" ]] ; then
    PROCCESS="$(cat $lockfile)"
else
    PROCCESS=" "
fi
if (ps up $PROCCESS) 1>/dev/null 2>&1 ; then
    echo -e "E: $(basename "$0" ) already running"
    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(){
    local pid desktop

    # Usage
    if [[ -z "${1}" ]] ; then
        echo -e "Usage: $(basename $BASH_SOURCE) e16|e17|enlightenment"
        exit 1
    fi

    desktop="$1"
    shift


    # play "breaking" sound
    if ! ((NOVOICE)) && ! ((SILENT)) && [[ -s "/usr/share/sounds/elive/glass-break.wav" ]] ; then
        hour="$(date +%k)"
        if [[ "${hour}" -lt "21" ]] && [[ "$hour" -gt "8" ]] ; then
            if [[ -x "$( which mpv )" ]] ; then
                ( mpv --no-video /usr/share/sounds/elive/glass-break.wav & )
            else
                if [[ -x "$( which mplayer )" ]] ; then
                    ( mplayer -cache-min 99 -cache-seek-min 99 -vc null -vo null -quiet -input nodefault-bindings -noconfig all  "/usr/share/sounds/elive/glass-break.wav" & )
                fi
            fi
            LC_ALL=C sleep 0.3
        fi
    fi

    cd ~ || exit 1
    sync

    case "$desktop" in
        e16|E16)

            elive-autostart-applications stop

            # kill all startup-desktop processes
            for i in ~/.e16/New/* ~/.e16/Init/*
            do
                pid="$( ps ux | grep -Fv grep | grep -F "$i" | awk '{print $2}' )"
                if [[ -n "$pid" ]] ; then
                    kill -9 "$pid"
                fi
            done

            killall -9 cairo-dock 2>/dev/null
            killall -9 conky 2>/dev/null

            killall -s STOP e16 starte16 2>/dev/null
            sync

            # upgrade confs
            EL_DEBUG=0 elive-skel upgrade .e16

            ;;

        e17|E17)
            # E17+
            killall -s STOP enlightenment_start enlightenment ecomorph efreetd 2>/dev/null

            # upgrade confs
            rm -fr ~/.e

            # upgrade confs
            EL_DEBUG=0 elive-skel upgrade .ecomp
            EL_DEBUG=0 elive-skel upgrade .emerald

            ;;
        enew|ENEW|Enlightenment|enlightenment)
            # Enlightenment
            killall -s STOP enlightenment 2>/dev/null

            ;;
        *)
            echo -e "E: unknown desktop to restart conf, example: $(basename "$0" ) e16|e17|enlightenment" 1>&2
            exit 1
            ;;
    esac


    # generic upgrades for any desktop
    EL_DEBUG=0 elive-skel upgrade .config/cairo-dock
    EL_DEBUG=0 elive-skel upgrade .config/ulauncher
    EL_DEBUG=0 elive-skel upgrade .local/share/ulauncher

    EL_DEBUG=0 elive-skel upgrade .gtkrc-2.0.mine
    EL_DEBUG=0 elive-skel upgrade .config/gtk-3.0/settings.ini
    EL_DEBUG=0 elive-skel upgrade .xsettingsd
    EL_DEBUG=0 elive-skel upgrade .Xdefaults

    EL_DEBUG=0 elive-skel upgrade .conkyrc

    EL_DEBUG=0 elive-skel upgrade .config/audacious/playlists/1000.audpl
    EL_DEBUG=0 elive-skel upgrade .config/terminology
    EL_DEBUG=0 elive-skel upgrade .config/mimeapps.list
    EL_DEBUG=0 elive-skel upgrade .local/share/applications/defaults.list
    EL_DEBUG=0 elive-skel upgrade .local/share/applications/mimeapps.list

    EL_DEBUG=0 elive-skel upgrade .config/smplayer

    # cleanups
    rm -fr ~/.elementary ~/.config/elementary 1>/dev/null 2>&1
    rm -fr ~/.xsession-errors 1>/dev/null 2>&1
    rm -fr ~/.config/Thunar/uca.xml 1>/dev/null 2>&1

    # restart audio configurations
    touch ~/.asoundrc-reconfigureme

    # sync

    # finish the kill process
    case "$desktop" in
        e16|E16)
            killall -9 e16 starte16 efreetd  2>/dev/null

            # upgrade confs
            EL_DEBUG=0 elive-skel upgrade .e16

            ;;
        e17|E17)
            # killall -9 enlightenment_start enlightenment_system enlightenment ecomorph e16 starte16 efreetd dbus-daemon ssh-agent gnome-keyring-daemon gvfsd at-spi-bus-launcher systemd 2>/dev/null
            # killall -9 enlightenment_start enlightenment_system enlightenment ecomorph e16 starte16 efreetd dbus-daemon ssh-agent gnome-keyring-daemon gvfsd at-spi-bus-launcher systemd 2>/dev/null
            killall -9 enlightenment_start enlightenment_system enlightenment ecomorph e16 starte16 efreetd dbus-daemon ssh-agent gnome-keyring-daemon gvfsd at-spi-bus-launcher dbus-launch systemd 2>/dev/null

            sleep 1
            killall -9 efreetd 2>/dev/null
            LC_ALL=C sleep 0.3

            # upgrade confs
            rm -fr ~/.e
            rm -fr ~/.cache/efreet

            ;;
        enew|ENEW|Enlightenment|enlightenment)
            killall enlightenment  2>/dev/null
            killall efreetd  2>/dev/null
            killall dbus-daemon 2>/dev/null
            killall enlightenment_start  2>/dev/null
            killall enlightenment enlightenment_start efreetd  2>/dev/null

            sync
            LC_ALL=C sleep 0.5

            killall -9 enlightenment_alert enlightenment enlightenment_start  2>/dev/null

            # upgrade confs
            rm -fr ~/.e
            rm -fr ~/.cache/efreet

            ;;
    esac


    # just in case something is remaining
    wait

}

#
#  MAIN
#
main "$@"

# vim: set foldmethod=marker :
