#!/bin/bash

# Upgrade Script

# Function to draw WinREST API Objects
function W() {
	wcall "$@" || exit -1
}

# Translate strings by request
function Trans() {
	local Dictionary="/home/winrest/plugins/wbrowser/plugin.dic"
	if [ -n "$1" ]; then
		[ -n "$2" ] && Dictionary="$2"
		translate "$Dictionary" "$1"
	else
		echo "$1"
	fi
}

# Check if directory exists and delete all in order to create it
function CheckDir() {
	[ -e $1 ] && rm -Rf $1
	mkdir $1
}

# Show WAP description
function ShowFile() {
	# Create WObjects
	local HWIN=`W 0 CreateObject Window 0` && W $HWIN Init 5 5 50 38 "`Trans "$2"`"
	local HList=`W 0 CreateObject ListBox 0` && W $HList Init $HWIN 1 1 48 29 "" 0
	local HOk=`W 0 CreateObject Button 0` && W $HOk Init $HWIN 39 31 10 6 "`Trans "OK"`" 1 511 28
	
	# Show description
	if [ -f "$1/wap.dic" ]; then
		echo "`Trans "[descrip]" "$1/wap.dic"`" | { while read; do
			W $HList Add "$REPLY" -1
		done }
	else
		cat $1/descrip | { while read; do
			W $HList Add "$REPLY" -1
		done }
	fi
	W $HList SetShowSelection 0
	
	# Manage events
	local -i HRet=-1 i=0
	while [ $HRet -ne 1 ]; do
		HRet=`W $HWIN DoEvents 0 0`
	done
	
	# Delete WObjects
	for i in $HWIN $HList $HOk; do W 0 DeleteObject $i; done
}

function Question() {
	# Create WObjects
	local -i WindowHeight=15
	[ -n "$2" ] && WindowHeight=19
	local WINDOW=`W 0 CreateObject Window 0` && W $WINDOW Init 16 17 40 $WindowHeight "`Trans "Confirmation"`"
	local Relevo=`W 0 CreateObject Bevel 0` && W $Relevo Init $WINDOW 1 1 38 $(( $WindowHeight - 9 )) 1 1
	local Texto1=`W 0 CreateObject Label 0` && W $Texto1 Init $WINDOW 3 3 "$1" 3 255 -1 0
	[ -n "$2" ] && local Texto2=`W 0 CreateObject Label 0` && W $Texto2 Init $WINDOW 3 7 "$2" 3 255 -1 0
	local BOk=`W 0 CreateObject Button 0` && W $BOk Init $WINDOW 29 $(( $WindowHeight - 7 )) 10 6 "`Trans "OK"`" 1 511 28
	local BCanc=`W 0 CreateObject Button 0` && W $BCanc Init $WINDOW 18 $(( $WindowHeight - 7 )) 10 6 "`Trans "Cancel"`" 2 767 1
	
	# Manage events
	local -i QRet=-1 i=0
	while [ $QRet != 1 ] && [ $QRet != 2 ]; do
		QRet=`W $WINDOW DoEvents 0 0`
	done
	case $QRet in
		1)	QRet=0	;;
		2)	QRet=1	;;
	esac
	
	# Delete WObjects
	for i in $WINDOW $BOk $BCanc $Texto1 $Relevo; do W 0 DeleteObject $i; done
	[ -n "$2" ] && W 0 DeleteObject $Texto2
	return $QRet
}

# Start script
WAPFile="$1"
WAPDir="/tmp/tmp_updates"

# Delete tempdir if exists and create a new one
CheckDir "$WAPDir"

# Unpack file
if [ -f "$WAPFile" ]; then
	tar -C "$WAPDir" -zxf "$WAPFile"
	rm -f "$WAPFile"
fi

# If the WAP file has a description, show it
[ -f "$WAPDir/wap.dic" ] || [ -f "$WAPDir/descrip" ] && ShowFile "$WAPDir" "Description"

# Ask for confirmation
declare WapName="`Trans "[descrip]" "$WAPDir/wap.dic" | head -n 1`"
[ "$WapName" == "[descrip]" ] && WapName=""
if ( Question "`Trans "Press OK to install WAP."`" "$WapName" ); then
	# Run WAP execution file
	cd "$WAPDir"
	"$WAPDir/run" "$WAPDir" "$WAPDir"
	cd ..
fi

# Delete tempdir
rm -Rf "$WAPDir"

# Exit script
exit 0
