#!/bin/bash

#	VERSION: 1.6

pinger() 
{
	myurl=www.sourceforge.net
	results=$(ping -c 1 -t 5 "$myurl" 2>/dev/null || echo "Unable to connect to internet")
	if [[ $results = *"Unable to"* ]]; then
		echo ""
		kill $KILLME
		echo -e "$results"
	else
		ping_success &
		sleep 1
		kill $KILLME
	fi
}

ping_success() 
{
	if [ -e /tmp/version.txt ]; then rm /tmp/version.txt; fi
	curl -\# -L -o /tmp/version.txt 'http://sourceforge.net/projects/vpkosx/files/version.txt/download' 2> /dev/null
	mver=$(cat ./version.txt)
	cver=$(cat /tmp/version.txt)
	rm /tmp/version.txt	
	if [[ "$mver" != "$cver" ]]; then
		if [[ $autoInstall = 1 ]]; then
			update_confimed
		else
			update_prompt $mver $cver
		fi
	else
		echo ""
		echo "No new updates available"
	fi
}

update_prompt() 
{
	result_value=$("${COCOAD}" msgbox --no-newline \
	--float \
	--title "vpk" \
	--text "An update is available:" \
	--informative-text "Would you like to install the latest version: $2" \
	--button1 "Install" \
	--button2 "Don't ask again" \
	--button3 "Cancel")
	
	if [ "$result_value" == "1" ]; then
		echo ""
		echo "Installing..."
		update_confimed
	elif [ "$result_value" == "2" ]; then
		echo ""
		echo "Canceled"
		defaults write ~/Library/Preferences/org.w0lf.vpk "autoCheck" '0'
	elif [ "$result_value" == "3" ]; then
		echo ""
		echo "Canceled"
	fi
}

update_confimed() 
{
	echo "Downloading update..."
	if [ -e /tmp/vpk.zip ]; then rm /tmp/vpk.zip; fi
	download_wprogress
	
	echo "Extracting update..."
	if [ -e /tmp/vpk.app ]; then rm -r /tmp/vpk.app; fi
	unzip /tmp/vpk.zip -d /tmp > /dev/null
	
	echo "Cleaning up..."
	if [ -e /tmp/vpk.zip ]; then rm /tmp/vpk.zip; fi
	if [ -e /tmp/__MACOSX ]; then rm -r /tmp/__MACOSX; fi
	
	echo "Launching installer..."
	open -a /tmp/vpk.app/Contents/Resources/upd/vpk_installer.app --args "$ORIDIR"
}

download_wprogress()
{
	curl -\# -L -o /tmp/vpk.zip 'http://sourceforge.net/projects/vpkosx/files/latest/download' 2> /tmp/derp.txt &
	pids="$pids $!"
	echo $pids
	wait_for_process $pids
	exec 7>&-
	rm -f /tmp/upipe
}

wait_for_process() 
{
	rm -f /tmp/upipe
	mkfifo /tmp/upipe
	"${COCOAD}" progressbar --title "Downloading Update..." --text "Please wait..." < /tmp/upipe &
	exec 7<> /tmp/upipe
	echo -n . >&7

	dlp="Downloading"
	num=0

    local errors=0
    while :; do
        debug "Processes remaining: $*"
        for pid in "$@"; do
            shift
            if kill -0 "$pid" 2>/dev/null; then
                debug "$pid is still alive."
                set -- "$@" "$pid"
                
                if [[ $num = 0 ]]; then
                	dlp="Downloading.  "
                	num=1
                elif	[[ $num = 1 ]]; then
                	dlp="Downloading.. "
                	num=2
                else
                	dlp="Downloading..."
                	num=0
                fi
                output=$(tail -n 1 /tmp/derp.txt) 
                output=${output##* }
                echo "${output} ${dlp} ${output}" >&7
                                
            elif wait "$pid"; then
            	debug "$pid exited with zero exit status."
                echo "100 Download Complete" >&7
                sleep 1
            else
                debug "$pid exited with non-zero exit status."
                ((++errors))
            fi
        done
        (("$#" > 0)) || break
        sleep ${WAITALL_DELAY:-.1}
    done
    ((errors == 0))
    
    exec 7>&-
	rm -f /tmp/upipe
	rm -f /tmp/derp.txt
}

debug() 
{ 
	echo "DEBUG: $*" >/dev/null
}

ORIDIR="$1"
autoInstall="$2"
COCOAD=./bin/CocoaDialog.app/Contents/MacOS/CocoaDialog
number=0
echo -n "Checking for updates"
while [ 1 ]; do
	echo -n "."
	sleep .4
done &
KILLME=$!
pinger
wait

#END