#!/bin/bash
SOURCE="$0"
source /usr/lib/elive-tools/functions
EL_REPORTS="1"
el_make_environment
# gettext functions
if [[ -x "/usr/bin/gettext.sh" ]] ; then
    . gettext.sh
else
    # make it compatible
    eval_gettext(){
        echo "$@"
    }
fi
TEXTDOMAIN="deliver"
export TEXTDOMAIN

# if we are in e16, our updated (reconfigured) language must be updated, that's not needed in e17 since the ENV var is set
if [[ -n "$E_ROOT" ]] ; then
    source /etc/default/locale 2>/dev/null || true
fi


main(){
    local instmenu
    local cpubogomips

    # debug mode
    if grep -Fqs "debug" /proc/cmdline ; then
        export EL_DEBUG=3
        if grep -Fqs "completedebug" /proc/cmdline ; then
            set -x
        fi
    fi

    if grep -Fqs "thanatests" /proc/cmdline ; then
        is_thanatests=1
    fi

    # memory="$( cat /proc/meminfo | grep -F MemTotal | tail -1 | awk '{print $2}' )"
    RAM_TOTAL_mb="$( el_ram_usage_show total )"
    cpubogomips="$( grep -F bogomips /proc/cpuinfo | sed -e 's|^.*: ||g' -e 's|\..*$||g' | tr ' ' '\n' | grep "[[:digit:]]" | head -1 )"

    # messages {{{
    local message_install
    message_install="$( printf "$( eval_gettext "Install Elive in your hard disk (entirely cost-free)." )" )"

    local message_live_mode
    message_live_mode="$( printf "$( eval_gettext "Continue trying Elive in Live mode." )" )"

    local message_donate
    message_donate="$( printf "$( eval_gettext "I want to make a donation to Elive" )" ) :)"

    local message_restart_confs
    message_restart_confs="$( printf "$( eval_gettext "Reset desktop settings" )" "" )"

    local message_synthwave
    message_synthwave="$( printf "$( eval_gettext "Play the Elive Synthwave Music Player !" )" )"


    local message_suggest_install
    message_suggest_install="$( printf "$( eval_gettext "The Live mode is a demo showcasing what Elive offers. Install it on your computer for a faster, more feature-rich system. Elive has over 20 years of development and is an amazing OS for daily use." )" )"


    # - messages }}}

    # create the script to run
    instmenu="$(mktemp)"
    cat > "$instmenu" << EOF
#!/bin/bash

# wait 8 minutes to tell the user if wants to install Elive, a quite big ammount is needed in order to let the user to test the system enough to be happy with it
sleep 8m

source /etc/default/locale
[[ -s /etc/elive/settings ]] && source /etc/elive/settings


if ! ps aux | grep -Fv grep | grep -Fqs eliveinstaller-wrapper ; then

    if ((is_premium_user)) ; then
        result="\$( zenity --list --width=520 --height=320 --column=Id --column="Option" --hide-column=1  1 "$message_install" 5 "$message_synthwave" 4 "$message_restart_confs" 2 "$message_live_mode" )"
    else
        result="\$( zenity --list --width=520 --height=320 --column=Id --column="Option" --hide-column=1  1 "$message_install" 5 "$message_synthwave" 3 "$message_donate" 4 "$message_restart_confs" 2 "$message_live_mode" )"
    fi

    case "\$result" in
        1)
            if [[ -x "/usr/sbin/eliveinstaller" ]] ; then
                eliveinstaller-wrapper
            else
                zenity --error --text="Installer of Elive not found, maybe this is an experimental version. Use our IRC Chat or Forums to get more help"
            fi
            ;;
        2)
            true
            ;;
        3)
            web-launcher "https://www.elivecd.org/donate/?id=system-live-menu-ask-install"
            ;;
        4)
            e17-restart-and-remove-conf-file-WARNING-dont-complain
            ;;
        5)
            ( synthwave-player & )
            ;;
    esac

    # free some ram so the system is more clean after setting up the main desktop
    wait
    sync
    LC_ALL=C sleep 5
    sudo bash -c "echo 3 > /proc/sys/vm/drop_caches"

