# -*-shell-script-*-

# UI abstraction protocol code

###
#
# This code is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
# 
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Copyright 2002 Mike Hearn (mike@theoretic.com)
#
###

PROTOCOL_VERSION=1

function _output() {
    cat <<EOF > "$autopackage_pipe"
$1
$2
EOF

    fe_result=`cat "$autopackage_pipe"`  # wait for feedback signal

    if [[ "$fe_result" == "OK" ]]; then
	return 0;
    elif [[ "$fe_result" == "ABORT" ]]; then

	return 1;
    fi
}

function outputStatus() {
    _output "STATUS" "$1"
    return $?
}

##
# outputTest <what>
# what: A short description of the test currently being performed
#
# This function is designed to be used within package prep scripts. It will be called for you by require()
# when using the skeletons framework, so should be rarely used explicitly, but you can also use it in your own scripts.
# It will indicate to the user that their system is currently being tested. It should be followed by a call
# to either outputTestFail, outputTestPass or outputTestRecommend.
#
# The output will be of the form "Checking for $1.... " - ie you should not provide the preamble, instead you should
# provide a succint description of what is being tested. For instance, a good string to use would be "ability to play
# Ogg Vorbis files", and bad string to use would be "Can we play Ogg files?". Remember: don't phrase it as a question,
# instead as a description.
#
# Also remember to try and keep the description simple. Don't use jargon unless it's really necessary. That way,
# if a test fails and the system cannot fix the problem itself, the users will find it easier to do so themselves.
# 
# Example:
# outputTest "GTK User Interface toolkit"
# outputTest "Ogg Vorbis playback capabilities"
# outputTest "ability to connect to the internet"
function outputTest() {
    _output "TEST" "$1"
    return $?
}


function outputTestFail() {
    _output "FAILTEST"
    return $?
}

function outputTestPass() {
    _output "PASSTEST"
    return $?
}

function outputTestRecommend() {
    _output "RECOMMEND" "$1"
    return $?
}

function outputFail() {
    _output "FAIL" "$@"
    return $?
}

# $1 = binding
# $2 = default
# $3 = message
function outputInteractString() {
    _output "INTERACT STRING" "$1
$2
$3"
    return $?
}

function outputInteractPath() {
    _output "INTERACT PATH" "$1
$2
$3"
    return $?
}

function outputInteractWait() {
    _output "WAIT"
    return $?
}

##############################################

# $1 = binding
# $2 = default
# $3 = message
function interactString() {
    outputInteractString "$1" "$2" "$3"
    return $?;
}

function interactPath() {
    outputInteractPath "$1" "$2" "$3"
    return $?
}

function waitForInteractions() {
    if ! outputInteractWait; then
      if [[ "$abort_script" != "" ]]; then
        eval "$abort_script"
      fi
      echo "ABORTING!!!"
      exit 1; ## ABORT! ABORT! ABORT!
    else
      return 0;
    fi	
}

function get() {
    _output "GET" "$1";
    echo $fe_result;
    exit 0; ### ????????? return zero surely?
}

# $1 = group id
# $2 = description (note, ignored if the group already exists)
function switchGroup() {
    _output "SWITCH GROUP" "$1
$2"
}

###############################################


# this establishes the communications between the front end and the installer
# $1 = the name of the package (just used to label the fe with)
function waitForHELLO() {
    # this will block until something reads from it
    echo "HELLO $PROTOCOL_VERSION
$1" >"$autopackage_pipe"
    read <"$autopackage_pipe"

    if echo "$REPLY" | grep "LETS GO" >/dev/null; then
	return 0
    elif echo "$REPLY" | grep "UHOH" >/dev/null; then
	# something went wrong - return the error code
	error_message="$REPLY"
	return 1
    fi
    return 2; ## wtf? we couldn't understand the answer at all
}

function terminateFE() {
    _output "TERMINATE"
}
