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

# 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 }}}
#
# XXX Note: there's a .desktop that references to this (name $0) command, do not break up things
main(){
    for arg in "$@" ; do
        case "$arg" in

            "--ask")
                zenity --info --text="$( eval_gettext "Make sure to close all your running applications correctly before activating new desktop settings." )"

                if ! zenity --question --text="$( eval_gettext "Do you want to reset your desktop settings? This will return them to a new default setup, which is helpful if you’re not sure how to fix your desktop." )" ; then
                    exit
                fi

                ;;
        esac
    done

    user_active="$( el_user_desktop_active_get user )"
    user_active_desktop="$( el_user_desktop_active_get desktop )"

    if [[ -z "$user_active" ]] ; then
        el_error "$( eval_gettext "No active user desktop found." )"
        zenity --error --text="$( eval_gettext "No active user desktop found." )"
        exit
    fi
    sync

    if [[ -n "$user_active_desktop" ]] ; then
        case "$user_active_desktop" in
            "E16")
                EROOT="/usr/share/e16"
                tmux-attach-jobs background erestart "e17-restart-and-remove-conf-file-WARNING-dont-complain-tmuxed e16"
                ;;
            "E17")
                tmux-attach-jobs background erestart "e17-restart-and-remove-conf-file-WARNING-dont-complain-tmuxed e17"
                ;;
            "Enlightenment")
                E_START="1"
                tmux-attach-jobs background erestart "e17-restart-and-remove-conf-file-WARNING-dont-complain-tmuxed enlightenment"
                ;;
            *)
                el_error "$( eval_gettext "Unknown desktop found." ): $user_active_desktop"
                zenity --error --text="$( eval_gettext "Unknown desktop found." )"
                exit
                ;;
        esac

    else
        # no running desktop found, show error
        el_error "$( eval_gettext "No running E session found." )"
        zenity --error --text="$( eval_gettext "No running E session found." )"
        exit
    fi

}

#
#  MAIN
#
main "$@"

# vim: set foldmethod=marker :