fi
EOF
chmod a+x "$instmenu"
    # end scripts

    # enable e16 sound theme, if we are in a good hour time
    #if [[ -n "$EROOT" ]] ; then
        #hour="$(date +%k)"
        #if [[ -s "/etc/elive-tools/geolocation/livemode-location-fetched.txt" ]] && [[ "${hour}" -lt "21" ]] && [[ "$hour" -gt "8" ]] && ! ((is_thanatests)) ; then
            #eesh -e sound on
        #fi
    #fi

    # elive welcome menu
    if [[ "${RAM_TOTAL_mb}" -gt "1600" ]] && ! [[ "$cpubogomips" -lt 3100 ]] && ! ((is_thanatests)) ; then
        if [[ -x /usr/bin/elive-welcome ]] &&  ! ps aux | grep -Fv grep | grep -Fv "hooks.d" | grep -Fqs elive-welcome && [[ -n "$E_START" ]] ; then
            enlightenment_remote -exec-cmd "sleep 60 ; elive-welcome"
        fi
    fi


    # installer menus & others
    if cat /proc/cmdline | tr ' ' '\n' | grep -Fv "ignoreinstallermodule" | grep -qsE "(eliveinstaller|installer|install|autoupgrade)" ; then
        # autorun installer
        sleep 8

        # e17
        if [[ -n "$E_START" ]] ; then
            if ! ps aux | grep -Fv grep | grep -Fqs eliveinstaller-wrapper ; then
                enlightenment_remote -exec-cmd "eliveinstaller-wrapper"
                # we are done, so we don't want to prompt the user again
                exit
            fi
        fi
        # e16
        if [[ -n "$EROOT" ]] ; then
            eesh -e exec eliveinstaller-wrapper
        fi
    else
        # installer menu
        if ! grep -qs "persistence" /proc/cmdline && ! ((is_thanatests)) ; then
            # e17
            if [[ -n "$E_START" ]] ; then
                enlightenment_remote -exec-cmd "$instmenu"
            fi
            # e16
            if [[ -n "$EROOT" ]] ; then
                eesh -e exec $instmenu
            fi
        fi
    fi


    #
    # PAUSE
    #
    sleep 5m

    # reconfigure conky network
    # this is a duplicated of the elive-tools configurator, but we run it here instead because we need to wait some time the user configures the network first
    if [[ -e "$HOME/.conkyrc" ]] ; then
        if pidof conky 1>/dev/null ; then
            killall conky
            is_restart_needed_conky=1
        fi

        if el_verify_internet ; then
            iface="$( grep -F "1" /sys/class/net/*/carrier 2>/dev/null | grep -Fv "/net/lo/" | sed -e 's|/carrier.*$||g' -e 's|^.*/||g' | head -1 )"
            if [[ -n "$iface" ]] ; then
                case "$iface" in
                    eth*|enp*)
                        # ETH lan cable networks, disable wifi features
                        sed -i -e "s|^\(ESSID.*\)$|#\1|gI" "$HOME/.conkyrc"
                        sed -i -e "s|^\(Connection quality.*\)$|#\1|gI" "$HOME/.conkyrc"
                        sed -i -e "s|wlan0|$iface|g" "$HOME/.conkyrc"
                        ;;
                    lo)
                        true
                        ;;
                    *)
                        # change iface to our used one
                        # update: this is not needed, but we need to have the network already set up from wlan before to run this, so it will probably run only when the system is installed
                        #iface="$( iwconfig 2>/dev/null | grep -F IEEE | awk '{print $1}' | head -1 )"
                        #if [[ -z "$iface" ]] ; then
                            #iface="wlan0"
                        #fi
                        sed -i -e "s|wlan0|$iface|g" "$HOME/.conkyrc"
                        ;;
                esac
            fi
        fi

        # change interval if computer is slow
        if [[ "$RAM_TOTAL_mb" -lt 2300 ]] ; then
            # reduce time interval
            #sed -i -e "s|^update_interval.*$|update_interval 4.0|gI" "$HOME/.conkyrc"
            sed -i -e "s|^.*update_interval.*$|\tupdate_interval = 4.0,|gI" "$HOME/.conkyrc"
        fi

        if ((is_restart_needed_conky)) ; then
            killall conky
            el_debug "restarting conky"
            ( conky 1>/dev/null 2>&1 & disown )
        fi
    fi


    # if we are debugging give it a little pause to see what is going on
    #if grep -Fqs "debug" /proc/cmdline ; then
        #echo -e "debug: sleep 2" 1>&2
        #sleep 2
    #fi

    # bug catched! seems like enlightenment_remote has some delay to be launched and probably the file is removed BEFORE, so let's add a damn pause
    #sleep 30
    #rm -f "$instmenu"

    exit 0
}

#
#  MAIN
#
main "$@"

# vim: set foldmethod=marker :
