#!/bin/bash
# --------------------------------------------------------------------
# An Openbox pipemenu for use with Dropbox and CrunchBang Linux.
# Written for CrunchBang Linux <http://crunchbanglinux.org/>
# by Philip Newborough (aka corenominal) <mail@philipnewborough.co.uk>
# Ported to #!++ <https://crunchbangplusplus.org>
# by Ben Young <computermouth@crunchbangplusplus.org>
# Ported to Manjaro <https://manjaro.github.io/>
# by Daniel Napora <napcok@gmail.com>
# --------------------------------------------------------------------

PROMPT_DELAY_TEXT='It is a good idea to add a delay before autostarting Dropbox
so that your system can establish a working network connection.

Select the number of seconds to delay Dropbox autostarting.
Click "Cancel" if you do not want to add a delay.'
PROMPT_DELAY_VARIANTS=(FALSE 10 FALSE 20 FALSE 30 FALSE 40 FALSE 50 TRUE 60)

USERDBDIR="$HOME/.dropbox-dist/"


if ! . mabox-include.cfg 2> /dev/null; then
    echo '  Failed to locate mabox-include.cfg in PATH' >&2
    exit 1
fi

# --------------------------------------------------------------------
# manipulation
# --------------------------------------------------------------------

if [[ $1 = '--start-dropbox' ]]; then
    "$USERDBDIR/dropboxd" &

elif [[ $1 = '--stop-dropbox' ]]; then
    killall dropbox

elif [[ $1 = '--install-dropbox' ]]; then
    zenity --question --title='Dropbox Installation' --text 'This script will install Dropbox.\nDo you want to proceed?' || exit 0

    if ! cd "$HOME"; then # TODO Change directory? What for? Better use absolute paths
        echo "Unable to change directory to $HOME"
        exit 1
    fi

    platform=$(uname -m)
    platform=${platform,,}

    case $platform in
        'x86_64')
            #DROPBOXURL='http://packages.crunchbang.org/waldorf-files/dropbox/64/dropbox.tar.gz'
            DROPBOXURL='http://www.dropbox.com/download?plat=lnx.x86_64'
            ;;
        *)
            #DROPBOXURL='http://packages.crunchbang.org/waldorf-files/dropbox/32/dropbox.tar.gz'
            DROPBOXURL='http://www.dropbox.com/download?plat=lnx.x86'
            ;;
    esac

    shitTemp=$(mktemp -u)
    curl -s -I "$DROPBOXURL" > "$shitTemp"
    if [[ $? = 0 ]]; then # TODO this check is just wrong. It doesn't even make sure if it is possible to download a file or not.
        read _ RESPONSE _ <<< $(head -n 1 "$shitTemp")

        #if [ $RESPONSE -ne "200" ]; then
        if [[ RESPONSE != 302 ]]; then
            zenity --warning --title='Dropbox Installation' --text 'Error, failed to contact server. Please try again later.'
            exit 1
        else
            dropboxFile=$(mktemp -u)
            wget "$DROPBOXURL" -O "$dropboxFile" 2>&1 | \
                sed -u 's/^.* \+\([0-9]\+%\) \+\([0-9.]\+[GMKB]\) \+\([0-9hms.]\+\).*$/\1\n# Downloading... \2 (\3)/' | \
                zenity --progress --title='Installing Dropbox' --auto-kill --auto-close
        fi
    else
        zenity --warning --title='Dropbox Installation' --text 'Error, failed to contact server. Please try again later.'
        exit 1
    fi

    #extract binary
    tar -xf "$dropboxFile"
    #delete binary archive
    rm "$dropboxFile"

    #quick sanity check
    if [[ ! -x $USERDBDIR/dropboxd ]]; then
        zenity --warning --title='Dropbox Installation' --text 'Oops! There was an error, Dropbox could not be installed. Sorry.'
        exit 1
    fi
    zenity --info --title='Dropbox Installation' --text "Dropbox has been installed to:\n$USERDBDIR"
    zenity --question --title='Dropbox Installation' --text 'Dropbox can be started automatically when you start your session. Would you like to autostart Dropbox when you login?'
    if [[ $? = 0 ]]; then # add to autostart!
        ans=$(zenity  --title='Dropbox Installation' --list --text  "$PROMPT_DELAY_TEXT" --radiolist  --column 'Pick' --column 'Seconds:' "${PROMPT_DELAY_VARIANTS[@]}")
        if [[ $? = 0 ]]; then
            echo >> "$HOME/.config/openbox/autostart"
            echo '# Autostart the Dropbox deamon' >> "$HOME/.config/openbox/autostart"
            echo "(sleep ${ans}s && \"\$HOME/.dropbox-dist/dropboxd\") &" >> "$HOME/.config/openbox/autostart"
            echo >> "$HOME/.config/openbox/autostart"
        fi
    fi
    zenity --question --title='Dropbox Installation' --text 'Do you wish to start the Dropbox client now?' || exit 0

    "$USERDBDIR/dropboxd" &
else
    # pipemenu stuff
    menuStart
    if [[ ! -d $USERDBDIR ]]; then
        menuItem 'Install Dropbox' 'mabox-dropbox-pipemenu --install-dropbox'
        menuSeparator
        menuItem 'Find out more about Dropbox' 'x-www-browser http://db.tt/5mJg9lb'
    else
        [[ -d $HOME/Dropbox ]] &&
            menuItem 'Open Dropbox Folder' 'thunar $HOME/Dropbox'

        if ! pidof dropbox > /dev/null; then
            menuItem 'Start Dropbox' 'mabox-dropbox-pipemenu --start-dropbox'
        else
            menuItem 'Stop Dropbox' 'mabox-dropbox-pipemenu --stop-dropbox'
        fi
        menuSeparator
        menuItem 'Launch Dropbox Website' 'x-www-browser https://www.dropbox.com/home'
    fi
    menuItem 'Dropbox Terms' 'x-www-browser https://www.dropbox.com/terms'
    menuEnd
fi
exit 0