##
##-------------------------------------
# Needs:
# HD_CHOICE
# BOOT_WHERE (also changed here)
# TARGET_MNT_POINT
# FLL_DISTRO_NAME
# HD_FSTYPE 
# BOOT_RUNNING_KERNEL
# TMPDIR
# LANG
# HD_MAP
#
# Calls:
# logit
# get_root_device
# chroot_it
# translate_linux_to_grub
# get_boot_cmdline
# getbootparam
# get_esp
# get_fat
#
# update_status
# install_grub
#
##-------------------------------------
##
install_grub()
{
	local GRUB_KOPT
	local GRUB_DEFOPTIONS
	local GRUB_VGA
	local root_partition
	local root_device
	local device
	local usb_dev
	local point
	local bootfrom
	local grubdev
	
	#
	# log my call
	#
	logit "installing grub"
	#

	root_partition=$(echo ${HD_CHOICE} |cut -d"'" -f2)
	bootfrom=""
        grubdev=""

        case "$BOOT_WHERE" in
                mbr)
                        bootfrom=$(grub-mkdevicemap -n -m - | awk '/^\(hd0\)/{print $2}')
                        grubdev="${bootfrom}"
                        ;;
                partition)
                        bootfrom="${root_partition}"
                        ;;
        esac
	[ -z "${bootfrom}" ] && bootfrom="${BOOT_WHERE}"
	# grub will install on $bf and tell debconf to install it to $gds
	gds=""
	for bf in ${bootfrom}; do
		# get the gd for this bf
		# mbr might have set it but we want it reset each loop otherwise
		if [ -z "$grubdev" ] ; then
			gd="$(ls -l /dev/disk/by-id | awk '/\/'${bf##*/}'$/{print "/dev/disk/by-id/"$9;exit}' )"
		else
			gd="${grubdev}"
		fi
		#
		# install grub
		#
		# is twice because sometimes the first one fails in that cases the second
		# don't fail !!!
		grub-install --recheck --no-floppy --force --root-directory=$TARGET_MNT_POINT "${bf}" >&2 || \
			grub-install --recheck --no-floppy --force --root-directory=$TARGET_MNT_POINT "${bf}" >&2
		if [ -z "${gds}" ]; then
			gds="$gd"
		else
			gds="$gds, $gd"
		fi
	done
	chroot_it sh -c "echo \"grub-pc grub-pc/install_devices multiselect ${gds}\" | debconf-set-selections "

	#
	# portable efi 
	#
	# when the install is on usb and that usb has a fat partition
	# we put portable efi bootloaders in the ESP on the device
	# or the first fat partition
	isusb=""
	for disk in $(fll_disk_device ${root_partition}); do
		if [ -n "$(fll_disk_isusb ${disk})" ]; then
			isusb="${isusb} ${disk}"
		else
			# any non-usb root device means we are not going portable
			isusb=""
			break
		fi
	done
	if [ -n "${isusb}" ]; then
		get_usbefi ${root_partition}
		if [ -n "${usbefi}" ]; then
			HD_MAP="$usbefi:/boot/efi ${HD_MAP}"

			# Mount it now
			mkdir $TARGET_MNT_POINT/boot/efi
			mount ${usbefi} $TARGET_MNT_POINT/boot/efi

			# Install grub-fll-portable-efi in the chroot
			EFIAPTSOURCES=$(mktemp -d --tmpdir=${TARGET_MNT_POINT}/tmp/ liveapt.XXXXXX)
			APTGETBASE="apt-get -o Dir::Etc=${EFIAPTSOURCES##${TARGET_MNT_POINT}}"
			. /lib/init/fll
			extramnt="/fll"
			[ -e "${extramnt}/extras" ] || extramnt="$(fll_get_mnt)"
			mount -o bind ${extramnt} ${TARGET_MNT_POINT}/mnt
			echo 'deb [trusted=yes] file:///mnt/extras sid main' > ${EFIAPTSOURCES}/sources.list
			mkdir ${EFIAPTSOURCES}/preferences.d
			chroot_it ${APTGETBASE} update > /dev/null
			chroot_it ${APTGETBASE} --allow-unauthenticated --assume-yes install grub2-fll-portable-efi > /dev/null
			chroot_it ${APTGETBASE} --allow-unauthenticated --assume-yes remove --purge os-prober > /dev/null || :
			umount ${TARGET_MNT_POINT}/mnt
			echo '' > ${EFIAPTSOURCES}/sources.list
			chroot_it ${APTGETBASE} update > /dev/null
			chroot_it ${APTGETBASE} clean > /dev/null
			rm -rf ${EFIAPTSOURCES}
		fi
	fi

	return 0
}
