
# Tries to find the Pashua executable in one of a few default search locations or in
# a custom path passed as optional argument. When it can be found, the filesystem
# path will be in $pashuapath, otherwise $pashuapath will be empty. The return value
# is 0 if it can be found, 1 otherwise.
#
# Argument 1: Path to a folder containing Pashua.app (optional)
locate_pashua() {

    local bundlepath="Pashua.app/Contents/MacOS/Pashua"
    local mypath=`dirname "$0"`

    pashuapath=""

    if [ ! "$1" = "" ]
    then
        searchpaths[0]="$1/$bundlepath"
    fi
    searchpaths[1]="$mypath/Pashua"
    searchpaths[2]="$mypath/$bundlepath"
    searchpaths[3]="./$bundlepath"
    searchpaths[4]="/Applications/$bundlepath"
    searchpaths[5]="$HOME/Applications/$bundlepath"

    for searchpath in "${searchpaths[@]}"
    do
        if [ -f "$searchpath" -a -x "$searchpath" ]
        then
            pashuapath=$searchpath
            return 0
        fi
    done

    return 1
}

# Function for communicating with Pashua
#
# Argument 1: Configuration string
# Argument 2: Path to a folder containing Pashua.app (optional)
pashua_run() {

    # Write config file
    local pashua_configfile=`/usr/bin/mktemp /tmp/pashua_XXXXXXXXX`
    echo "$1" > "$pashua_configfile"

    locate_pashua "$2"

    if [ "" = "$pashuapath" ]
    then
        >&2 echo "Error: Pashua could not be found"
        exit 1
    fi

    # Get result
    local result=$("$pashuapath" "$pashua_configfile")

    # Remove config file
    rm "$pashua_configfile"

    oldIFS="$IFS"
    IFS=$'\n'

    # Parse result
    for line in $result
    do
        local name=$(echo $line | sed 's/^\([^=]*\)=.*$/\1/')
        local value=$(echo $line | sed 's/^[^=]*=\(.*\)$/\1/')
        eval $name='$value'
    done

    IFS="$oldIFS"
}



function pash {
	location=''
	locate_pashua "$location"
	pashua_run "$conf" "$location" 2>/dev/null
}

function _escape_newline {
	text=${text//$'\n'/[return]}
}

_escape_newline


function password {
	conf="
	*.title="$title"
	*.floating=1
	*.autoclosetime="$timeout"
	tx.type=text
	tx.default="$text"
	pw.type=password
	pw.label="$label"
	pw.default="$init"
	cb.type=cancelbutton
	"
	pash
	if [ ! -z "$pw" ]; then
		echo "$pw"
	fi
    exit $cb
}

function input {
	conf="
	*.title="$title"
	*.floating=1
	*.autoclosetime="$timeout"
	txt.type=text
	txt.default="$text"
	tx.type=textfield
	tx.label="$label"
	tx.default="$init"
	cb.type=cancelbutton
	"
	pash
	if [ ! -z "$tx" ]; then
		echo "$tx"
	fi
    exit $cb
}

function confirm_default_no {
	conf="
	*.title="$title"
	*.floating=1
	*.autoclosetime="$timeout"
	txt.type=text
	txt.default="$text"
	ok.type=defaultbutton
	ok.label=No
	cb.type=cancelbutton
	cb.label=Yes
	"
	pash
	if [ $cb != 0 ]; then
		echo "yes"
		exit 0
	else
		exit 1
	fi
}

function confirm_default_yes {
	conf="
	*.title="$title"
	*.floating=1
	*.autoclosetime="$timeout"
	txt.type=text
	txt.default="$text"
	ok.type=defaultbutton
	ok.label=Yes
	cb.type=cancelbutton
	cb.label=No
	"
	pash
	if [ $cb == 0 ]; then
		echo "yes"
	fi
    exit $cb
}

function _select {
	conf="
	*.title="$title"
	*.floating=1
	*.autoclosetime="$timeout"
	txt.type=text
	txt.default="$text"
	tx.type=popup
	tx.label="$label"
	tx.default="$init"
	tx.option="$init"
	cb.type=cancelbutton
	"
	for opt in "${@}"; do
		conf+="tx.option="$opt"
		"
	done
	pash
	if [ ! -z "$tx" ]; then
		echo "$tx"
	fi
    exit $cb
}

function _select_other {
	conf="
	*.title="$title"
	*.floating=1
	*.autoclosetime="$timeout"
	txt.type=text
	txt.default="$text"
	tx.type=combobox
	tx.completion=2
	tx.label="$label"
	tx.default="$init"
	tx.option="$init"
	cb.type=cancelbutton
	"
	for opt in "${@}"; do
		conf+="tx.option="$opt"
		"
	done
	pash
	if [ ! -z "$tx" ]; then
		echo "$tx"
	fi
    exit $cb
}

function select2 {
    if [ "$type" == "select-other" ]; then
      _select_other ${@} 
    else
      _select ${@}
    fi
}

function multiple {
	conf="
	*.title="$title"
	*.floating=1
	*.autoclosetime="$timeout"
	txt.type=text
	txt.default="$text"
	tx.type=textbox
	tx.height=104
	tx.label="$label"
	tx.default="$(printf '%s[return]' "${@}")"
	cb.type=cancelbutton
	"
	pash
	if [ ! -z "$tx" ]; then
		echo "$(echo "$tx" | sed 's/\[return\]/\
/g' | grep -v '^$')"
	fi
    exit $cb
}

function simple_link_QR {
	conf="
	*.title="$title"
	*.floating=1
	*.autoclosetime="$timeout"
	txt.type=text
	txt.default="$text"
	img.type=image
	img.path="$init"
	"
	pash
}


function complex_link {
	conf="
	*.title="$title"
	*.floating=1
	*.autoclosetime="$timeout"
	txt.type=text
	txt.default="$text"
	"
	pash
}
