#!/usr/bin/env cbsd
. ${subrdir}/nc.subr
. ${strings}
. ${tools}
. ${nodes}

# dialog_menu_main
#
# Display the dialog(1)-based application main menu.
#
dialog_menu_main()
{
	local _title="$DIALOG_TITLE"
	local _btitle="$DIALOG_BACKTITLE"
	local _prompt=""
	local _menu_list=
	local _defaultitem=
	local _hline="Hit ENTER for SELECT"
	local _prompt="'I:number' flags means that the base is already installed. \n number is indicated version. Anyway you can upgrade it. \n '*' means is native for this node"
	local _baseelf

	local _f_dialog_title=" CBSD FreeBSD Base Repository "

	for _i in $( ${SEQ_CMD} 1 255 ); do
		base_name=
		eval base_name="\$base${_i}_name"
		[ -z "${base_name}" ] && break
		base_ver=
		base_arch=
		eval base_ver="\$base${_i}_ver"
		eval base_arch="\$base${_i}_arch"

		eval mark=\$selected_base${_i}
		base_installed=
		if [ -f "${basejaildir}/${base_name}/bin/sh" ]; then
			_baseelf=$( ${miscdir}/elf_tables --ver ${basejaildir}/${base_name}/bin/sh 2>/dev/null )
			base_installed="I:${_baseelf}"
		fi
		
		[ "${base_ver}" = "${native_ver}" -a "${base_arch}" = "${native_arch}" ] && base_installed="${base_installed} *"

		[ -z "${mark}" ] && mark=" "
		_menu_list="${_menu_list} '${_i} ${base_name}'	'[${mark}] ${base_installed}'"
	done

	_menu_list="${_menu_list} '-'	''"
	_menu_list="${_menu_list} 'Download'	''"

	local _height _width _rows
	eval f_dialog_menu_size _height _width _rows \
		\"\$DIALOG_TITLE\"     \
		\"\$DIALOG_BACKTITLE\" \
		\"\$_prompt\"           \
		\"\$_hline\"            \
		$_menu_list

	_height=$(( _height + 1 ))

	# Obtain default-item from previously stored selection
	f_dialog_default_fetch _defaultitem

	local _menu_choice
	_menu_choice=$( eval $DIALOG \
		--title \"\$DIALOG_TITLE\"         \
		--backtitle \"\$DIALOG_BACKTITLE\" \
		--hline \"\$_hline\"                \
		--ok-label \"\$msg_ok\"            \
		--cancel-label \"\$msg_cancel\"    \
		--default-item \"\$_defaultitem\"   \
		--menu \" \$_prompt \"              \
		$_height $_width $_rows               \
		$_menu_list                         \
		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
	)

	local _retval=$?
	f_dialog_data_sanitize _menu_choice
	f_dialog_menutag_store "$_menu_choice"

	# Only update default-item on success
	[ $_retval -eq $DIALOG_OK ] && f_dialog_default_store "$_menu_choice"

	return $DIALOG_OK
}


download_bases()
{
	local _fetch_bases=
	local _mark
	local _i
	local _res

	for _i in $( ${SEQ_CMD} 1 255 ); do
		eval base_name="\$base${_i}_name"
		[ -z "${base_name}" ] && break
		eval _mark=\$selected_base${_i}
		[ "${_mark}" = "X" ] && _fetch_bases="${_fetch_bases} ${base_name}"
	done

	[ -z "${_fetch_bases}" ] && return 0

	for _i in ${_fetch_bases}; do
		eval $( echo "${_i}" |${TR_CMD}  "_" " "|while read _base _arch _target _ver; do
			echo "rarch=\"${_arch}\""
			echo "rver=\"${_ver}\""
			_res=$( echo "${_ver}" |${GREP_CMD} -q '\.' )
			stable=$?
			echo "stable=\"${stable}\""
		done )
		echo
		echo "exec: cbsd repo action=get sources=base ver=${rver} stable=${stable} arch=${rarch}"

		export DIALOG="${DIALOG}"
		# return stdout
		exec 3>&-
		repo action=get sources=base ver=${rver} stable=${stable} arch=${rarch} mode=upgrade
		eval exec $TERMINAL_STDOUT_PASSTHRU\>\&1
		eval exec $TERMINAL_STDERR_PASSTHRU\>\&2
	done
	echo "Press a key..."
	read p
}


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

. ${subrdir}/bsdconfig.subr
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages-repo.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_repo_bases "
f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"

baselist=$( ${MKTEMP_CMD} )
f_dialog_info "Obtain index files from ${repo}. Please wait..."
trap "rm -f ${baselist}" HUP INT ABRT BUS TERM EXIT
res=$( 2>/dev/null repo quiet=1 action=list sources=base ver=any arch=any > ${baselist} )

_i=1

eval $( ${CAT_CMD} ${baselist} |while read _line; do
	echo "base${_i}_name=\"${_line}\""

	echo "${_line}" |${TR_CMD} "_" " " |while read _base _arch _target _ver; do
		echo "base${_i}_arch=\"${_arch}\""
		echo "base${_i}_ver=\"${_ver}\""
	done

	_i=$(( _i + 1 ))
done )

#
# Loop over the main menu until we've accomplished what we came here to do
#
while :; do
	dialog_menu_main || break
	f_dialog_menutag_fetch _mtag

	[ -z "${_mtag}" ] && exit $SUCCESS

	_command=

	case "${_mtag}" in
		?" $msg_exit") break ;;
		"Download")
				download_bases
				exit 0
				;;
		"-")
				continue
				;;
		*)
			index=${_mtag%% *}
			base=${_mtag##* }
			eval mark=\$selected_base${index}
			if [ "${mark}" = "X" ];then
				eval "selected_base${index}=\" \""
			else
				eval "selected_base${index}=\"X\""
			fi
			continue
			;;
	esac

	if [ "${_command}" ]; then
		$BSDCFG_LIBE/$APP_DIR/${_command} ${USE_XDIALOG:+-X}
	fi

done

return $SUCCESS

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