#!/usr/local/bin/cbsd
. ${subrdir}/nc.subr
. ${tools}
. ${dialog}
. ${strings}


# form for $rnodename
get_nodename()
{
	local _input

	f_dialog_title " nodename "
	f_dialog_input _input "Enter IP or DNS name of remote node" "${rnodename}" \
		"${_message}" || return $?

	rnodename="${_input}"
}


# form for $port
get_port()
{
	local _input

	f_dialog_title " port "
	f_dialog_input _input "Enter remote node SSH port" "${port}" \
		"${_message}" || return $?

	port="${_input}"
}

# form for $password
get_password()
{
	local prompt1="Enter CBSD user password of remote node"
	local _input

	local height1 width1
	f_dialog_inputbox_size height1 width1 \
		"$DIALOG_TITLE"     \
		"$DIALOG_BACKTITLE" \
		"$prompt1"          \
		""                  \
		"$hline"

	local _password1 _password2
	_input=$( $DIALOG \
	--title "$DIALOG_TITLE"         \
	--backtitle "$DIALOG_BACKTITLE" \
	--hline "$hline"                \
	--ok-label "$msg_ok"            \
	--cancel-label "$msg_cancel"    \
	--insecure                      \
	--passwordbox "$prompt1"        \
	$height1 $width1                \
	2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
	) || return $?

	f_dialog_line_sanitize _input
	password="${_input}"
}


# dialog_menu_main
#
# Display the dialog(1)-based application main menu.
#
dialog_menu_main()
{
	local _input _retval

	local title="$DIALOG_TITLE"
	local btitle="$DIALOG_BACKTITLE"

	local f_dialog_title=" Add CBSD Remote Node "
	hline=

	local prompt="${_desc}"

	if [ -n "${password}" ]; then
		pw_filled="***"
	else
		pw_filled=
	fi

	local menu_list="
		'nodename'	'${rnodename}'		'Hostname or IP address of remote node'
		'port'		'${port}'		'Remove port of SSH daemon'
		'password'	'${pw_filled}'		'Password of CBSD user on remote node'
		'-'		'-'			''
		'ADD'		''			'Add new node via: cbsd node mode=add node=${rnodename} port=${port}'
	" # END-QUOTE

	cbsd_menubox_with_help
	retval=$?

	f_dialog_data_sanitize menu_choice
	f_dialog_menutag_store "$menu_choice"
	f_dialog_default_store "$menu_choice"

	return $retval
}


############################################################ MAIN
export NOCOLOR=1

MYARG=""
MYOPTARG=""
MYDESC="Add node by ssh"
CBSDMODULE="bhyve"

globalconf="${distdir}/cbsd.conf";

set -e
. ${globalconf}
set +e

. ${cbsdinit}

. ${subrdir}/bsdconfig.subr
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages-nodecfg.subr

# Incorporate rc-file if it exists
[ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"

#
# Process command-line arguments
#
while getopts h$GETOPTS_STDARGS flag; do
	case "$flag" in
	h|\?) f_usage $BSDCFG_LIBE/$APP_DIR/USAGE "PROGRAM_NAME" "$pgm" ;;
	esac
done
shift $(( $OPTIND - 1 ))

#
# Initialize
#
f_dialog_title " $msg_add_new_node "
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
f_mustberoot_init

port=22
nodename=
password=
pw_filled=

#
# Loop over the main menu until we've accomplished what we came here to do
#
while :; do
	dialog_menu_main
	ret=$?

	command=
	case $ret in
		${DIALOG_OK})
			case "$mtag" in
				?" $msg_exit")
					break
					;;
				nodename|port|password)
					get_${mtag}
					;;
				"ADD")
					[ -z "${rnodename}" ] && f_dialog_msgbox "nodename field is mandatory" && continue
					[ -z "${port}" ] && f_dialog_msgbox "port field is mandatory" && continue
					[ -z "${password}" ] && f_dialog_msgbox "password field is mandatory" && continue
					f_dialog_info "Trying to communicate with cbsd@${rnodename}:${port}. Please wait..."
					res=$( ${MKTEMP_CMD} )
					trap "/bin/rm -f ${res}" HUP INT ABRT BUS TERM EXIT
					echo "cbsd mode=add node=\"${rnodename}\" port=\"${port}\":" > ${res}
					echo >> ${res}
					node mode=add node="${rnodename}" port="${port}" pw="${password}" >> ${res} 2>&1
					f_show_help "${res}"
					/bin/rm -f ${res}
					trap "" HUP INT ABRT BUS TERM EXIT
					;;
				*)
					continue
					;;
			esac
			;;
		${DIALOG_HELP})
			get_help
			continue
			;;
		*)
			f_die
			;;
	esac
done

return $SUCCESS

################################################################################
# END
################################################################################
