# Whiptail functions for modconf

# (c) 2000 Software in the Public Interest
# Maintainer: Randolph Chung <tausq@debian.org> and debian-boot@lists.debian.org

# Derived from:
# Shell interface to "dialog"
# Bruce Perens, November 1995
# This is free software under the GNU General Public License.

export DIALOG_COMMAND=whiptail
#test -x /usr/bin/dialog && export DIALOG_COMMAND=dialog
# dialog breaks on zero-arguments

ttydev=${ttydev:-/dev/tty}

# Global options
#  The variable $BACKTITLE specifies the back title.
#  The variable $DIALOG_OPTIONS, initialized here to --clear, provides
#  options you want on the command line of each dialog invocation.
#  The variable $DIALOG_TEST can be set to "echo" to see the calls
#  to dialog without executing them.

DIALOG_OPTIONS=""
BACKTITLE=""

# Make any dialogue box, with default settings and backtitle from
# $BACKTITLE in the environment.
#
# dialog [options] -- --type arg arg ...
#
dialogBox () {
  local args

  while true; do
      if [ "$1" = "--" ]; then
	  shift
	  break
      else
	  args="$args $1"
	  shift
      fi
  done

  local type="$1"
  shift
  local title=""
  local backtitle=""
  local result
  local status

  local text="$1"
  shift

  if [ $# -ge 1 ]; then
    title="$1"
    shift
  fi

  if [ -n "$BACKTITLE" ]; then
    backtitle="$BACKTITLE"
  fi

  # do not confuse whiptail with different LC_* vars, we do not need
  # LC_MESSAGES at this point anyways
  unset LC_MESSAGES || true
  $DIALOG_TEST $DIALOG_COMMAND $DIALOG_OPTIONS $args --title "$title" \
     --backtitle "$backtitle" "$type" "$text " 0 0 "$@" 2>&1 1>$ttydev
  
  return $?
}

# Display a file.
#
# fileBox filename [title]
#
fileBox () {
  dialogBox -- --textbox "$1" "$2"
}

msgBox () {
  dialogBox -- --msgbox "$1" "$2"
}

infoBox () {
  dialogBox -- --infobox "$1" "$2"
}

yesNoBox () {
  dialogBox -- --yesno "$1" "$2"
  return $?
}

inputBox () {
  dialogBox -- --inputbox "$1" "$2" "$3" "$4"
  return $?
}

# menu text title defaultitem tag1 item1 ...
menu () {
  local text="$1"
  shift
  local title="$1"
  shift
  local defaultitem="$1"
  shift
  if [ -z "$defaultitem" ]; then
      dialogBox -- --menu "$text" "$title" 0 "$@"
  else
      dialogBox --default-item "$defaultitem" -- --menu "$text" "$title" 0 "$@"
  fi
  return $?
}

# menu text title tag1 item1 status1 ...
checklist () {
  local text="$1"
  shift
  local title="$1"
  shift
  dialogBox -- --checklist "$text" "$title" 0 "$@"
  return $?
}

# menu text title tag1 item1 status1 ...
radiolist () {
  local text="$1"
  shift
  local title="$1"
  shift
  dialogBox -- --radiolist "$text" "$title" 0 "$@"
  return $?
}

# Local Variables:
# mode: shell-script
# End:
