#
#   Copyright (C) 2022 SUSE LLC
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program 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 General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#   Written by Olaf Kirch <okir@suse.com>

function d_stolen_from_jeos {

	dialog_out=`mktemp -qt 'firstboot-XXXXXX'`

	retval=
	while true
	do
		retval=0
		dialog --backtitle "$PRETTY_NAME" --output-fd 3 "$@" 3>"${dialog_out}" || retval=$?
		case $retval in
		  0)
			# need || true as dialog doesn't write newlines
			read result < $dialog_out || true
			rm -f $dialog_out
			return 0
			;;
		  1)
			echo "$(xargs -a "$dialog_out")" >/var/log/jeos
			dialog --backtitle "$PRETTY_NAME" --yesno $"Do you really want to quit?" 0 0 && exit 1
			continue
			;;
		  255)
			# xargs to remove whitespaces
			echo "$(xargs -a "$dialog_out")" >/var/log/jeos
			result_error="$(xargs -a "$dialog_out")"
			if [ -z "$result_error" ]; then
				dialog --backtitle "$PRETTY_NAME" --yesno $"Do you really want to quit?" 0 0 && exit 1
				continue
			fi
			logger -p err -t jeos-firstboot "$result_error"
			dialog --backtitle "$PRETTY_NAME" --msgbox $"Exiting due to error, please check the system log" 0 0
			rm -f $dialog_out
			exit 2
			;;
		esac
	done
}

# If we're used by jeos-firstboot, d() will already be defined. If not,
# overwrite it with ours.
if [ "$(type -t d)" != "function" ]; then
    alias d=d_stolen_from_jeos
fi

##################################################################
# Display an error box
##################################################################
function display_errorbox {

    logger -t fde-tools -p local0.err "$*"
    d --title ERROR --msgbox "$*" 8 60
}

function display_infobox {

    logger -t fde-tools -p local0.info "$*"
    d --infobox "$*" 5 40
}

##################################################################
# request_password MESSAGE
#	Prompt the user for a password, using the first argument
#	as prompt message.
#	On success, it sets the global variable result_password
#	and returns 0.
#	If the user cancelled the dialog, it returns 1.
##################################################################
function request_password {

    declare -g result_password

    prompt="$1"

    if ! d --no-cancel --insecure --passwordbox "${prompt}" 8 50; then
	result_password=""
	return 1
    fi
    result_password="${result}"
    return 0
}

##################################################################
# request_new_password MESSAGE
#	Prompt the user for a password, using the first argument
#	as prompt message. Have the user confirm the password to
#	avoid typos.
#	On success, it sets the global variable result_password
#	and returns 0.
#	If the user cancelled the dialog, it returns 1.
##################################################################
function request_new_password {

    declare -g result_password

    prompt="$1"

    result_password=""

    attempt=0
    while [ $attempt -lt 3 ]; do
	let attempt+=1

	if ! d --no-cancel --insecure --passwordbox "${prompt}" 8 50; then
	    continue
	fi

	password1="$result"

	if d --no-cancel --insecure --passwordbox "Please retype password" 8 50; then
	    password2="$result"
	    if [ "${password1}" = "${password2}" ];  then
		result_password="${password1}"
		return 0
	    fi
	fi
    done
    return 1
}

##################################################################
# display_gauge MESSAGE
#	Display a gauge meter like "dialog --gauge" does. This
#	function receives integer values from 0 to 100 on standard
#	input and should render a progress bar with the provided
#	message above or below the bar.
##################################################################
function display_gauge {

    d --gauge "$*" 7 40
}

