#!/bin/sh
set -e

# grub-mkconfig helper script for kazamOS desktop base.
# Based on Debian's 05_debian_theme script.

# Include the GRUB helper library for grub-mkconfig.
. /usr/share/grub/grub-mkconfig_lib

# We want to work in /boot/grub/ only.
test -d /boot/grub || exit 0
cd /boot/grub

# Set the location of a possibly necessary cache file for the background image.
BACKGROUND_CACHE=".background_cache"

module_available(){
	local module
	for module in "${1}.mod" */"${1}.mod"; do
		if [ -f "${module}" ]; then
			return 0
		fi
	done
	return 1
}

set_background_image(){
	# Step 1: Search all available output modes
	local output
	for output in ${GRUB_TERMINAL_OUTPUT}; do
		if [ "x$output" = "xgfxterm" ]; then
			break
		fi
	done

	# Check if we can display a background image at all.
	if ! [ "x${output}" = "xgfxterm" ]; then
		return 1
	fi

	# Step 2: Check if the specified background image exists.
	if ! [ -f "${1}" ]; then
		return 2
	fi

	# Step 3: Find the correct GRUB module for our background image.
	local reader
	case "${1}" in
		*.jpg|*.JPG|*.jpeg|*.JPEG) reader="jpeg";;
		*.png|*.PNG) reader="png";;
		*.tga|*.TGA) reader="tga";;
		*) return 3;; # Unknown image type.
	esac

	# Step 4: Check if the necessary GRUB module is available.
	if ! module_available "${reader}"; then
		return 4
	fi

	# Step 5: Check if GRUB can read the background image directly.
	if is_path_readable_by_grub "${1}" && \
	   [ "`${grub_probe} --target=device "${1}"`" = "`${grub_probe} --target=device .`" ]; then
		rm --force "${BACKGROUND_CACHE}.jpeg" \
			"${BACKGROUND_CACHE}.png" "${BACKGROUND_CACHE}.tga"
	elif cp "${1}" "${BACKGROUND_CACHE}.${reader}"; then
		set -- "${BACKGROUND_CACHE}.${reader}" "${2}" "${3}"
	else
		return 5
	fi

	# Step 6: Prepare GRUB to read the background image.
	if ! prepare_grub_to_access_device "`${grub_probe} --target=device "${1}"`"; then
		return 6
	fi

	# Step 7: Print message and configuration.
	echo "Found kazamOS background image: ${1}" >&2
	echo "insmod ${reader}"
	echo "if background_image `make_system_path_relative_to_its_root "${1}"`; then"
	if [ -n "${2}" ]; then
		echo "  set color_normal=${2}"
	fi
	if [ -n "${3}" ]; then
		echo "  set color_highlight=${3}"
	fi
	if [ -z "${2}" ] && [ -z "${3}" ]; then
		echo "  true"
	fi
	echo "else"
	echo "  set menu_color_normal=white/black"
	echo "  set menu_color_highlight=black/light-gray"
	echo "fi"
}

# Include the configuration from kazamos-desktop-base if available.
if [ -f "/usr/share/desktop-base/grub_background.sh" ]; then
	. "/usr/share/desktop-base/grub_background.sh"
fi

# If user has specified a custom background, use it and exit.
if [ -n "${GRUB_BACKGROUND+x}" ]; then
	set_background_image "${GRUB_BACKGROUND}"
	exit 0
fi

# Try to use the kazamOS background image and colors.
if set_background_image "${WALLPAPER}" "${COLOR_NORMAL}" "${COLOR_HIGHLIGHT}"; then
	exit 0
fi

# Fall back to the desktop-grub.png alternative.
if set_background_image "/usr/share/images/desktop-base/desktop-grub.png"; then
	exit 0
fi
