#!/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(){
    killall nm-applet 2>/dev/null || true
    rm -f "$lockfile"
}
exit_error(){
    killall nm-applet 2>/dev/null || true
    rm -f "$lockfile"
}

if [[ -r "$lockfile" ]] ; then
    PROCCESS="$(cat $lockfile)"
else
    PROCCESS=" "
fi
if (ps up $PROCCESS) 1>/dev/null 2>&1 ; then
    el_debug "$(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
# INFO signals:
# 0 EXIT: when program finishes
# 1 HUP: loss of terminal
# 2 INT: is ^C
# 3 QUIT: ^d quit from keyboard
# 4 ILL: illegal instruction
# 5 TRAP: breakpoints
# 6 ABRT: abort
# 7 BUS: hardware problem
# 8 FPE: wrong math operation
# 9 KILL: uncatcheable / unstopable
# 10 USR1: customizable
# 13 PIPE: broken pipe
# 14 ALMR: timers
# 15 TERM: termination
# 17 CHLD: children monitor
# 20 TSTP: ^z
# 24 XCPU: too much cpu consumption
# 28 WINCH: window resize
trap "exit_ok" EXIT
#trap "exit_error" 1 3 5 6 14 15 ERR
trap "exit_error" 1 2 3 5 6 14 ERR

# SET the lock file
echo "$$" > "$lockfile"


# end lock system }}}



main(){

    user_active_desktop="$( el_user_desktop_active_get desktop )"

    # Kill any running nm-applet instances (ignore errors)
    killall nm-applet 2>/dev/null || true


    if [[ -n "$user_active_desktop" ]] ; then
        case "$user_active_desktop" in
            "E16")
                # EROOT="/usr/share/e16"

                # Start nm-applet in the background with the popup option
                ( nm-applet --popup & )

                ;;
            "Enlightenment")
                # E_START="1"

                # old trayer running? launch it directly
                if pidof trayer 1>/dev/null 2>&1  ; then
                    ( nm-applet --popup & )
                else
                    if ! el_flag check wifi-app-newtrayer && ! el_flag check wifi-app-oldtrayer ; then
                        if $guitool --question --text="$( eval_gettext "Do you want to use the new Systray instead of the traditional one?" )" ; then
                            el_flag set wifi-app-newtrayer

                        else
                            if ! el_dependencies_check "trayer" ; then
                                el_dependencies_install "trayer"
                            fi
                            el_flag set wifi-app-oldtrayer
                        fi
                    fi

                    if el_flag check wifi-app-newtrayer ; then
                        # Start nm-applet in the background with the popup option
                        ( nm-applet --indicator & )
                        el_notify soft wifi "Wi-Fi" "$( eval_gettext "Select from the bottom-left corner the Wi-Fi network you want to connect." )"
                    fi

                    if el_flag check wifi-app-oldtrayer ; then
                        # Start nm-applet in the background with the popup option
                        ( trayer --edge top --align left --widthtype request --heighttype request --transparent true --alpha 256 & )
                        ( nm-applet --popup & )
                    fi
                fi

                ;;
            *)
                el_error "$( eval_gettext "Unknown desktop found." ): $user_active_desktop"
                zenity --error --text="$( eval_gettext "Unknown desktop found." )"
                exit
                ;;
        esac

    else
        el_error "$( eval_gettext "No desktop found." )"
        zenity --error --text="$( eval_gettext "No desktop found." )"
        exit
    fi

    # Wait for 6 minutes
    count=0
    while pidof nm-applet >/dev/null ; do
        sleep 5
        count="$((count+1))"
        [[ "$count" -ge 72 ]] && break
    done

    # Kill nm-applet again after the sleep (ignore errors)
    killall nm-applet 2>/dev/null || true


}

#
#  MAIN
#
main "$@"

# vim: set foldmethod=marker :
