#!/bin/sh
#
# This file is Copyright (C) 1995-1997 by Karsten Ballder (Ballueder@usa.net)
#
# It is part of the KBackup package, see the file COPYING for details.
# KBackup and all files included in this package are licensed and protected
# under the terms and conditions of the GNU General Public License Version 2.
#
#	If you want to contact the current maintainer of this software, please
#	send e-mail to: KBackup@usa.net
#       Or visit the KBackup-Homepage at http://KBackup.home.ml.org/
#
#
# replacement for dialog
#
# $Id: dialog,v 1.4 1997/09/19 19:49:25 kbackup Exp $
#
MASK=/tmp/mask$$
LINE="-------------------------------------------------------------------------------"

dialog_menu()
{
	echo -e "$1" >>$MASK; shift	# menu text
	shift; shift;shift;	# skip width, height, height
	allowed=
	while [ $# -gt 0 ]
	do
		echo $1	-	$2 >>$MASK
		allowed="$allowed $1"
		shift; shift
	done
	valid=NO
	while [ "$valid" = "NO" ]
	do
		cat $MASK
		echo -n " > " 
		read input
		input=`echo $input | tr a-z A-Z`
		for i in $allowed  
		do
			if [ "$i" = "$input" ]
			then 
				valid=YES
				echo $i>&2
				return 0
			fi
		done
	done
}

dialog_inputbox()
{
	echo -e "$1">>$MASK
	cat $MASK
	echo -n " > "
	read input
	echo $input >&2
}

dialog_yesno()
{
	echo -e "$1" >>$MASK
	valid=NO
	while [ $valid = NO ]
	do
		cat $MASK
		echo -en "\nPlease choose: [Y]es	[N]o > " 
		read input
		if [ "$input" = "Y" -o "$input" = "y" ]
		then
			return 0
		fi
		if [ "$input" = N -o "$input" = n ]
		then
			return 1
		fi
	done
}


dialog()
{
	clear >$MASK
	if [ "$1" = "--backtitle" ]
	then
		echo $2>>$MASK
		shift;shift
		echo $LINE >>$MASK
	fi
	if [ "$1" = "--title" ]
	then
		echo $2>>$MASK
		shift;shift
	fi

	case $1 in
	--menu)
		shift
		dialog_menu "$@"
		;;
	--msgbox)
		echo -e "$2" 
		echo -n "Press Enter to continue..."
		read
		;;
	--textbox)
		shift
		less $1
		;;
	--yesno)
		shift
		dialog_yesno "$@"
		retval=$?
		rm $MASK
		return $retval
		;;
	--infobox)
		shift
		echo
		echo -e "$1"
		echo
		;;
	--inputbox)
		shift
		dialog_inputbox "$@"
		;;
		
	*)	echo $1 not implemented
		rm $MASK
		return 1
		;;
esac
rm $MASK
}

