#!/bin/bash
### XXX
# Unfortunately too few people donate, so don't annoy users them with popups
# also this needs a little rewrite and support for e16
# exit




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 tempfile has_updated_donors
    tempfile="/tmp/.${USER}-donors.txt"

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


    # donate?
    if el_flag check "crowdfunding-advertised" ; then
        return 0
    fi

    # check: only if we are in a development version and is recent
    if ! el_check_version_development_is_days_recent 90 ; then
        exit 0
    fi

    if ! el_verify_internet fast ; then
        exit 0
    fi

    url="https://donorbox.org/join-the-revival-of-a-linux-gem"

    progress_percent="$( curl -m 30 -Ls "$url" | grep -E "^\s+width: [[:digit:]]+%;$" | sed -e 's|^.*width: ||g' -e 's|%.*$||g' )"

    if [[ -n "$progress_percent" ]]; then

        # TODO: include in YAD the progres of the E development too
        # get the progress of E development:
        # Fetch open and closed issue counts
        open_closed="$( curl -m 30 -Ls https://git.enlightenment.org/enlightenment/enlightenment/issues | sed -e 's|&nbsp;| |g' | grep -E "[[:digit:]]+ (Open|Closed)" | sort -u )"

        # Parse the numbers for open and closed issues
        open_count="$(echo "$open_closed" | grep "Open" | awk '{print $1}')"
        closed_count="$(echo "$open_closed" | grep "Closed" | awk '{print $1}')"

        # Calculate the total number of issues
        total_count="$((open_count + closed_count))"

        # Calculate the progress percentage (closed issues as progress)
        if [ "$total_count" -gt 0 ]; then
            progress_percentage="$(echo "scale=2; $closed_count / $total_count * 100" | LC_ALL=C bc -l )"
        fi


        # echo "Progress: $progress_percent%"
        local message_message
        message_message="$( printf "$( eval_gettext "We're working hard to bring back the magic of the Elive desktop to be more beautiful and featured. But we need your help to make it happen! Your contribution, no matter how small, will help us reach this goal. Let's show the world what the desktop of the future should be!" )" "" )"
        local message_progress
        message_progress="$( printf "$( eval_gettext "Actual progress is:" )" "" )"
        local message_button1
        message_button1="$( printf "$( eval_gettext "Not Yet, But I'll Share!" )" "" )"
        local message_button2
        message_button2="$( printf "$( eval_gettext "I believe in Elive - I want to Support it" )" "" )"
        local message_elive_progress
        message_elive_progress="$( printf "$( eval_gettext "Elive campaign reaching" )" "" )"
        local message_ebugs_progress
        message_ebugs_progress="$( printf "$( eval_gettext "Bugs fixed by E" )" "" )"
        local message_title
        message_title="$( printf "$( eval_gettext "Support the Revival of a Linux Gem" )" "" )"


        if {
            echo "1:#${message_elive_progress} ${progress_percent} %"
            echo "1:${progress_percent%.*}"
        } | $yad --progress --multi-progress \
            --title="${message_title}" \
            --text="${message_message}\n\n${message_ebugs_progress}: ${progress_percentage%.*}% (from ${total_count} in total)\n\n${message_progress}\n" \
            --width=400  \
            --bar="" \
            --buttons-layout=spread \
            --button="${message_button2}":0 \
            --button="${message_button1}":1 \
            $NULL ; then

            # add a heredoc with the html code:
            cat > /tmp/support-elive.html <<EOF
<h2><a href="$url" style="text-align: center; display: block; font-size: 1.5em; font-weight: bold;"><i>View it on the Donorbox website</i></a></h2>
<br>
<script src="https://donorbox.org/widget.js" paypalExpress="false"></script><iframe src="https://donorbox.org/embed/join-the-revival-of-a-linux-gem?default_interval=o&amount=30&show_content=true" name="aaaaaaa" allowpaymentrequest="allowpaymentrequest" seamless="seamless" frameborder="1" scrolling="no" height="900px" width="100%" style="max-width: 100%; min-width: 100%; max-height:none!important" allow="payment"></iframe>
EOF
            if [[ -e /tmp/support-elive.html ]] ; then
                web-launcher /tmp/support-elive.html
            else
                web-launcher "$url"
            fi
        fi

    else
        el_warning "Unable to get progress"
    fi



    # NOTE: the next is old code so exit here:
    exit

    # crowdfunding for elive 3.0
    if el_verify_internet ; then
        cd /tmp
        curl -L -s -A Mozilla https://www.elivecd.org/donors-crowdfunding-3.0.txt -o "$tempfile"

        if grep -Fqs "Eric Fith" "$tempfile" 2>/dev/null ; then
            has_updated_donors=1
        fi
    fi

    if ! ((has_updated_donors)) ; then
        rm -f "$tempfile"
            # heredoc cached list {{{
            cat > "$tempfile" <<EOF
100
Eric Fithian
60
W.J. van der Marel
52
Mika Kuja
50
Ricci Enzo
50
(anonymous donator)
50
(anonymous donator)
50
Kyle Minor
50
Kingdon Barrett
50
Davy Moerloose
30
(anonymous donator)
30
Stefano  Senni
30
Ricard Pradell
30
luigi vargas aquino
30
Lawrence A De Coste
30
Ken Beals
30
(anonymous donator)
30
Aaron Whiting
20
Radek Valáek
20
(anonymous donator)
15
Michal Kitta
15
Jörg Strohmeier
10
Pavel Kuznetsov
10
(anonymous donator)
10
Marcus Clark
10
Jesse de Barros
8
(anonymous donator)
EOF
        # }}}
    fi

    local message_first
    message_first="$( printf "$( eval_gettext "These individuals are contributing to the development of the next stable version. Each donation is crucial. Thanks to their support, Elive has remained an amazing OS for computers for over 20 years.\n\nWould you like to add yourself (or your brand) to this list?" )" "" )"


    if cat "$tempfile" | yad --center --height=400 --width=400 --title="Elive thanks the generosity of everyone!" --image="znes" --text="$message_first" --list --print-all --column="Donated" --column="Name" ; then
        # open donations page
        web-launcher "www.elivecd.org/make-elive-3-0-reality"
        has_probably_donated=1
    else
        # tell about designs
        if zenity --question --text="$( eval_gettext "No matter how modest your contribution may be, it will significantly enhance our crowdfunding campaign, furthermore, you would gain access to new versions and designs before they are available to anyone else, would you be interested in joining us in this endeavor?" )" ; then
            # open donations page
            web-launcher "www.elivecd.org/make-elive-3-0-reality"
            has_probably_donated=1
        #else
            ## open page to show designs
            #/etc/alternatives/x-www-browser "www.elivecd.org/crowdfunding-3.0-benefits"
        fi
    fi
    # wait again and ask again
    #if ! ((has_probably_donated)) ; then
        ## 4 minutes
        #sleep 240
        #if zenity --question --text="$( eval_gettext "It will be really helpful if you donate even a small amount to our crowdfunding, Elive needs you! we are desesperate to fund it!" )" ; then
            ## open donations page
            #/etc/alternatives/x-www-browser "www.elivecd.org/make-elive-3-0-reality"
        #fi
    #fi

    el_flag add "crowdfunding-advertised"

    # 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

    exit 0
}

#
#  MAIN
#
main "$@"

# vim: set foldmethod=marker :
